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 124756a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 4 additions & 8 deletions share/p2p/peers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"unsafe"

logging "github.com/ipfs/go-log/v2"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/event"
Expand All @@ -17,6 +12,9 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"sync"
"sync/atomic"
"time"

libhead "github.com/celestiaorg/go-header"

Expand Down Expand Up @@ -518,9 +516,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.RLock()
defer p.m.RUnlock()
// swap the pool with an empty one
*p = *newPool(time.Second)
}

0 comments on commit 124756a

Please sign in to comment.