This repository was archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrplx_api.go
154 lines (117 loc) · 3.32 KB
/
rplx_api.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package rplx
import (
"github.com/pkg/errors"
"time"
)
var (
// ErrVariableNotExists returns if variable not exists
ErrVariableNotExists = errors.New("variable not exists")
// ErrVariableExpired returns if variable is expired
ErrVariableExpired = errors.New("variable expired")
)
// Get returns variable v or error if variable not exists or expired
// if variable expired, removes variable from rplx.variables map
func (rplx *Rplx) Get(name string) (int64, error) {
rplx.variablesMx.RLock()
v, ok := rplx.variables[name]
rplx.variablesMx.RUnlock()
if !ok {
return 0, ErrVariableNotExists
}
ttl := v.TTL()
if ttl > 0 && ttl < time.Now().UTC().UnixNano() {
rplx.variablesMx.Lock()
delete(rplx.variables, name)
rplx.variablesMx.Unlock()
return 0, ErrVariableExpired
}
return v.get(), nil
}
// VariablePartsCount returns count remote nodes parts for variable
func (rplx *Rplx) VariablePartsCount(name string) (int, error) {
rplx.variablesMx.RLock()
v, ok := rplx.variables[name]
rplx.variablesMx.RUnlock()
if !ok {
return 0, ErrVariableNotExists
}
ttl := v.TTL()
if ttl > 0 && ttl < time.Now().UTC().UnixNano() {
rplx.variablesMx.Lock()
delete(rplx.variables, name)
rplx.variablesMx.Unlock()
return 0, ErrVariableExpired
}
return v.partsCount(), nil
}
// Delete sets for variable ttl with -1 sec from Now,
// sends variable to replication (update TTL on clients) and remove variable from rplx.variables map
func (rplx *Rplx) Delete(name string) error {
rplx.variablesMx.Lock()
defer rplx.variablesMx.Unlock()
v, ok := rplx.variables[name]
if !ok {
return ErrVariableNotExists
}
v.updateTTL(time.Now().UTC().Add(-time.Second).UnixNano())
go rplx.sendToReplication(v)
delete(rplx.variables, name)
return nil
}
// UpdateTTL updates TTL for variable or return error if variable not exists
func (rplx *Rplx) UpdateTTL(name string, ttl time.Time) error {
rplx.variablesMx.RLock()
defer rplx.variablesMx.RUnlock()
v, ok := rplx.variables[name]
if !ok {
return ErrVariableNotExists
}
v.updateTTL(ttl.UnixNano())
go rplx.sendToReplication(v)
return nil
}
// Upsert change variable on delta or create variable, if not exists
// returns new value
func (rplx *Rplx) Upsert(name string, delta int64) int64 {
var v *variable
var ok bool
rplx.variablesMx.RLock()
v, ok = rplx.variables[name]
rplx.variablesMx.RUnlock()
if !ok {
rplx.variablesMx.Lock()
v, ok = rplx.variables[name]
if !ok {
v = newVariable(name)
rplx.variables[name] = v
}
rplx.variablesMx.Unlock()
}
ttl := v.TTL()
// if variable has TTL and TTL less than Now, variable was expired, but not garbage collected
if ttl > 0 && ttl < time.Now().UTC().UnixNano() {
v.updateTTL(0)
delta = delta - v.get()
}
v.update(delta)
go rplx.sendToReplication(v)
return v.get()
}
// All returns all variables values
// first returns param - not expires variables
// second param - expires, but not garbage collected variables
func (rplx *Rplx) All() (notExpired map[string]int64, expired map[string]int64) {
notExpired = make(map[string]int64)
expired = make(map[string]int64)
rplx.variablesMx.RLock()
defer rplx.variablesMx.RUnlock()
for name, v := range rplx.variables {
ttl := v.TTL()
if ttl > 0 && ttl < time.Now().UTC().UnixNano() {
expired[name] = v.get()
continue
}
notExpired[name] = v.get()
}
return
}