/
timeout.go
190 lines (170 loc) · 4.67 KB
/
timeout.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
重复插入key相同的value, 不更新该key的deadline
*/
package timeout
import (
"container/heap"
"context"
"strings"
"sync"
"time"
"github.com/erda-project/erda/pkg/jsonstore/js_util"
"github.com/erda-project/erda/pkg/jsonstore/storetypes"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type elem struct {
deadline time.Time
key string
}
type elemHeap struct {
elems []elem
keys map[string]struct{}
}
func (h elemHeap) Len() int { return len(h.elems) }
func (h elemHeap) Less(i, j int) bool { return h.elems[i].key < h.elems[j].key }
func (h elemHeap) Swap(i, j int) { h.elems[i], h.elems[j] = h.elems[j], h.elems[i] }
func (h *elemHeap) Push(v interface{}) {
e := v.(elem)
if _, ok := h.keys[e.key]; ok {
return
}
h.elems = append(h.elems, e)
h.keys[e.key] = struct{}{}
}
func (h *elemHeap) Pop() interface{} {
old := h.elems
n := len(old)
v := old[n-1]
h.elems = old[0 : n-1]
return v
}
type TimeoutStore struct {
timeout time.Duration
backend storetypes.Store
elemheap *elemHeap
sync.RWMutex
}
func New(timeout int, backend storetypes.Store) (*TimeoutStore, error) {
if timeout <= 0 {
return nil, errors.New("illegal timeout value")
}
s := &TimeoutStore{
timeout: time.Duration(timeout) * time.Second,
backend: backend,
elemheap: &elemHeap{keys: map[string]struct{}{}},
}
heap.Init(s.elemheap)
go s.clear()
return s, nil
}
func (s *TimeoutStore) clear() {
for {
if len(s.elemheap.keys) == 0 {
time.Sleep(100 * time.Millisecond)
continue
}
s.Lock()
first := heap.Pop(s.elemheap).(elem)
s.Unlock()
<-time.After(time.Until(first.deadline))
s.Remove(context.Background(), first.key)
s.Lock()
delete(s.elemheap.keys, first.key)
s.Unlock()
// sync s.elemheap.elems with s.elemheap.keys
s.Lock()
gap := len(s.elemheap.elems) - len(s.elemheap.keys)
if gap > len(s.elemheap.elems)/3 && gap > 10 {
newElems := []elem{}
for _, v := range s.elemheap.elems {
if _, ok := s.elemheap.keys[v.key]; ok {
newElems = append(newElems, v)
}
}
s.elemheap.elems = newElems
if len(s.elemheap.elems) != len(s.elemheap.keys) {
logrus.Errorf("inconsistent num elemheap.elems and elemheap.keys")
}
}
s.Unlock()
}
}
func (s *TimeoutStore) Put(ctx context.Context, key, value string) error {
if err := s.backend.Put(ctx, key, value); err != nil {
return err
}
s.Lock()
heap.Push(s.elemheap, elem{time.Now().Add(s.timeout), key})
s.Unlock()
return nil
}
func (s *TimeoutStore) PutWithOption(ctx context.Context, key, value string, opts []interface{}) (interface{}, error) {
return nil, s.Put(ctx, key, value)
}
func (s *TimeoutStore) Get(ctx context.Context, key string) (storetypes.KeyValue, error) {
s.RLock()
defer s.RUnlock()
if _, ok := s.elemheap.keys[key]; !ok {
return storetypes.KeyValue{}, errors.New("not found")
}
return s.backend.Get(ctx, key)
}
func (s *TimeoutStore) PrefixGet(ctx context.Context, prefix string) ([]storetypes.KeyValue, error) {
s.RLock()
defer s.RUnlock()
ks := []string{}
for k := range s.elemheap.keys {
if strings.HasPrefix(k, prefix) {
ks = append(ks, k)
}
}
kvs, err := s.backend.PrefixGet(ctx, prefix)
if err != nil {
return nil, err
}
interSectionKvs, _ := js_util.InterSectionKVs(kvs, ks)
return interSectionKvs, nil
}
func (s *TimeoutStore) Remove(ctx context.Context, key string) (*storetypes.KeyValue, error) {
s.Lock()
defer s.Unlock()
if _, ok := s.elemheap.keys[key]; !ok {
return nil, nil
}
delete(s.elemheap.keys, key)
return s.backend.Remove(ctx, key)
}
func (s *TimeoutStore) PrefixRemove(ctx context.Context, prefix string) ([]storetypes.KeyValue, error) {
s.Lock()
defer s.Unlock()
for k := range s.elemheap.keys {
if strings.HasPrefix(k, prefix) {
delete(s.elemheap.keys, k)
}
}
return s.backend.PrefixRemove(ctx, prefix)
}
func (s *TimeoutStore) PrefixGetKey(ctx context.Context, prefix string) ([]storetypes.Key, error) {
s.RLock()
defer s.RUnlock()
keys := []storetypes.Key{}
for k := range s.elemheap.keys {
if strings.HasPrefix(k, prefix) {
keys = append(keys, storetypes.Key(k))
}
}
return keys, nil
}