import (
"bufio"
+ "container/heap"
"crypto/rand"
"crypto/sha1"
"errors"
duplicateConnsAvoided = expvar.NewInt("duplicateConnsAvoided")
)
-// Justification for set bits follows.
-//
-// Extension protocol: http://www.bittorrent.org/beps/bep_0010.html
-// DHT: http://www.bittorrent.org/beps/bep_0005.html
-const extensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x01"
+const (
+ // Justification for set bits follows.
+ //
+ // Extension protocol: http://www.bittorrent.org/beps/bep_0010.html
+ // DHT: http://www.bittorrent.org/beps/bep_0005.html
+ extensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x01"
+
+ socketsPerTorrent = 40
+)
// Currently doesn't really queue, but should in the future.
func (cl *Client) queuePieceCheck(t *torrent, pieceIndex pp.Integer) {
}
}
t.Conns = append(t.Conns, c)
+ if len(t.Conns) > socketsPerTorrent {
+ wcs := t.worstConnsHeap()
+ heap.Pop(wcs).(*connection).Close()
+ }
return true
}
if me.halfOpen >= me.halfOpenLimit {
return
}
+ if me.halfOpen+me.handshaking+len(t.Conns) >= socketsPerTorrent {
+ break
+ }
var (
k peersKey
p Peer
package torrent
import (
+ "container/heap"
"container/list"
"fmt"
"io"
"log"
"net"
+ "sort"
"sync"
"bitbucket.org/anacrolix/go.torrent/util"
metadataHave []bool
}
+func (t *torrent) worstConnsHeap() (wcs *worstConnsHeap) {
+ wcs = new(worstConnsHeap)
+ *wcs = make([]*connection, 0, len(t.Conns))
+ *wcs = append(*wcs, t.Conns...)
+ heap.Init(wcs)
+ return
+}
+
func (t *torrent) CeaseNetworking() {
t.stateMu.Lock()
defer t.stateMu.Unlock()
fmt.Fprintln(w)
fmt.Fprintf(w, "Pending peers: %d\n", len(t.Peers))
fmt.Fprintf(w, "Active peers: %d\n", len(t.Conns))
+ wcs := new(worstConnsHeap)
+ *wcs = t.Conns
+ sort.Sort(wcs)
for _, c := range t.Conns {
c.WriteStatus(w)
}