12 "github.com/RoaringBitmap/roaring"
13 "github.com/anacrolix/chansync"
14 . "github.com/anacrolix/generics"
15 "github.com/anacrolix/log"
16 "github.com/anacrolix/missinggo/iter"
17 "github.com/anacrolix/missinggo/v2/bitmap"
18 "github.com/anacrolix/multiless"
20 "github.com/anacrolix/torrent/internal/alloclim"
21 "github.com/anacrolix/torrent/mse"
22 pp "github.com/anacrolix/torrent/peer_protocol"
23 request_strategy "github.com/anacrolix/torrent/request-strategy"
24 "github.com/anacrolix/torrent/typed-roaring"
29 // First to ensure 64-bit alignment for atomics. See #262.
39 RemoteAddr PeerRemoteAddr
40 // The local address as observed by the remote peer. WebRTC seems to get this right without needing hints from the
42 localPublicAddr peerLocalPublicAddr
43 bannableAddr Option[bannableAddr]
44 // True if the connection is operating over MSE obfuscation.
46 cryptoMethod mse.CryptoMethod
49 closed chansync.SetOnce
50 // Set true after we've added our ConnStats generated during handshake to
51 // other ConnStat instances as determined when the *Torrent became known.
52 reconciledHandshakeStats bool
54 lastMessageReceived time.Time
55 completedHandshake time.Time
56 lastUsefulChunkReceived time.Time
57 lastChunkSent time.Time
59 // Stuff controlled by the local peer.
60 needRequestUpdate string
61 requestState request_strategy.PeerRequestState
62 updateRequestsTimer *time.Timer
63 lastRequestUpdate time.Time
64 peakRequests maxRequests
65 lastBecameInterested time.Time
66 priorInterest time.Duration
68 lastStartedExpectingToReceiveChunks time.Time
69 cumulativeExpectedToReceiveChunks time.Duration
70 _chunksReceivedWhileExpecting int64
73 piecesReceivedSinceLastRequestUpdate maxRequests
74 maxPiecesReceivedBetweenRequestUpdates maxRequests
75 // Chunks that we might reasonably expect to receive from the peer. Due to latency, buffering,
76 // and implementation differences, we may receive chunks that are no longer in the set of
77 // requests actually want. This could use a roaring.BSI if the memory use becomes noticeable.
78 validReceiveChunks map[RequestIndex]int
79 // Indexed by metadata piece, set to true if posted and pending a
81 metadataRequests []bool
82 sentHaves bitmap.Bitmap
84 // Stuff controlled by the remote peer.
87 peerRequests map[Request]*peerRequestState
88 PeerPrefersEncryption bool // as indicated by 'e' field in extension handshake
90 // The highest possible number of pieces the torrent could have based on
91 // communication with the peer. Generally only useful until we have the
93 peerMinPieces pieceIndex
94 // Pieces we've accepted chunks for from the peer.
95 peerTouchedPieces map[pieceIndex]struct{}
96 peerAllowedFast typedRoaring.Bitmap[pieceIndex]
98 PeerMaxRequests maxRequests // Maximum pending requests the peer allows.
99 PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber
100 PeerClientName atomic.Value
107 peerRequestState struct {
109 allocReservation *alloclim.Reservation
112 PeerRemoteAddr interface {
116 peerRequests = orderedBitmap[RequestIndex]
120 PeerSourceTracker = "Tr"
121 PeerSourceIncoming = "I"
122 PeerSourceDhtGetPeers = "Hg" // Peers we found by searching a DHT.
123 PeerSourceDhtAnnouncePeer = "Ha" // Peers that were announced to us by a DHT.
125 // The peer was given directly, such as through a magnet link.
126 PeerSourceDirect = "M"
129 func (p *Peer) initRequestState() {
130 p.requestState.Requests = &peerRequests{}
133 func (cn *Peer) updateExpectingChunks() {
134 if cn.expectingChunks() {
135 if cn.lastStartedExpectingToReceiveChunks.IsZero() {
136 cn.lastStartedExpectingToReceiveChunks = time.Now()
139 if !cn.lastStartedExpectingToReceiveChunks.IsZero() {
140 cn.cumulativeExpectedToReceiveChunks += time.Since(cn.lastStartedExpectingToReceiveChunks)
141 cn.lastStartedExpectingToReceiveChunks = time.Time{}
146 func (cn *Peer) expectingChunks() bool {
147 if cn.requestState.Requests.IsEmpty() {
150 if !cn.requestState.Interested {
156 haveAllowedFastRequests := false
157 cn.peerAllowedFast.Iterate(func(i pieceIndex) bool {
158 haveAllowedFastRequests = roaringBitmapRangeCardinality[RequestIndex](
159 cn.requestState.Requests,
160 cn.t.pieceRequestIndexOffset(i),
161 cn.t.pieceRequestIndexOffset(i+1),
163 return !haveAllowedFastRequests
165 return haveAllowedFastRequests
168 func (cn *Peer) remoteChokingPiece(piece pieceIndex) bool {
169 return cn.peerChoking && !cn.peerAllowedFast.Contains(piece)
172 func (cn *Peer) cumInterest() time.Duration {
173 ret := cn.priorInterest
174 if cn.requestState.Interested {
175 ret += time.Since(cn.lastBecameInterested)
180 func (cn *Peer) locker() *lockWithDeferreds {
181 return cn.t.cl.locker()
184 func (cn *Peer) supportsExtension(ext pp.ExtensionName) bool {
185 _, ok := cn.PeerExtensionIDs[ext]
189 // The best guess at number of pieces in the torrent for this peer.
190 func (cn *Peer) bestPeerNumPieces() pieceIndex {
192 return cn.t.numPieces()
194 return cn.peerMinPieces
197 func (cn *Peer) completedString() string {
198 have := pieceIndex(cn.peerPieces().GetCardinality())
199 if all, _ := cn.peerHasAllPieces(); all {
200 have = cn.bestPeerNumPieces()
202 return fmt.Sprintf("%d/%d", have, cn.bestPeerNumPieces())
205 func eventAgeString(t time.Time) string {
209 return fmt.Sprintf("%.2fs ago", time.Since(t).Seconds())
212 // Inspired by https://github.com/transmission/transmission/wiki/Peer-Status-Text.
213 func (cn *Peer) statusFlags() (ret string) {
215 ret += string([]byte{b})
217 if cn.requestState.Interested {
224 ret += cn.connectionFlags()
226 if cn.peerInterested {
235 func (cn *Peer) downloadRate() float64 {
236 num := cn._stats.BytesReadUsefulData.Int64()
240 return float64(num) / cn.totalExpectingTime().Seconds()
243 func (cn *Peer) DownloadRate() float64 {
245 defer cn.locker().RUnlock()
247 return cn.downloadRate()
250 func (cn *Peer) iterContiguousPieceRequests(f func(piece pieceIndex, count int)) {
251 var last Option[pieceIndex]
253 next := func(item Option[pieceIndex]) {
264 cn.requestState.Requests.Iterate(func(requestIndex request_strategy.RequestIndex) bool {
265 next(Some(cn.t.pieceIndexOfRequestIndex(requestIndex)))
268 next(None[pieceIndex]())
271 func (cn *Peer) writeStatus(w io.Writer, t *Torrent) {
272 // \t isn't preserved in <pre> blocks?
273 if cn.closed.IsSet() {
274 fmt.Fprint(w, "CLOSED: ")
276 fmt.Fprintln(w, strings.Join(cn.peerImplStatusLines(), "\n"))
277 prio, err := cn.peerPriority()
278 prioStr := fmt.Sprintf("%08x", prio)
280 prioStr += ": " + err.Error()
282 fmt.Fprintf(w, "bep40-prio: %v\n", prioStr)
283 fmt.Fprintf(w, "last msg: %s, connected: %s, last helpful: %s, itime: %s, etime: %s\n",
284 eventAgeString(cn.lastMessageReceived),
285 eventAgeString(cn.completedHandshake),
286 eventAgeString(cn.lastHelpful()),
288 cn.totalExpectingTime(),
291 "%s completed, %d pieces touched, good chunks: %v/%v:%v reqq: %d+%v/(%d/%d):%d/%d, flags: %s, dr: %.1f KiB/s\n",
292 cn.completedString(),
293 len(cn.peerTouchedPieces),
294 &cn._stats.ChunksReadUseful,
295 &cn._stats.ChunksRead,
296 &cn._stats.ChunksWritten,
297 cn.requestState.Requests.GetCardinality(),
298 cn.requestState.Cancelled.GetCardinality(),
299 cn.nominalMaxRequests(),
301 len(cn.peerRequests),
304 cn.downloadRate()/(1<<10),
306 fmt.Fprintf(w, "requested pieces:")
307 cn.iterContiguousPieceRequests(func(piece pieceIndex, count int) {
308 fmt.Fprintf(w, " %v(%v)", piece, count)
313 func (p *Peer) close() {
317 if p.updateRequestsTimer != nil {
318 p.updateRequestsTimer.Stop()
322 p.t.decPeerPieceAvailability(p)
324 for _, f := range p.callbacks.PeerClosed {
329 // Peer definitely has a piece, for purposes of requesting. So it's not sufficient that we think
330 // they do (known=true).
331 func (cn *Peer) peerHasPiece(piece pieceIndex) bool {
332 if all, known := cn.peerHasAllPieces(); all && known {
335 return cn.peerPieces().ContainsInt(piece)
338 // 64KiB, but temporarily less to work around an issue with WebRTC. TODO: Update when
339 // https://github.com/pion/datachannel/issues/59 is fixed.
341 writeBufferHighWaterLen = 1 << 15
342 writeBufferLowWaterLen = writeBufferHighWaterLen / 2
346 interestedMsgLen = len(pp.Message{Type: pp.Interested}.MustMarshalBinary())
347 requestMsgLen = len(pp.Message{Type: pp.Request}.MustMarshalBinary())
348 // This is the maximum request count that could fit in the write buffer if it's at or below the
349 // low water mark when we run maybeUpdateActualRequestState.
350 maxLocalToRemoteRequests = (writeBufferHighWaterLen - writeBufferLowWaterLen - interestedMsgLen) / requestMsgLen
353 // The actual value to use as the maximum outbound requests.
354 func (cn *Peer) nominalMaxRequests() maxRequests {
355 return maxInt(1, minInt(cn.PeerMaxRequests, cn.peakRequests*2, maxLocalToRemoteRequests))
358 func (cn *Peer) totalExpectingTime() (ret time.Duration) {
359 ret = cn.cumulativeExpectedToReceiveChunks
360 if !cn.lastStartedExpectingToReceiveChunks.IsZero() {
361 ret += time.Since(cn.lastStartedExpectingToReceiveChunks)
366 func (cn *Peer) setInterested(interested bool) bool {
367 if cn.requestState.Interested == interested {
370 cn.requestState.Interested = interested
372 cn.lastBecameInterested = time.Now()
373 } else if !cn.lastBecameInterested.IsZero() {
374 cn.priorInterest += time.Since(cn.lastBecameInterested)
376 cn.updateExpectingChunks()
377 // log.Printf("%p: setting interest: %v", cn, interested)
378 return cn.writeInterested(interested)
381 // The function takes a message to be sent, and returns true if more messages
383 type messageWriter func(pp.Message) bool
385 // This function seems to only used by Peer.request. It's all logic checks, so maybe we can no-op it
386 // when we want to go fast.
387 func (cn *Peer) shouldRequest(r RequestIndex) error {
388 err := cn.t.checkValidReceiveChunk(cn.t.requestIndexToRequest(r))
392 pi := cn.t.pieceIndexOfRequestIndex(r)
393 if cn.requestState.Cancelled.Contains(r) {
394 return errors.New("request is cancelled and waiting acknowledgement")
396 if !cn.peerHasPiece(pi) {
397 return errors.New("requesting piece peer doesn't have")
399 if !cn.t.peerIsActive(cn) {
400 panic("requesting but not in active conns")
402 if cn.closed.IsSet() {
403 panic("requesting when connection is closed")
405 if cn.t.hashingPiece(pi) {
406 panic("piece is being hashed")
408 if cn.t.pieceQueuedForHash(pi) {
409 panic("piece is queued for hash")
411 if cn.peerChoking && !cn.peerAllowedFast.Contains(pi) {
412 // This could occur if we made a request with the fast extension, and then got choked and
413 // haven't had the request rejected yet.
414 if !cn.requestState.Requests.Contains(r) {
415 panic("peer choking and piece not allowed fast")
421 func (cn *Peer) mustRequest(r RequestIndex) bool {
422 more, err := cn.request(r)
429 func (cn *Peer) request(r RequestIndex) (more bool, err error) {
430 if err := cn.shouldRequest(r); err != nil {
433 if cn.requestState.Requests.Contains(r) {
436 if maxRequests(cn.requestState.Requests.GetCardinality()) >= cn.nominalMaxRequests() {
437 return true, errors.New("too many outstanding requests")
439 cn.requestState.Requests.Add(r)
440 if cn.validReceiveChunks == nil {
441 cn.validReceiveChunks = make(map[RequestIndex]int)
443 cn.validReceiveChunks[r]++
444 cn.t.requestState[r] = requestState{
448 cn.updateExpectingChunks()
449 ppReq := cn.t.requestIndexToRequest(r)
450 for _, f := range cn.callbacks.SentRequest {
451 f(PeerRequestEvent{cn, ppReq})
453 return cn.peerImpl._request(ppReq), nil
456 func (me *Peer) cancel(r RequestIndex) {
457 if !me.deleteRequest(r) {
458 panic("request not existing should have been guarded")
461 if !me.requestState.Cancelled.CheckedAdd(r) {
462 panic("request already cancelled")
466 if me.isLowOnRequests() {
467 me.updateRequests("Peer.cancel")
471 // Sets a reason to update requests, and if there wasn't already one, handle it.
472 func (cn *Peer) updateRequests(reason string) {
473 if cn.needRequestUpdate != "" {
476 if reason != peerUpdateRequestsTimerReason && !cn.isLowOnRequests() {
479 cn.needRequestUpdate = reason
480 cn.handleUpdateRequests()
483 // Emits the indices in the Bitmaps bms in order, never repeating any index.
484 // skip is mutated during execution, and its initial values will never be
486 func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
487 return func(cb iter.Callback) {
488 for _, bm := range bms {
490 func(_i interface{}) bool {
492 if skip.Contains(bitmap.BitIndex(i)) {
495 skip.Add(bitmap.BitIndex(i))
506 func (cn *Peer) peerPiecesChanged() {
507 cn.t.maybeDropMutuallyCompletePeer(cn)
510 // After handshake, we know what Torrent and Client stats to include for a
512 func (cn *Peer) postHandshakeStats(f func(*ConnStats)) {
518 // All ConnStats that include this connection. Some objects are not known
519 // until the handshake is complete, after which it's expected to reconcile the
521 func (cn *Peer) allStats(f func(*ConnStats)) {
523 if cn.reconciledHandshakeStats {
524 cn.postHandshakeStats(f)
528 func (cn *Peer) readBytes(n int64) {
529 cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesRead }))
532 // Returns whether the connection could be useful to us. We're seeding and
533 // they want data, we don't have metainfo and they can provide it, etc.
534 func (c *Peer) useful() bool {
536 if c.closed.IsSet() {
540 return c.supportsExtension("ut_metadata")
542 if t.seeding() && c.peerInterested {
545 if c.peerHasWantedPieces() {
551 func (c *Peer) lastHelpful() (ret time.Time) {
552 ret = c.lastUsefulChunkReceived
553 if c.t.seeding() && c.lastChunkSent.After(ret) {
554 ret = c.lastChunkSent
559 // Returns whether any part of the chunk would lie outside a piece of the given length.
560 func chunkOverflowsPiece(cs ChunkSpec, pieceLength pp.Integer) bool {
564 case cs.Begin+cs.Length > pieceLength:
565 // Check for integer overflow
566 case cs.Begin > pp.IntegerMax-cs.Length:
571 func runSafeExtraneous(f func()) {
579 // Returns true if it was valid to reject the request.
580 func (c *Peer) remoteRejectedRequest(r RequestIndex) bool {
581 if c.deleteRequest(r) {
583 } else if !c.requestState.Cancelled.CheckedRemove(r) {
586 if c.isLowOnRequests() {
587 c.updateRequests("Peer.remoteRejectedRequest")
589 c.decExpectedChunkReceive(r)
593 func (c *Peer) decExpectedChunkReceive(r RequestIndex) {
594 count := c.validReceiveChunks[r]
596 delete(c.validReceiveChunks, r)
597 } else if count > 1 {
598 c.validReceiveChunks[r] = count - 1
604 func (c *Peer) doChunkReadStats(size int64) {
605 c.allStats(func(cs *ConnStats) { cs.receivedChunk(size) })
608 // Handle a received chunk from a peer.
609 func (c *Peer) receiveChunk(msg *pp.Message) error {
610 chunksReceived.Add("total", 1)
612 ppReq := newRequestFromMessage(msg)
614 err := t.checkValidReceiveChunk(ppReq)
616 err = log.WithLevel(log.Warning, err)
619 req := c.t.requestIndexFromRequest(ppReq)
621 if c.bannableAddr.Ok {
622 t.smartBanCache.RecordBlock(c.bannableAddr.Value, req, msg.Piece)
626 chunksReceived.Add("while choked", 1)
629 if c.validReceiveChunks[req] <= 0 {
630 chunksReceived.Add("unexpected", 1)
631 return errors.New("received unexpected chunk")
633 c.decExpectedChunkReceive(req)
635 if c.peerChoking && c.peerAllowedFast.Contains(pieceIndex(ppReq.Index)) {
636 chunksReceived.Add("due to allowed fast", 1)
639 // The request needs to be deleted immediately to prevent cancels occurring asynchronously when
640 // have actually already received the piece, while we have the Client unlocked to write the data
644 if c.requestState.Requests.Contains(req) {
645 for _, f := range c.callbacks.ReceivedRequested {
646 f(PeerMessageEvent{c, msg})
649 // Request has been satisfied.
650 if c.deleteRequest(req) || c.requestState.Cancelled.CheckedRemove(req) {
653 c._chunksReceivedWhileExpecting++
655 if c.isLowOnRequests() {
656 c.updateRequests("Peer.receiveChunk deleted request")
659 chunksReceived.Add("unintended", 1)
665 // Do we actually want this chunk?
666 if t.haveChunk(ppReq) {
667 // panic(fmt.Sprintf("%+v", ppReq))
668 chunksReceived.Add("redundant", 1)
669 c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadWasted }))
673 piece := &t.pieces[ppReq.Index]
675 c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadUseful }))
676 c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulData }))
678 c.piecesReceivedSinceLastRequestUpdate++
679 c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulIntendedData }))
681 for _, f := range c.t.cl.config.Callbacks.ReceivedUsefulData {
682 f(ReceivedUsefulDataEvent{c, msg})
684 c.lastUsefulChunkReceived = time.Now()
686 // Need to record that it hasn't been written yet, before we attempt to do
688 piece.incrementPendingWrites()
689 // Record that we have the chunk, so we aren't trying to download it while
690 // waiting for it to be written to storage.
691 piece.unpendChunkIndex(chunkIndexFromChunkSpec(ppReq.ChunkSpec, t.chunkSize))
693 // Cancel pending requests for this chunk from *other* peers.
694 if p := t.requestingPeer(req); p != nil {
696 panic("should not be pending request from conn that just received it")
704 concurrentChunkWrites.Add(1)
705 defer concurrentChunkWrites.Add(-1)
706 // Write the chunk out. Note that the upper bound on chunk writing concurrency will be the
707 // number of connections. We write inline with receiving the chunk (with this lock dance),
708 // because we want to handle errors synchronously and I haven't thought of a nice way to
709 // defer any concurrency to the storage and have that notify the client of errors. TODO: Do
711 return t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
714 piece.decrementPendingWrites()
717 c.logger.WithDefaultLevel(log.Error).Printf("writing received chunk %v: %v", req, err)
719 // Necessary to pass TestReceiveChunkStorageFailureSeederFastExtensionDisabled. I think a
720 // request update runs while we're writing the chunk that just failed. Then we never do a
721 // fresh update after pending the failed request.
722 c.updateRequests("Peer.receiveChunk error writing chunk")
723 t.onWriteChunkErr(err)
727 c.onDirtiedPiece(pieceIndex(ppReq.Index))
729 // We need to ensure the piece is only queued once, so only the last chunk writer gets this job.
730 if t.pieceAllDirty(pieceIndex(ppReq.Index)) && piece.pendingWrites == 0 {
731 t.queuePieceCheck(pieceIndex(ppReq.Index))
732 // We don't pend all chunks here anymore because we don't want code dependent on the dirty
733 // chunk status (such as the haveChunk call above) to have to check all the various other
734 // piece states like queued for hash, hashing etc. This does mean that we need to be sure
735 // that chunk pieces are pended at an appropriate time later however.
739 // We do this because we've written a chunk, and may change PieceState.Partial.
740 t.publishPieceChange(pieceIndex(ppReq.Index))
745 func (c *Peer) onDirtiedPiece(piece pieceIndex) {
746 if c.peerTouchedPieces == nil {
747 c.peerTouchedPieces = make(map[pieceIndex]struct{})
749 c.peerTouchedPieces[piece] = struct{}{}
750 ds := &c.t.pieces[piece].dirtiers
752 *ds = make(map[*Peer]struct{})
754 (*ds)[c] = struct{}{}
757 func (cn *Peer) netGoodPiecesDirtied() int64 {
758 return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64()
761 func (c *Peer) peerHasWantedPieces() bool {
762 if all, _ := c.peerHasAllPieces(); all {
763 return !c.t.haveAllPieces() && !c.t._pendingPieces.IsEmpty()
766 return !c.peerPieces().IsEmpty()
768 return c.peerPieces().Intersects(&c.t._pendingPieces)
771 // Returns true if an outstanding request is removed. Cancelled requests should be handled
773 func (c *Peer) deleteRequest(r RequestIndex) bool {
774 if !c.requestState.Requests.CheckedRemove(r) {
777 for _, f := range c.callbacks.DeletedRequest {
778 f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)})
780 c.updateExpectingChunks()
781 if c.t.requestingPeer(r) != c {
782 panic("only one peer should have a given request at a time")
784 delete(c.t.requestState, r)
785 // c.t.iterPeers(func(p *Peer) {
786 // if p.isLowOnRequests() {
787 // p.updateRequests("Peer.deleteRequest")
793 func (c *Peer) deleteAllRequests(reason string) {
794 if c.requestState.Requests.IsEmpty() {
797 c.requestState.Requests.IterateSnapshot(func(x RequestIndex) bool {
798 if !c.deleteRequest(x) {
799 panic("request should exist")
804 c.t.iterPeers(func(p *Peer) {
805 if p.isLowOnRequests() {
806 p.updateRequests(reason)
812 func (c *Peer) assertNoRequests() {
813 if !c.requestState.Requests.IsEmpty() {
814 panic(c.requestState.Requests.GetCardinality())
818 func (c *Peer) cancelAllRequests() {
819 c.requestState.Requests.IterateSnapshot(func(x RequestIndex) bool {
827 func (c *Peer) peerPriority() (peerPriority, error) {
828 return bep40Priority(c.remoteIpPort(), c.localPublicAddr)
831 func (c *Peer) remoteIp() net.IP {
832 host, _, _ := net.SplitHostPort(c.RemoteAddr.String())
833 return net.ParseIP(host)
836 func (c *Peer) remoteIpPort() IpPort {
837 ipa, _ := tryIpPortFromNetAddr(c.RemoteAddr)
838 return IpPort{ipa.IP, uint16(ipa.Port)}
841 func (c *Peer) trust() connectionTrust {
842 return connectionTrust{c.trusted, c.netGoodPiecesDirtied()}
845 type connectionTrust struct {
847 NetGoodPiecesDirted int64
850 func (l connectionTrust) Less(r connectionTrust) bool {
851 return multiless.New().Bool(l.Implicit, r.Implicit).Int64(l.NetGoodPiecesDirted, r.NetGoodPiecesDirted).Less()
854 // Returns a new Bitmap that includes bits for all pieces the peer could have based on their claims.
855 func (cn *Peer) newPeerPieces() *roaring.Bitmap {
856 // TODO: Can we use copy on write?
857 ret := cn.peerPieces().Clone()
858 if all, _ := cn.peerHasAllPieces(); all {
860 ret.AddRange(0, bitmap.BitRange(cn.t.numPieces()))
862 ret.AddRange(0, bitmap.ToEnd)
868 func (cn *Peer) stats() *ConnStats {
872 func (p *Peer) TryAsPeerConn() (*PeerConn, bool) {
873 pc, ok := p.peerImpl.(*PeerConn)
877 func (p *Peer) uncancelledRequests() uint64 {
878 return p.requestState.Requests.GetCardinality()
881 type peerLocalPublicAddr = IpPort
883 func (p *Peer) isLowOnRequests() bool {
884 return p.requestState.Requests.IsEmpty() && p.requestState.Cancelled.IsEmpty()
887 func (p *Peer) decPeakRequests() {
888 // // This can occur when peak requests are altered by the update request timer to be lower than
889 // // the actual number of outstanding requests. Let's let it go negative and see what happens. I
890 // // wonder what happens if maxRequests is not signed.
891 // if p.peakRequests < 1 {
892 // panic(p.peakRequests)