Skip to content

Commit c8a8300

Browse files
committed
fix(p2p): bootstrap context
1 parent dca6313 commit c8a8300

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

rolling-shutter/p2p/bootstrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func bootstrap(
7373

7474
f := func(c context.Context) (bool, error) {
7575
if err := connectBootstrapNodes(c, h, config.BootstrapPeers); err != nil {
76-
return true, err
76+
return false, err
7777
}
78-
return false, nil
78+
return true, nil
7979
}
8080

8181
if config.IsBootstrapNode {

rolling-shutter/p2p/messaging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (m *P2PMessaging) Start(
217217
runner service.Runner,
218218
) error { //nolint:unparam
219219
runner.Go(func() error {
220-
return m.P2P.Run(ctx, m.topics(), m.validatorRegistry)
220+
return m.P2P.Run(ctx, runner, m.topics(), m.validatorRegistry)
221221
})
222222
if m.hasHandler() {
223223
runner.Go(func() error {

rolling-shutter/p2p/p2p.go

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ import (
1111
"github.com/libp2p/go-libp2p/core/host"
1212
"github.com/libp2p/go-libp2p/core/peer"
1313
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
14-
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
1514
rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
1615
"github.com/multiformats/go-multiaddr"
1716
"github.com/pkg/errors"
1817
"github.com/rs/zerolog/log"
19-
"golang.org/x/sync/errgroup"
2018

2119
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/address"
2220
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/env"
2321
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/keys"
22+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
2423
)
2524

2625
var DefaultBootstrapPeers []*address.P2PAddress
@@ -82,52 +81,43 @@ func NewP2PNode(config p2pNodeConfig) *P2PNode {
8281

8382
func (p *P2PNode) Run(
8483
ctx context.Context,
84+
runner service.Runner,
8585
topicNames []string,
8686
topicValidators ValidatorRegistry,
8787
) error {
88-
defer func() {
89-
close(p.GossipMessages)
90-
}()
88+
p.mux.Lock()
89+
defer p.mux.Unlock()
9190

92-
errorgroup, errorgroupctx := errgroup.WithContext(ctx)
93-
errorgroup.Go(func() error {
94-
p.mux.Lock()
95-
defer p.mux.Unlock()
96-
if err := p.init(ctx); err != nil {
97-
return err
98-
}
91+
runner.Defer(func() {
92+
close(p.GossipMessages)
93+
})
9994

100-
for topicName, validator := range topicValidators {
101-
if err := p.pubSub.RegisterTopicValidator(topicName, validator); err != nil {
102-
return err
103-
}
104-
}
95+
if err := p.init(ctx); err != nil {
96+
return err
97+
}
10598

106-
if err := p.joinTopics(topicNames); err != nil {
99+
for topicName, validator := range topicValidators {
100+
if err := p.pubSub.RegisterTopicValidator(topicName, validator); err != nil {
107101
return err
108102
}
103+
}
109104

110-
// listen to gossip on all topics
111-
for _, room := range p.gossipRooms {
112-
room := room
113-
errorgroup.Go(func() error {
114-
return room.readLoop(errorgroupctx, p.GossipMessages)
115-
})
116-
}
117-
118-
err := bootstrap(ctx, p.host, p.config, p.dht)
119-
if err != nil {
120-
return err
121-
}
105+
if err := p.joinTopics(topicNames); err != nil {
106+
return err
107+
}
122108

123-
// block the function until the context is canceled
124-
errorgroup.Go(func() error {
125-
<-errorgroupctx.Done()
126-
return ctx.Err()
109+
err := bootstrap(ctx, p.host, p.config, p.dht)
110+
if err != nil {
111+
return err
112+
}
113+
// listen to gossip on all topics
114+
for _, room := range p.gossipRooms {
115+
room := room
116+
runner.Go(func() error {
117+
return room.readLoop(ctx, p.GossipMessages)
127118
})
128-
return nil
129-
})
130-
return errorgroup.Wait()
119+
}
120+
return nil
131121
}
132122

133123
func (p *P2PNode) Publish(ctx context.Context, topic string, message []byte) error {

0 commit comments

Comments
 (0)