Skip to content

Commit af8e47d

Browse files
authored
Merge pull request #534 from shutter-network/fix/floodsub-broadcast
feat: added subscription for floodsub topic
2 parents 29ef965 + a3f8561 commit af8e47d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

rolling-shutter/p2p/floodsubpeerdiscovery/gossippeerdiscovery.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ const defaultTopic = "_peer-discovery._p2p._pubsub"
1717

1818
type FloodsubPeerDiscovery struct {
1919
PeerDiscoveryComponents
20-
Interval int
21-
Topics []*pubsub.Topic
20+
Interval int
21+
Topics []*pubsub.Topic
22+
Subscription []*pubsub.Subscription
2223
}
2324

2425
type PeerDiscoveryComponents struct {
@@ -40,13 +41,25 @@ func (pd *FloodsubPeerDiscovery) Init(config PeerDiscoveryComponents, interval i
4041
return fmt.Errorf("failed to join topic | err %w", err)
4142
}
4243
pd.Topics = append(pd.Topics, topic)
44+
45+
subs, err := topic.Subscribe()
46+
if err != nil {
47+
return fmt.Errorf("failed to subscribe topic | err %w", err)
48+
}
49+
pd.Subscription = append(pd.Subscription, subs)
4350
}
4451
} else {
4552
topic, err := pd.Pubsub.Join(defaultTopic)
4653
if err != nil {
4754
return fmt.Errorf("failed to join topic | err %w", err)
4855
}
4956
pd.Topics = append(pd.Topics, topic)
57+
58+
subs, err := topic.Subscribe()
59+
if err != nil {
60+
return fmt.Errorf("failed to subscribe topic | err %w", err)
61+
}
62+
pd.Subscription = append(pd.Subscription, subs)
5063
}
5164
return nil
5265
}
@@ -63,6 +76,10 @@ func (pd *FloodsubPeerDiscovery) Start(ctx context.Context) error {
6376
return err
6477
}
6578
case <-ctx.Done():
79+
for _, subs := range pd.Subscription {
80+
subs.Cancel()
81+
}
82+
6683
for _, topic := range pd.Topics {
6784
if err := topic.Close(); err != nil {
6885
return fmt.Errorf("error in closing topic | %w", err)

0 commit comments

Comments
 (0)