Debian Codegraph @

store/memstore/memstore.go

raw
1 // Package memstore offers an in-memory store implementation for throttled.
2 package memstore // import "github.com/throttled/throttled/store/memstore"
3
4 import (
5 "sync"
6 "sync/atomic"
7 "time"
8
9 "github.com/hashicorp/golang-lru"
10 )
11
12 // MemStore is an in-memory store implementation for throttled. It
13 // supports evicting the least recently used keys to control memory
14 // usage. It is stored in memory in the current process and thus
15 // doesn't share state with other rate limiters.
16 type MemStore struct {
17 sync.RWMutex
18 keys *lru.Cache
19 m map[string]*int64
20 }
21
22 // New initializes a Store. If maxKeys > 0, the number of different
23 // keys is restricted to the specified amount. In this case, it uses
24 // an LRU algorithm to evict older keys to make room for newer
25 // ones. If maxKeys <= 0, there is no limit on the number of keys,
26 // which may use an unbounded amount of memory.
27 func New(maxKeys int) (*MemStore, error) {
28 var m *MemStore
29
30 if maxKeys > 0 {
31 keys, err := lru.New(maxKeys)
32 if err != nil {
33 return nil, err
34 }
35
36 m = &MemStore{
37 keys: keys,
38 }
39 } else {
40 m = &MemStore{
41 m: make(map[string]*int64),
42 }
43 }
44 return m, nil
45 }
46
47 // GetWithTime returns the value of the key if it is in the store or
48 // -1 if it does not exist. It also returns the current local time on
49 // the machine.
50 func (ms *MemStore) GetWithTime(key string) (int64, time.Time, error) {
51 now := time.Now()
52 valP, ok := ms.get(key, false)
53
54 if !ok {
55 return -1, now, nil
56 }
57
58 return atomic.LoadInt64(valP), now, nil
59 }
60
61 // SetIfNotExistsWithTTL sets the value of key only if it is not
62 // already set in the store it returns whether a new value was set. It
63 // ignores the ttl.
64 func (ms *MemStore) SetIfNotExistsWithTTL(key string, value int64, _ time.Duration) (bool, error) {
65 _, ok := ms.get(key, false)
66
67 if ok {
68 return false, nil
69 }
70
71 ms.Lock()
72 defer ms.Unlock()
73
74 _, ok = ms.get(key, true)
75
76 if ok {
77 return false, nil
78 }
79
80 // Store a pointer to a new instance so that the caller
81 // can't mutate the value after setting
82 v := value
83
84 if ms.keys != nil {
85 ms.keys.Add(key, &v)
86 } else {
87 ms.m[key] = &v
88 }
89
90 return true, nil
91 }
92
93 // CompareAndSwapWithTTL atomically compares the value at key to the
94 // old value. If it matches, it sets it to the new value and returns
95 // true. Otherwise, it returns false. If the key does not exist in the
96 // store, it returns false with no error. It ignores the ttl.
97 func (ms *MemStore) CompareAndSwapWithTTL(key string, old, new int64, _ time.Duration) (bool, error) {
98 valP, ok := ms.get(key, false)
99
100 if !ok {
101 return false, nil
102 }
103
104 return atomic.CompareAndSwapInt64(valP, old, new), nil
105 }
106
107 func (ms *MemStore) get(key string, locked bool) (*int64, bool) {
108 var valP *int64
109 var ok bool
110
111 if ms.keys != nil {
112 var valI interface{}
113
114 valI, ok = ms.keys.Get(key)
115 if ok {
116 valP = valI.(*int64)
117 }
118 } else {
119 if !locked {
120 ms.RLock()
121 defer ms.RUnlock()
122 }
123 valP, ok = ms.m[key]
124 }
125
126 return valP, ok
127 }