-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
60 lines (50 loc) · 1.03 KB
/
counter.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
package httpclient
import (
"container/list"
)
// NonceCounter implements an LRU cache for tracking the
// counter values of nonces.
type NonceCounter struct {
// m maps nonce to list elements
m map[string]*list.Element
// ll orders most recently used elements to the front
ll *list.List
// cap specifies the capacity of this cache
cap int
}
// NewNonceCounter returns a new NonceCounter with
// the specified capacity
func NewNonceCounter(cap int) *NonceCounter {
if cap < 1 {
cap = 1
}
return &NonceCounter{
m: make(map[string]*list.Element),
ll: list.New(),
cap: cap,
}
}
// Next increments the counter for nonce and returns
// the new counter value
func (nc *NonceCounter) Next(nonce string) int {
p, ok := nc.m[nonce]
if ok {
nc.ll.MoveToFront(p)
v := p.Value.(item)
v.n = v.n + 1
return v.n
}
if len(nc.m) == nc.cap {
p = nc.ll.Back()
nc.ll.Remove(p)
delete(nc.m, p.Value.(item).k)
}
v := item{nonce, 1}
p = nc.ll.PushFront(v)
nc.m[nonce] = p
return v.n
}
type item struct {
k string
n int
}