Skip to content

Commit

Permalink
use mutex for peer-manager pool cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Oct 6, 2023
1 parent 0b5fc0b commit 14b8922
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 1 addition & 4 deletions share/p2p/peers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

logging "github.com/ipfs/go-log/v2"
pubsub "github.com/libp2p/go-libp2p-pubsub"
Expand Down Expand Up @@ -518,9 +517,7 @@ func (m *Manager) markPoolAsSynced(datahash string) {
p := m.getOrCreatePool(datahash)
if p.isSynced.CompareAndSwap(false, true) {
p.isSynced.Store(true)
old := (*unsafe.Pointer)(unsafe.Pointer(&p.pool))
// release pointer to old pool to free up memory
atomic.StorePointer(old, unsafe.Pointer(newPool(time.Second)))
p.reset()
}
}

Expand Down
7 changes: 7 additions & 0 deletions share/p2p/peers/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,10 @@ func (p *pool) len() int {
defer p.m.RUnlock()
return p.activeCount
}

func (p *pool) reset() {
p.m.Lock()
defer p.m.Unlock()
// swap the pool with an empty one
*p = *newPool(time.Second)
}

0 comments on commit 14b8922

Please sign in to comment.