-
Notifications
You must be signed in to change notification settings - Fork 22
/
message_que.go
62 lines (51 loc) · 1.1 KB
/
message_que.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
package hbbft
import (
"sync"
)
// MessageTuple holds the payload of the message along with the identifier of
// the receiver node.
type MessageTuple struct {
To uint64
Payload interface{}
}
type messageQue struct {
que []MessageTuple
lock sync.RWMutex
}
func newMessageQue() *messageQue {
return &messageQue{
que: []MessageTuple{},
}
}
func (q *messageQue) addMessage(msg interface{}, to uint64) {
q.lock.Lock()
defer q.lock.Unlock()
q.que = append(q.que, MessageTuple{to, msg})
}
func (q *messageQue) addMessages(msgs ...MessageTuple) {
q.lock.Lock()
defer q.lock.Unlock()
q.que = append(q.que, msgs...)
}
func (q *messageQue) addQue(que *messageQue) {
newQue := make([]MessageTuple, len(q.que)+que.len())
copy(newQue, q.messages())
copy(newQue, que.messages())
q.lock.Lock()
defer q.lock.Unlock()
q.que = newQue
}
func (q *messageQue) len() int {
q.lock.RLock()
defer q.lock.RUnlock()
return len(q.que)
}
func (q *messageQue) messages() []MessageTuple {
q.lock.RLock()
msgs := q.que
q.lock.RUnlock()
q.lock.Lock()
defer q.lock.Unlock()
q.que = []MessageTuple{}
return msgs
}