Skip to content

Commit

Permalink
independent lock
Browse files Browse the repository at this point in the history
  • Loading branch information
mh0lt committed Jul 1, 2024
1 parent bb2e4de commit 23dc0e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
31 changes: 17 additions & 14 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,30 +692,33 @@ func runSafeExtraneous(f func()) {
}

// Returns true if it was valid to reject the request.
func (c *Peer) remoteRejectedRequest(r RequestIndex, lock bool, lockTorrent bool) bool {
if lockTorrent {
func (c *Peer) remoteRejectedRequest(r RequestIndex) bool {
if !func() bool {
c.t.mu.Lock()
defer c.t.mu.Unlock()
}

if lock {
c.mu.Lock()
defer c.mu.Unlock()
}

if c.deleteRequest(r, false, false) {
c.decPeakRequests(false)
} else {
removed := c.requestState.Cancelled.CheckedRemove(r)
if c.deleteRequest(r, false, false) {
c.decPeakRequests(false)
} else {
removed := c.requestState.Cancelled.CheckedRemove(r)

if !removed {
return false
if !removed {
return false
}
}

return true
}() {
return false
}
if c.isLowOnRequests(false, false) {
c.updateRequests("Peer.remoteRejectedRequest", false, false)

if c.isLowOnRequests(true, true) {
c.updateRequests("Peer.remoteRejectedRequest", true, true)
}
c.decExpectedChunkReceive(r, false)
c.decExpectedChunkReceive(r, true)
return true
}

Expand Down
9 changes: 1 addition & 8 deletions peerconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,17 +1006,10 @@ func (c *PeerConn) mainReadLoop() (err error) {
err = c.peerSentHaveNone(true, true)
case pp.Reject:
req := newRequestFromMessage(&msg)
func() {
c.t.mu.Lock()
defer c.t.mu.Unlock()
c.mu.Lock()
defer c.mu.Unlock()

if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req, false), false, false) {
if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req, false)) {
err = fmt.Errorf("received invalid reject for request %v", req)
c.logger.Levelf(log.Debug, "%v", err)
}
}()
case pp.AllowedFast:
torrent.Add("allowed fasts received", 1)
log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).LogLevel(log.Debug, c.t.logger)
Expand Down
2 changes: 1 addition & 1 deletion webseed-peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Re
}
}

if !ws.peer.remoteRejectedRequest(ws.peer.t.requestIndexFromRequest(r, true), true, true) {
if !ws.peer.remoteRejectedRequest(ws.peer.t.requestIndexFromRequest(r, true)) {
err = fmt.Errorf(`received invalid reject "%w", for request %v`, err, r)
ws.peer.logger.Levelf(log.Debug, "%v", err)
}
Expand Down

0 comments on commit 23dc0e7

Please sign in to comment.