From 377cb62da9f0397db2645131616721304c4bf27a Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 16 Feb 2018 12:15:07 +1100 Subject: [PATCH] Make half-open connection limit more dynamic --- torrent.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/torrent.go b/torrent.go index ffaedb3f..007d35de 100644 --- a/torrent.go +++ b/torrent.go @@ -82,7 +82,8 @@ type Torrent struct { info *metainfo.Info files *[]*File - // Active peer connections, running message stream loops. + // Active peer connections, running message stream loops. TODO: Make this + // open (not-closed) connections only. conns map[*connection]struct{} maxEstablishedConns int // Set of addrs to which we're attempting to connect. Connections are @@ -1054,13 +1055,32 @@ func (t *Torrent) pieceCompletionChanged(piece int) { t.updatePiecePriority(piece) } +func (t *Torrent) numReceivedConns() (ret int) { + for c := range t.conns { + if c.Discovery == peerSourceIncoming { + ret++ + } + } + return +} + +func (t *Torrent) maxHalfOpen() int { + // Note that if we somehow exceed the maximum established conns, we want + // the negative value to have an effect. + establishedHeadroom := int64(t.maxEstablishedConns - len(t.conns)) + extraIncoming := int64(t.numReceivedConns() - t.maxEstablishedConns/2) + // We want to allow some experimentation with new peers, and to try to + // upset an oversupply of received connections. + return int(min(max(5, extraIncoming)+establishedHeadroom, int64(t.cl.halfOpenLimit))) +} + func (t *Torrent) openNewConns() { defer t.updateWantPeersEvent() for len(t.peers) != 0 { if !t.wantConns() { return } - if len(t.halfOpen) >= t.cl.halfOpenLimit { + if len(t.halfOpen) >= t.maxHalfOpen() { return } var ( -- 2.50.0