Skip to content

Commit

Permalink
perf: avoid lots of communication by signaling once at end of method
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
  • Loading branch information
Brian Tiger Chow committed Dec 17, 2014
1 parent eddc51a commit e95730a
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions exchange/bitswap/strategy/ledgermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func (lm *LedgerManager) BlockIsWantedByPeer(k u.Key, p peer.Peer) bool {
// MessageReceived performs book-keeping. Returns error if passed invalid
// arguments.
func (lm *LedgerManager) MessageReceived(p peer.Peer, m bsmsg.BitSwapMessage) error {
newWorkExists := false
defer func() {
if newWorkExists {
// Signal task generation to restart (if stopped!)
select {
case lm.workSignal <- struct{}{}:
default:
}
}
}()
lm.lock.Lock()
defer lm.lock.Unlock()

Expand All @@ -117,13 +127,8 @@ func (lm *LedgerManager) MessageReceived(p peer.Peer, m bsmsg.BitSwapMessage) er
lm.tasklist.Cancel(e.Key, p)
} else {
l.Wants(e.Key, e.Priority)
newWorkExists = true
lm.tasklist.Push(e.Key, e.Priority, p)

// Signal task generation to restart (if stopped!)
select {
case lm.workSignal <- struct{}{}:
default:
}
}
}

Expand All @@ -132,14 +137,8 @@ func (lm *LedgerManager) MessageReceived(p peer.Peer, m bsmsg.BitSwapMessage) er
l.ReceivedBytes(len(block.Data))
for _, l := range lm.ledgerMap {
if l.WantListContains(block.Key()) {
newWorkExists = true
lm.tasklist.Push(block.Key(), 1, l.Partner)

// Signal task generation to restart (if stopped!)
select {
case lm.workSignal <- struct{}{}:
default:
}

}
}
}
Expand Down

0 comments on commit e95730a

Please sign in to comment.