Skip to content

Commit

Permalink
Merge pull request #155 from SiaFoundation/chris/peer-cond
Browse files Browse the repository at this point in the history
Use condition rather than polling to wait for closed peers
  • Loading branch information
n8maninger authored Jan 18, 2025
2 parents d8d7b02 + 9a05377 commit bab2bc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Use condition rather than polling to determine whether all peers are closed in 'Run'
19 changes: 11 additions & 8 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ type Syncer struct {

wg sync.WaitGroup

mu sync.Mutex
peers map[string]*Peer
strikes map[string]int
mu sync.Mutex
peerRemoved sync.Cond // broadcasts when peer is removed from 'peers'
peers map[string]*Peer
strikes map[string]int
}

func (s *Syncer) resync(p *Peer, reason string) {
Expand Down Expand Up @@ -272,6 +273,9 @@ func (s *Syncer) runPeer(p *Peer) error {
s.mu.Lock()
delete(s.peers, p.t.Addr)
s.mu.Unlock()

// notify goroutines of removed peer
s.peerRemoved.Broadcast()
}()

inflight := make(chan struct{}, s.config.MaxInflightRPCs)
Expand Down Expand Up @@ -664,12 +668,9 @@ func (s *Syncer) Run(ctx context.Context) error {
<-errChan

// wait for all peer goroutines to exit
// TODO: a cond would be nicer than polling here
s.mu.Lock()
for len(s.peers) != 0 {
s.mu.Unlock()
time.Sleep(100 * time.Millisecond)
s.mu.Lock()
s.peerRemoved.Wait()
}
s.mu.Unlock()

Expand Down Expand Up @@ -786,7 +787,7 @@ func New(l net.Listener, cm ChainManager, pm PeerStore, header gateway.Header, o
for _, opt := range opts {
opt(&config)
}
return &Syncer{
s := &Syncer{
l: l,
cm: cm,
pm: pm,
Expand All @@ -796,4 +797,6 @@ func New(l net.Listener, cm ChainManager, pm PeerStore, header gateway.Header, o
peers: make(map[string]*Peer),
strikes: make(map[string]int),
}
s.peerRemoved = sync.Cond{L: &s.mu}
return s
}

0 comments on commit bab2bc5

Please sign in to comment.