]> Sergey Matveev's repositories - btrtrc.git/blobdiff - torrent.go
Limit half-open connections at the Client level
[btrtrc.git] / torrent.go
index 639e330e21ad61f2cbeb2ea778c7b4554e9e3846..21069ec5fe0cd0324b42aaf2a460f01dc36a9785 100644 (file)
@@ -1075,7 +1075,7 @@ func (t *Torrent) maxHalfOpen() int {
        return int(min(max(5, extraIncoming)+establishedHeadroom, int64(t.cl.config.HalfOpenConnsPerTorrent)))
 }
 
-func (t *Torrent) openNewConns() {
+func (t *Torrent) openNewConns() (initiated int) {
        defer t.updateWantPeersEvent()
        for t.peers.Len() != 0 {
                if !t.wantConns() {
@@ -1087,9 +1087,14 @@ func (t *Torrent) openNewConns() {
                if len(t.cl.dialers) == 0 {
                        return
                }
+               if t.cl.numHalfOpen >= t.cl.config.TotalHalfOpenConns {
+                       return
+               }
                p := t.peers.PopMax()
                t.initiateConn(p)
+               initiated++
        }
+       return
 }
 
 func (t *Torrent) getConnPieceInclination() []int {
@@ -1889,7 +1894,6 @@ func (t *Torrent) initiateConn(peer PeerInfo) {
        if peer.Id == t.cl.peerID {
                return
        }
-
        if t.cl.badPeerAddr(peer.Addr) && !peer.Trusted {
                return
        }
@@ -1897,6 +1901,7 @@ func (t *Torrent) initiateConn(peer PeerInfo) {
        if t.addrActive(addr.String()) {
                return
        }
+       t.cl.numHalfOpen++
        t.halfOpen[addr.String()] = peer
        go t.cl.outgoingConnection(t, addr, peer.Source, peer.Trusted)
 }