-
Notifications
You must be signed in to change notification settings - Fork 197
/
countInterceptor.go
50 lines (40 loc) · 1.31 KB
/
countInterceptor.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
package integrationTests
import (
"sync"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go/p2p"
)
// CountInterceptor represents an interceptors that counts received messages on topics
type CountInterceptor struct {
mutMessagesCount sync.RWMutex
messagesCount map[string]int
}
// NewCountInterceptor creates a new CountInterceptor instance
func NewCountInterceptor() *CountInterceptor {
return &CountInterceptor{
messagesCount: make(map[string]int),
}
}
// ProcessReceivedMessage is called each time a new message is received
func (ci *CountInterceptor) ProcessReceivedMessage(message p2p.MessageP2P, _ core.PeerID) error {
ci.mutMessagesCount.Lock()
ci.messagesCount[message.Topic()]++
ci.mutMessagesCount.Unlock()
return nil
}
// MessageCount returns the number of messages received on the provided topic
func (ci *CountInterceptor) MessageCount(topic string) int {
ci.mutMessagesCount.RLock()
defer ci.mutMessagesCount.RUnlock()
return ci.messagesCount[topic]
}
// Reset resets the counters values
func (ci *CountInterceptor) Reset() {
ci.mutMessagesCount.Lock()
ci.messagesCount = make(map[string]int)
ci.mutMessagesCount.Unlock()
}
// IsInterfaceNil returns true if there is no value under the interface
func (ci *CountInterceptor) IsInterfaceNil() bool {
return ci == nil
}