Skip to content

Commit f53dc7c

Browse files
committed
bitswap: clean up ledgers when disconnecting
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 8e2aed3 commit f53dc7c

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

exchange/bitswap/bitswap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
414414
// Connected/Disconnected warns bitswap about peer connections
415415
func (bs *Bitswap) PeerConnected(p peer.ID) {
416416
bs.wm.Connected(p)
417+
bs.engine.PeerConnected(p)
417418
}
418419

419420
// Connected/Disconnected warns bitswap about peer connections

exchange/bitswap/decision/engine.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,32 @@ func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
298298
return nil
299299
}
300300

301+
func (e *Engine) PeerConnected(p peer.ID) {
302+
e.lock.Lock()
303+
l, ok := e.ledgerMap[p]
304+
if !ok {
305+
l = newLedger(p)
306+
e.ledgerMap[p] = l
307+
}
308+
l.lk.Lock()
309+
l.ref++
310+
l.lk.Unlock()
311+
e.lock.Unlock()
312+
}
313+
301314
func (e *Engine) PeerDisconnected(p peer.ID) {
302-
// TODO: release ledger
315+
e.lock.Lock()
316+
defer e.lock.Unlock()
317+
l, ok := e.ledgerMap[p]
318+
if !ok {
319+
return
320+
}
321+
l.lk.Lock()
322+
l.ref--
323+
if l.ref <= 0 {
324+
delete(e.ledgerMap, p)
325+
}
326+
l.lk.Unlock()
303327
}
304328

305329
func (e *Engine) numBytesSentTo(p peer.ID) uint64 {

exchange/bitswap/decision/ledger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type ledger struct {
4343
// to a given peer
4444
sentToPeer map[string]time.Time
4545

46+
// ref is the reference count for this ledger, its used to ensure we
47+
// don't drop the reference to this ledger in multi-connection scenarios
48+
ref int
49+
4650
lk sync.Mutex
4751
}
4852

0 commit comments

Comments
 (0)