-
Notifications
You must be signed in to change notification settings - Fork 20
/
message.go
96 lines (81 loc) · 1.93 KB
/
message.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
package bcache
import (
"log"
"sync"
"github.com/weaveworks/mesh"
)
const (
defaultNumEntries = 5
)
// message defines gossip message used for communication between peers
// TODO: use protobuf for encoding
type message struct {
mux sync.RWMutex
PeerID mesh.PeerName
Entries map[string]entry
}
// entry is a single key value entry
type entry struct {
Val string
Expired int64
Deleted int64
}
func newMessage(peerID mesh.PeerName, numEntries int) *message {
if numEntries == 0 {
numEntries = defaultNumEntries
}
return newMessageFromEntries(peerID, make(map[string]entry, numEntries))
}
func newMessageFromEntries(peerID mesh.PeerName, entries map[string]entry) *message {
newEntries := make(map[string]entry, len(entries))
for k, v := range entries {
newEntries[k] = v
}
return &message{
PeerID: peerID,
Entries: newEntries,
}
}
func newMessageFromBuf(b []byte) (*message, error) {
var m message
err := unmarshal(b, &m)
return &m, err
}
func (m *message) add(key, val string, expired, deleted int64) {
m.mux.Lock()
m.Entries[key] = entry{
Val: val,
Expired: expired,
Deleted: deleted,
}
m.mux.Unlock()
}
// Encode implements mesh.GossipData.Encode
// TODO: split the encoding by X number of keys
func (m *message) Encode() [][]byte {
m.mux.RLock()
defer m.mux.RUnlock()
b, err := marshal(m)
if err != nil {
log.Printf("failed to encode message: %v", err)
}
return [][]byte{b}
}
// Merge implements mesh.GossipData.Merge
func (m *message) Merge(other mesh.GossipData) (complete mesh.GossipData) {
return m.mergeComplete(other.(*message))
}
func (m *message) mergeComplete(other *message) mesh.GossipData {
m.mux.Lock()
defer m.mux.Unlock()
for k, v := range other.Entries {
existing, ok := m.Entries[k]
// merge
// - the key not exists in
// - has less expiration time
if !ok || existing.Expired < v.Expired {
m.Entries[k] = v
}
}
return newMessageFromEntries(m.PeerID, m.Entries)
}