This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
234 lines (199 loc) · 4.78 KB
/
wrapper.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package blobstore
import (
"bytes"
"container/list"
"io"
"io/ioutil"
"log"
"path"
"sync"
)
type lru struct {
maxSize int64
currentSize int64
ll *list.List
lookup map[string]*list.Element
Client
}
// Create a new LRU cache. Size is in bytes
func LRU(size int64, i Client) Client {
return &lru{
maxSize: size,
lookup: make(map[string]*list.Element),
ll: list.New(),
Client: i,
}
}
type entry struct {
key string
size int64
}
func (l *lru) Put(key string, blob io.Reader, length int64) error {
if err := l.Client.Put(key, blob, length); err != nil {
return err
}
if ee, ok := l.lookup[key]; ok {
l.ll.MoveToFront(ee)
return nil
} else {
ele := l.ll.PushFront(entry{key, length})
l.currentSize += length
l.lookup[key] = ele
for l.maxSize != 0 && l.currentSize > l.maxSize {
if ele := l.ll.Back(); ele != nil {
l.Delete(ele.Value.(entry).key)
}
}
}
return nil
}
func (l *lru) Get(key string) (io.ReadCloser, int64, error) {
if ele, hit := l.lookup[key]; hit {
l.ll.MoveToFront(ele)
}
return l.Client.Get(key)
}
func (l *lru) Delete(key string) error {
if ele, hit := l.lookup[key]; hit {
l.ll.Remove(ele)
l.currentSize -= ele.Value.(entry).size
delete(l.lookup, key)
}
return l.Client.Delete(key)
}
type synchronized struct {
mu sync.RWMutex
Client
}
func NewSynchronized(c Client) Client {
return &synchronized{Client: c}
}
func (s *synchronized) Put(key string, blob io.Reader, length int64) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.Client.Put(key, blob, length)
}
func (s *synchronized) Get(key string) (io.ReadCloser, int64, error) {
// Note the use of `Lock` instead of `RLock`. This is intentional.
//
// The LRU wrapper mutates internal state on every get call. Using RLock
// led to panics due to concurrent map and list access.
s.mu.Lock()
defer s.mu.Unlock()
return s.Client.Get(key)
}
func (s *synchronized) Delete(key string) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.Client.Delete(key)
}
func (s *synchronized) Contains(key string) (bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.Client.Contains(key)
}
type prefixed struct {
prefix string
Client
}
func Prefixed(prefix string, i Client) Client {
return &prefixed{prefix, i}
}
func (p *prefixed) Put(key string, blob io.Reader, length int64) error {
return p.Client.Put(path.Join("/", p.prefix, key), blob, length)
}
func (p *prefixed) Get(key string) (io.ReadCloser, int64, error) {
return p.Client.Get(path.Join("/", p.prefix, key))
}
func (p *prefixed) Delete(key string) error {
return p.Client.Delete(path.Join("/", p.prefix, key))
}
func (p *prefixed) Contains(key string) (bool, error) {
return p.Client.Contains(path.Join("/", p.prefix, key))
}
type cached struct {
authority Client
caches []Client
signal chan struct{} // for testing only
}
func Cached(authority Client, caches ...Client) Client {
return &cached{authority, caches, nil}
}
func (c *cached) Put(key string, blob io.Reader, length int64) error {
blob = c.putCache(key, ioutil.NopCloser(blob), length)
err := c.authority.Put(key, blob, length)
if err != nil {
return err
}
return nil
}
func (c *cached) Get(key string) (io.ReadCloser, int64, error) {
for _, cache := range c.caches {
rd, length, err := cache.Get(key)
if err == nil {
return rd, length, nil
}
}
rd, length, err := c.authority.Get(key)
if err != nil {
return nil, 0, err
}
rd = c.putCache(key, rd, length)
return rd, length, nil
}
func (c *cached) Delete(key string) error {
for _, cache := range c.caches {
// Deletion from a cache is best-effort, ignore failures
cache.Delete(key)
}
return c.authority.Delete(key)
}
func (c *cached) Contains(key string) (bool, error) {
return c.authority.Contains(key)
}
func (c *cached) putCache(key string, r io.ReadCloser, length int64) io.ReadCloser {
return &teeCacher{r: r, c: c, key: key, length: length}
}
type teeCacher struct {
r io.ReadCloser
c *cached
key string
length int64
buf bytes.Buffer
once sync.Once
}
func (t *teeCacher) Read(p []byte) (n int, err error) {
n, err = t.r.Read(p)
if n > 0 {
if n, err := t.buf.Write(p[:n]); err != nil {
return n, err
}
}
if int64(t.buf.Len()) == t.length {
t.put()
}
return
}
func (t *teeCacher) Close() error {
return t.r.Close()
}
func (t *teeCacher) put() {
t.once.Do(func() {
go func() {
defer t.signal()
for _, cache := range t.c.caches {
err := cache.Put(t.key, bytes.NewReader(t.buf.Bytes()), t.length)
if err != nil {
log.Printf("Failed to write to cache key: %s err: %s", t.key, err)
}
}
}()
})
}
// signal is only set in testing. it notifies a client of when
// the teeCacher has finished putting a value into its cache
func (t *teeCacher) signal() {
if t.c.signal != nil {
t.c.signal <- struct{}{}
}
}