-
Notifications
You must be signed in to change notification settings - Fork 202
/
topicProcessors.go
68 lines (52 loc) · 1.5 KB
/
topicProcessors.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
58
59
60
61
62
63
64
65
66
67
68
package libp2p
import (
"fmt"
"sync"
"github.com/ElrondNetwork/elrond-go/p2p"
)
type topicProcessors struct {
processors map[string]p2p.MessageProcessor
mutProcessors sync.RWMutex
}
func newTopicProcessors() *topicProcessors {
return &topicProcessors{
processors: make(map[string]p2p.MessageProcessor),
}
}
func (tp *topicProcessors) addTopicProcessor(identifier string, processor p2p.MessageProcessor) error {
tp.mutProcessors.Lock()
defer tp.mutProcessors.Unlock()
_, alreadyExists := tp.processors[identifier]
if alreadyExists {
return fmt.Errorf("%w, in addTopicProcessor, identifier %s",
p2p.ErrMessageProcessorAlreadyDefined,
identifier,
)
}
tp.processors[identifier] = processor
return nil
}
func (tp *topicProcessors) removeTopicProcessor(identifier string) error {
tp.mutProcessors.Lock()
defer tp.mutProcessors.Unlock()
_, alreadyExists := tp.processors[identifier]
if !alreadyExists {
return fmt.Errorf("%w, in removeTopicProcessor, identifier %s",
p2p.ErrMessageProcessorDoesNotExists,
identifier,
)
}
delete(tp.processors, identifier)
return nil
}
func (tp *topicProcessors) getList() ([]string, []p2p.MessageProcessor) {
tp.mutProcessors.RLock()
defer tp.mutProcessors.RUnlock()
list := make([]p2p.MessageProcessor, 0, len(tp.processors))
identifiers := make([]string, 0, len(tp.processors))
for identifier, handler := range tp.processors {
list = append(list, handler)
identifiers = append(identifiers, identifier)
}
return identifiers, list
}