-
Notifications
You must be signed in to change notification settings - Fork 2
/
msgs.go
109 lines (90 loc) · 3.04 KB
/
msgs.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package msgstore
import (
"sync"
"github.com/hyperledger/fabric/gossip/common"
)
// invalidationTrigger is invoked on each message that was invalidated because of a message addition
// i.e: if add(0), add(1) was called one after the other, and the store has only {1} after the sequence of invocations
// then the invalidation trigger on 0 was called when 1 was added.
type invalidationTrigger func(message interface{})
// NewMessageStore returns a new MessageStore with the message replacing
// policy and invalidation trigger passed.
func NewMessageStore(pol common.MessageReplacingPolicy, trigger invalidationTrigger) MessageStore {
return &messageStoreImpl{pol: pol, lock: &sync.RWMutex{}, messages: make([]*msg, 0), invTrigger: trigger}
}
// MessageStore adds messages to an internal buffer.
// When a message is received, it might:
// - Be added to the buffer
// - Discarded because of some message already in the buffer (invalidated)
// - Make a message already in the buffer to be discarded (invalidates)
// When a message is invalidated, the invalidationTrigger is invoked on that message.
type MessageStore interface {
// add adds a message to the store
// returns true or false whether the message was added to the store
Add(msg interface{}) bool
// size returns the amount of messages in the store
Size() int
// get returns all messages in the store
Get() []interface{}
}
type messageStoreImpl struct {
pol common.MessageReplacingPolicy
lock *sync.RWMutex
messages []*msg
invTrigger invalidationTrigger
}
type msg struct {
data interface{}
}
// add adds a message to the store
func (s *messageStoreImpl) Add(message interface{}) bool {
s.lock.Lock()
defer s.lock.Unlock()
n := len(s.messages)
for i := 0; i < n; i++ {
m := s.messages[i]
switch s.pol(message, m.data) {
case common.MessageInvalidated:
return false
case common.MessageInvalidates:
s.invTrigger(m.data)
s.messages = append(s.messages[:i], s.messages[i+1:]...)
n--
i--
break
default:
break
}
}
s.messages = append(s.messages, &msg{data: message})
return true
}
// size returns the amount of messages in the store
func (s *messageStoreImpl) Size() int {
s.lock.RLock()
defer s.lock.RUnlock()
return len(s.messages)
}
// get returns all messages in the store
func (s *messageStoreImpl) Get() []interface{} {
s.lock.RLock()
defer s.lock.RUnlock()
n := len(s.messages)
res := make([]interface{}, n)
for i := 0; i < n; i++ {
res[i] = s.messages[i].data
}
return res
}