-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsync_tools.go
120 lines (103 loc) · 2.54 KB
/
sync_tools.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
/*
*
* Copyright © 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package gobrick
import (
"sync"
"time"
)
type ttlCacheEntry struct {
data interface{}
expireTime time.Time
}
func newTTLCache(ttl time.Duration) *ttlCache {
c := &ttlCache{ttl: ttl}
c.entries = make(map[string]ttlCacheEntry)
c.ticker = time.NewTicker(time.Second)
go c.tick()
return c
}
// ttl cache
type ttlCache struct {
ttl time.Duration
mutex sync.RWMutex
entries map[string]ttlCacheEntry
ticker *time.Ticker
}
func (c *ttlCache) tick() {
for range c.ticker.C {
c.purge()
}
}
func (c *ttlCache) Get(key string) (interface{}, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if value, ok := c.entries[key]; ok {
return value.data, true
}
return nil, false
}
func (c *ttlCache) Set(key string, value interface{}) {
c.set(key, value, c.ttl)
}
func (c *ttlCache) SetWithTTL(key string, value interface{}, ttl time.Duration) {
c.set(key, value, ttl)
}
func (c *ttlCache) set(key string, value interface{}, ttl time.Duration) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.entries[key] = ttlCacheEntry{data: value, expireTime: time.Now().Add(ttl)}
}
// remove entries with expired ttl
func (c *ttlCache) purge() {
c.mutex.Lock()
defer c.mutex.Unlock()
for key, value := range c.entries {
if value.expireTime.Before(time.Now()) {
delete(c.entries, key)
}
}
}
func (c *ttlCache) Stop() {
c.ticker.Stop()
}
func newRateLock() *rateLock {
m := &rateLock{}
m.ttlCache = newTTLCache(time.Second)
return m
}
// ttl based named m
type rateLock struct {
ttlCache *ttlCache
m sync.Mutex
}
// RateCheck checks if key exist in internal ttl cache
// If key not exist will create it with required ttl.
// Useful for ttl-based rate limiting
func (r *rateLock) RateCheck(key string, ttl time.Duration) bool {
r.m.Lock()
defer r.m.Unlock()
_, found := r.ttlCache.Get(key)
if found {
return false
}
r.ttlCache.SetWithTTL(key, "", ttl)
return true
}
func (r *rateLock) Stop() {
r.ttlCache.Stop()
}