Skip to content

Commit

Permalink
identify: simplify triggering of pushes
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Feb 7, 2023
1 parent 7fae446 commit 86b20f8
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ type idService struct {
snapshot *identifySnapshot
}

triggerPush chan struct{}
// pushSemaphore limits the push concurrency
pushSemaphore chan struct{}
}
Expand Down Expand Up @@ -177,7 +176,6 @@ func NewIDService(h host.Host, opts ...Option) (*idService, error) {
ctxCancel: cancel,
conns: make(map[network.Conn]entry),
disableSignedPeerRecord: cfg.disableSignedPeerRecord,
triggerPush: make(chan struct{}, 1),
pushSemaphore: make(chan struct{}, 1),
}

Expand Down Expand Up @@ -227,14 +225,9 @@ func (ids *idService) loop(ctx context.Context) {
}
defer sub.Close()

triggerPush := make(chan struct{}, 1) // semaphore to make sure we're only running a single push loop
for {
select {
case <-ids.triggerPush:
ids.refCount.Add(1)
go func() {
defer ids.refCount.Done()
ids.sendPushes(ctx)
}()
case e, more := <-sub.Out():
if !more {
return
Expand All @@ -244,7 +237,13 @@ func (ids *idService) loop(ctx context.Context) {
case event.EvtLocalAddressesUpdated, event.EvtLocalProtocolsUpdated:
// trigger a push
select {
case ids.triggerPush <- struct{}{}:
case triggerPush <- struct{}{}:
ids.refCount.Add(1)
go func() {
defer ids.refCount.Done()
ids.sendPushes(ctx)
<-triggerPush
}()
default: // another push is already queued
}
}
Expand Down

0 comments on commit 86b20f8

Please sign in to comment.