mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
bitswap: clean up ledgers when disconnecting
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
This commit is contained in:
@ -414,6 +414,7 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
|
|||||||
// Connected/Disconnected warns bitswap about peer connections
|
// Connected/Disconnected warns bitswap about peer connections
|
||||||
func (bs *Bitswap) PeerConnected(p peer.ID) {
|
func (bs *Bitswap) PeerConnected(p peer.ID) {
|
||||||
bs.wm.Connected(p)
|
bs.wm.Connected(p)
|
||||||
|
bs.engine.PeerConnected(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connected/Disconnected warns bitswap about peer connections
|
// Connected/Disconnected warns bitswap about peer connections
|
||||||
|
@ -298,8 +298,32 @@ func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Engine) PeerConnected(p peer.ID) {
|
||||||
|
e.lock.Lock()
|
||||||
|
l, ok := e.ledgerMap[p]
|
||||||
|
if !ok {
|
||||||
|
l = newLedger(p)
|
||||||
|
e.ledgerMap[p] = l
|
||||||
|
}
|
||||||
|
l.lk.Lock()
|
||||||
|
l.ref++
|
||||||
|
l.lk.Unlock()
|
||||||
|
e.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Engine) PeerDisconnected(p peer.ID) {
|
func (e *Engine) PeerDisconnected(p peer.ID) {
|
||||||
// TODO: release ledger
|
e.lock.Lock()
|
||||||
|
defer e.lock.Unlock()
|
||||||
|
l, ok := e.ledgerMap[p]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.lk.Lock()
|
||||||
|
l.ref--
|
||||||
|
if l.ref <= 0 {
|
||||||
|
delete(e.ledgerMap, p)
|
||||||
|
}
|
||||||
|
l.lk.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) numBytesSentTo(p peer.ID) uint64 {
|
func (e *Engine) numBytesSentTo(p peer.ID) uint64 {
|
||||||
|
@ -43,6 +43,10 @@ type ledger struct {
|
|||||||
// to a given peer
|
// to a given peer
|
||||||
sentToPeer map[string]time.Time
|
sentToPeer map[string]time.Time
|
||||||
|
|
||||||
|
// ref is the reference count for this ledger, its used to ensure we
|
||||||
|
// don't drop the reference to this ledger in multi-connection scenarios
|
||||||
|
ref int
|
||||||
|
|
||||||
lk sync.Mutex
|
lk sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user