X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=client.go;h=b0f4b2a3728a497a5e0061379f6def8f136fce33;hb=a8ab0baec81a0856c38bb5ff77da490670dfbc66;hp=e641ffab74588b9c7d767f5648e2f0588bd5b14c;hpb=9be2500729825626e15d89bc6437455f9a826c93;p=btrtrc.git diff --git a/client.go b/client.go index e641ffab..b0f4b2a3 100644 --- a/client.go +++ b/client.go @@ -35,7 +35,6 @@ import ( "github.com/dustin/go-humanize" gbtree "github.com/google/btree" "github.com/pion/datachannel" - "golang.org/x/time/rate" "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/internal/check" @@ -82,9 +81,8 @@ type Client struct { torrents map[InfoHash]*Torrent pieceRequestOrder map[interface{}]*request_strategy.PieceRequestOrder - acceptLimiter map[ipStr]int - dialRateLimiter *rate.Limiter - numHalfOpen int + acceptLimiter map[ipStr]int + numHalfOpen int websocketTrackers websocketTrackers @@ -201,7 +199,6 @@ func (cl *Client) init(cfg *ClientConfig) { cl.config = cfg g.MakeMap(&cl.dopplegangerAddrs) cl.torrents = make(map[metainfo.Hash]*Torrent) - cl.dialRateLimiter = rate.NewLimiter(10, 10) cl.activeAnnounceLimiter.SlotsPerKey = 2 cl.event.L = cl.locker() cl.ipBlockList = cfg.IPBlocklist @@ -734,6 +731,19 @@ func doProtocolHandshakeOnDialResult( // Returns nil connection and nil error if no connection could be established for valid reasons. func (cl *Client) dialAndCompleteHandshake(opts outgoingConnOpts) (c *PeerConn, err error) { + // It would be better if dial rate limiting could be tested when considering to open connections + // instead. Doing it here means if the limit is low, and the half-open limit is high, we could + // end up with lots of outgoing connection attempts pending that were initiated on stale data. + { + dialReservation := cl.config.DialRateLimiter.Reserve() + if !opts.receivedHolepunchConnect { + if !dialReservation.OK() { + err = errors.New("can't make dial limit reservation") + return + } + time.Sleep(dialReservation.Delay()) + } + } torrent.Add("establish outgoing connection", 1) addr := opts.peerInfo.Addr dialPool := dialPool{ @@ -771,7 +781,9 @@ func (cl *Client) dialAndCompleteHandshake(opts outgoingConnOpts) (c *PeerConn, if !opts.receivedHolepunchConnect { g.MakeMapIfNilAndSet(&cl.undialableWithoutHolepunch, holepunchAddr, struct{}{}) } - opts.t.startHolepunchRendezvous(holepunchAddr) + if !opts.skipHolepunchRendezvous { + opts.t.trySendHolepunchRendezvous(holepunchAddr) + } cl.unlock() } err = fmt.Errorf("all initial dials failed") @@ -796,6 +808,12 @@ func (cl *Client) dialAndCompleteHandshake(opts outgoingConnOpts) (c *PeerConn, torrent.Add("initiated conn with preferred header obfuscation", 1) return } + c.logger.Levelf( + log.Debug, + "error doing protocol handshake with header obfuscation %v", + obfuscatedHeaderFirst, + ) + firstDialResult.Conn.Close() // We should have just tried with the preferred header obfuscation. If it was required, there's nothing else to try. if headerObfuscationPolicy.RequirePreferred { return @@ -820,6 +838,12 @@ func (cl *Client) dialAndCompleteHandshake(opts outgoingConnOpts) (c *PeerConn, torrent.Add("initiated conn with fallback header obfuscation", 1) return } + c.logger.Levelf( + log.Debug, + "error doing protocol handshake with header obfuscation %v", + !obfuscatedHeaderFirst, + ) + secondDialResult.Conn.Close() return } @@ -841,7 +865,6 @@ func (cl *Client) outgoingConnection( opts outgoingConnOpts, attemptKey outgoingConnAttemptKey, ) { - cl.dialRateLimiter.Wait(context.Background()) c, err := cl.dialAndCompleteHandshake(opts) if err == nil { c.conn.SetWriteDeadline(time.Time{}) @@ -1283,7 +1306,7 @@ func (cl *Client) newTorrentOpt(opts AddTorrentOpts) (t *Torrent) { t.smartBanCache.Hash = sha1.Sum t.smartBanCache.Init() t.networkingEnabled.Set() - t.logger = cl.logger.WithContextValue(t).WithNames("torrent", t.infoHash.HexString()).WithDefaultLevel(log.Debug) + t.logger = cl.logger.WithDefaultLevel(log.Debug) t.sourcesLogger = t.logger.WithNames("sources") if opts.ChunkSize == 0 { opts.ChunkSize = defaultChunkSize @@ -1590,8 +1613,8 @@ func (cl *Client) newConnection(nc net.Conn, opts newConnectionOpts) (c *PeerCon } c.logger.Levelf( log.Debug, - "new PeerConn %p [Client %p remoteAddr %v network %v outgoing %t]", - c, cl, opts.remoteAddr, opts.network, opts.outgoing, + "inited with remoteAddr %v network %v outgoing %t", + opts.remoteAddr, opts.network, opts.outgoing, ) for _, f := range cl.config.Callbacks.NewPeer { f(&c.Peer)