Skip to content

Commit

Permalink
Try to do smart ban block hashing without client lock
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Feb 18, 2024
1 parent fe51e37 commit 106de70
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"strings"
"sync"
"time"

"github.com/RoaringBitmap/roaring"
Expand Down Expand Up @@ -600,9 +601,13 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
}
req := c.t.requestIndexFromRequest(ppReq)

if c.bannableAddr.Ok {
t.smartBanCache.RecordBlock(c.bannableAddr.Value, req, msg.Piece)
}
recordBlockForSmartBan := sync.OnceFunc(func() {
c.recordBlockForSmartBan(req, msg.Piece)
})
// This needs to occur before we return, but we try to do it when the client is unlocked. It
// can't be done before checking if chunks are valid because they won't be deallocated by piece
// hashing if they're out of bounds.
defer recordBlockForSmartBan()

if c.peerChoking {
chunksReceived.Add("while choked", 1)
Expand Down Expand Up @@ -683,6 +688,8 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
err = func() error {
cl.unlock()
defer cl.lock()
// Opportunistically do this here while we aren't holding the client lock.
recordBlockForSmartBan()
concurrentChunkWrites.Add(1)
defer concurrentChunkWrites.Add(-1)
// Write the chunk out. Note that the upper bound on chunk writing concurrency will be the
Expand Down Expand Up @@ -875,3 +882,9 @@ func (p *Peer) decPeakRequests() {
// }
p.peakRequests--
}

func (p *Peer) recordBlockForSmartBan(req RequestIndex, blockData []byte) {
if p.bannableAddr.Ok {
p.t.smartBanCache.RecordBlock(p.bannableAddr.Value, req, blockData)
}
}

0 comments on commit 106de70

Please sign in to comment.