-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_gcounter.go
More file actions
57 lines (48 loc) · 1.1 KB
/
async_gcounter.go
File metadata and controls
57 lines (48 loc) · 1.1 KB
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
package percounter
import (
"github.com/Arceliar/phony"
)
type AsyncGCounter struct {
phony.Inbox
inner *GCounter
sink GCounterStateSink
}
func NewAsyncGCounter(identity string) *AsyncGCounter {
return NewAsyncGCounterWithSink(identity, &noOpGcounterState{})
}
func NewAsyncGCounterWithSink(identity string, sink GCounterStateSink) *AsyncGCounter {
return &AsyncGCounter{
inner: NewGCounter(identity),
sink: sink,
}
}
func NewAsyncGCounterWithSinkFromState(identity string, state GCounterState, sink GCounterStateSink) *AsyncGCounter {
return &AsyncGCounter{
inner: NewGCounterFromState(identity, state),
sink: sink,
}
}
func (c *AsyncGCounter) Increment() {
c.Act(c, func() {
c.inner.Increment()
})
}
func (c *AsyncGCounter) Value() int64 {
var val int64
phony.Block(c, func() {
val = c.inner.Value()
})
return val
}
func (c *AsyncGCounter) GetState() GCounterState {
var res GCounterState
phony.Block(c, func() {
res = c.inner.GetState().Copy()
})
return res
}
func (c *AsyncGCounter) MergeWith(other GCounterStateSource) {
c.Act(c, func() {
c.inner.MergeWith(other)
})
}