-
Notifications
You must be signed in to change notification settings - Fork 246
/
topic_register.go
57 lines (50 loc) · 1.23 KB
/
topic_register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package peers
import (
"sync"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/status-im/status-go/discovery"
)
// Register manages register topic queries
type Register struct {
discovery discovery.Discovery
topics []discv5.Topic
wg sync.WaitGroup
quit chan struct{}
}
// NewRegister creates instance of topic register
func NewRegister(discovery discovery.Discovery, topics ...discv5.Topic) *Register {
return &Register{discovery: discovery, topics: topics}
}
// Start topic register query for every topic
func (r *Register) Start() error {
if !r.discovery.Running() {
return ErrDiscv5NotRunning
}
r.quit = make(chan struct{})
for _, topic := range r.topics {
r.wg.Add(1)
go func(t discv5.Topic) {
log.Debug("v5 register topic", "topic", t)
if err := r.discovery.Register(string(t), r.quit); err != nil {
log.Error("error registering topic", "topic", t, "error", err)
}
r.wg.Done()
}(topic)
}
return nil
}
// Stop all register topic queries and waits for them to exit
func (r *Register) Stop() {
if r.quit == nil {
return
}
select {
case <-r.quit:
return
default:
close(r.quit)
}
log.Debug("waiting for register queries to exit")
r.wg.Wait()
}