forked from bolt-observer/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channelcache.go
122 lines (100 loc) · 2.83 KB
/
channelcache.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
110
111
112
113
114
115
116
117
118
119
120
121
122
package nodedata
import (
"sync"
"github.com/golang/glog"
)
// ChannelCache struct
type ChannelCache interface {
// Lock - acquire lock
Lock()
// Unlock - release lock
Unlock()
// Get - get channel cache
Get(name string) (string, bool)
// Set - set channel cache
Set(name string, value string)
// DeferredSet - set channel cache but do not commit it yet
DeferredSet(name, old, new string)
// DeferredCommit - commit channel cache from DeferredSet
DeferredCommit() bool
// DeferredRevert - revert channel cache from DeferredSet
DeferredRevert() bool
}
// Lock - acquire lock
func (c *InMemoryChannelCache) Lock() {
c.chanMutex.Lock()
}
// Unlock - release lock
func (c *InMemoryChannelCache) Unlock() {
c.chanMutex.Unlock()
}
// Get - get channel cache
func (c *InMemoryChannelCache) Get(name string) (string, bool) {
a, b := c.chanCache[name]
return a, b
}
// Set - set channel cache
func (c *InMemoryChannelCache) Set(name string, value string) {
c.chanCache[name] = value
}
// OldNewVal struct
type OldNewVal struct {
OldValue string
NewValue string
}
// InMemoryChannelCache - ChannelCache implementation that stores data in memory
type InMemoryChannelCache struct {
chanMutex sync.Mutex
chanCache map[string]string
deferredCacheMutex sync.Mutex
deferredCache map[string]OldNewVal
}
// NewInMemoryChannelCache - construct new InMemoryChannelCache
func NewInMemoryChannelCache() *InMemoryChannelCache {
resp := &InMemoryChannelCache{
chanCache: make(map[string]string),
chanMutex: sync.Mutex{},
deferredCacheMutex: sync.Mutex{},
deferredCache: make(map[string]OldNewVal),
}
return resp
}
// DeferredSet - set channel cache but do not commit it yet
func (c *InMemoryChannelCache) DeferredSet(name, old, new string) {
c.deferredCacheMutex.Lock()
defer c.deferredCacheMutex.Unlock()
glog.V(3).Infof("DeferredSet %s old %s new %s", name, old, new)
c.deferredCache[name] = OldNewVal{OldValue: old, NewValue: new}
}
// DeferredCommit - commit channel cache from DeferredSet
func (c *InMemoryChannelCache) DeferredCommit() bool {
c.Lock()
c.deferredCacheMutex.Lock()
defer c.Unlock()
defer c.deferredCacheMutex.Unlock()
for k, v := range c.deferredCache {
val, exists := c.Get(k)
if !exists || val == v.OldValue {
glog.V(3).Infof("Commit ok %s from %s to %s", k, val, v.NewValue)
c.Set(k, v.NewValue)
} else {
glog.V(3).Infof("Commit %s not same value %s vs %s", k, val, v.OldValue)
}
}
for k := range c.deferredCache {
delete(c.deferredCache, k)
}
return true
}
// DeferredRevert - revert channel cache from DeferredSet
func (c *InMemoryChannelCache) DeferredRevert() bool {
c.Lock()
c.deferredCacheMutex.Lock()
defer c.Unlock()
defer c.deferredCacheMutex.Unlock()
glog.V(3).Infof("DeferredRevert")
for k := range c.deferredCache {
delete(c.deferredCache, k)
}
return true
}