Skip to content

Commit

Permalink
fix: split monitoring from add current peers
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Thompson <jeff@thefirst.org>
  • Loading branch information
jefft0 committed Jul 7, 2023
1 parent 15e9deb commit e4c5794
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 3 additions & 6 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ func (p *PubSubNotif) startMonitoring() error {
return fmt.Errorf("unable to subscribe to EventBus: %w", err)
}

// add current peers
p.addPeers(p.host.Network().Peers()...)

go func() {
defer sub.Close()

Expand All @@ -38,7 +35,7 @@ func (p *PubSubNotif) startMonitoring() error {
case event.EvtPeerConnectednessChanged:
// send record to connected peer only
if evt.Connectedness == network.Connected {
go p.addPeers(evt.Peer)
go p.AddPeers(evt.Peer)
}
case event.EvtPeerProtocolsUpdated:
supportedProtocols := p.rt.Protocols()
Expand All @@ -47,7 +44,7 @@ func (p *PubSubNotif) startMonitoring() error {
for _, addedProtocol := range evt.Added {
for _, wantedProtocol := range supportedProtocols {
if wantedProtocol == addedProtocol {
go p.addPeers(evt.Peer)
go p.AddPeers(evt.Peer)
break protocol_loop
}
}
Expand All @@ -69,7 +66,7 @@ func (p *PubSubNotif) isTransient(pid peer.ID) bool {
return true
}

func (p *PubSubNotif) addPeers(peers ...peer.ID) {
func (p *PubSubNotif) AddPeers(peers ...peer.ID) {
p.newPeersPrioLk.RLock()
p.newPeersMx.Lock()

Expand Down
11 changes: 8 additions & 3 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,18 @@ func NewPubSub(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option
}
}

// start monitoring for new peers
notify := (*PubSubNotif)(ps)
if err := notify.startMonitoring(); err != nil {
return nil, fmt.Errorf("unable to start pubsub monitorin: %w", err)
}

ps.val.Start(ps)

go ps.processLoop(ctx)

if err := (*PubSubNotif)(ps).startMonitoring(); err != nil {
return nil, fmt.Errorf("unable to start pubsub monitoring: %w", err)
}
// add current peers to notify system
notify.AddPeers(h.Network().Peers()...)

return ps, nil
}
Expand Down

0 comments on commit e4c5794

Please sign in to comment.