This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
filecache.go
448 lines (372 loc) · 12.3 KB
/
filecache.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package filecache
import (
"crypto/md5"
"errors"
"fmt"
"hash/fnv"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/djherbis/times"
"github.com/hashicorp/golang-lru"
log "github.com/sirupsen/logrus"
)
const (
DownloadMangerS3 = iota
DownloadMangerDropbox
)
var (
errInvalidURLPath = errors.New("invalid URL path")
// HashableArgs allows us to support various authentication headers in the future
HashableArgs = map[string]struct{}{}
)
type DownloadManager int
// DownloadRecord contains information about a file which will be downloaded
type DownloadRecord struct {
Manager DownloadManager
Path string
Args map[string]string
HashedArgs string
}
type RecordDownloaderFunc = func(dr *DownloadRecord, localFile *os.File) error
// FileCache is a wrapper for hashicorp/golang-lru
type FileCache struct {
BaseDir string
Cache *lru.Cache
Waiting map[string]chan struct{}
WaitLock sync.Mutex
DownloadFunc func(dr *DownloadRecord, localPath string) error
OnEvict func(key interface{}, value interface{})
DefaultExtension string
DownloadTimeout time.Duration
downloaders map[DownloadManager]RecordDownloaderFunc
}
type option func(*FileCache) error
func setSize(size int) option {
return func(c *FileCache) error {
cache, err := lru.NewWithEvict(size, c.onEvictDelete)
if err != nil {
return fmt.Errorf("invalid size: %s", err)
}
c.Cache = cache
return nil
}
}
func setBaseDir(baseDir string) option {
return func(c *FileCache) error {
if baseDir == "" {
return errors.New("empty baseDir")
}
c.BaseDir = baseDir
return nil
}
}
// DownloadTimeout sets the file download timeout
func DownloadTimeout(timeout time.Duration) option {
return func(c *FileCache) error {
c.DownloadTimeout = timeout
return nil
}
}
// DefaultExtension sets the default extension which will be appended to
// cached files in the local directory
func DefaultExtension(ext string) option {
return func(c *FileCache) error {
c.DefaultExtension = ext
return nil
}
}
// S3Downloader allows the DownloadFunc to pull files from S3 buckets.
// Bucket names are passed at the first part of the path in files requested
// from the cache. Bubbles up errors from the Hashicrorp LRU library
// when something goes wrong there.
func S3Downloader(awsRegion string) option {
return func(c *FileCache) error {
c.downloaders[DownloadMangerS3] = func(dr *DownloadRecord, localFile *os.File) error {
return NewS3RegionManagedDownloader(awsRegion).Download(
dr, localFile, c.DownloadTimeout,
)
}
return nil
}
}
// DropboxDownloader allows the DownloadFunc to pull files from Dropbox
// accounts. Bubbles up errors from the Hashicrorp LRU library when
// something goes wrong there.
func DropboxDownloader() option {
return func(c *FileCache) error {
c.downloaders[DownloadMangerDropbox] = func(dr *DownloadRecord, localFile *os.File) error {
return DropboxDownload(dr, localFile, c.DownloadTimeout)
}
return nil
}
}
// download is a generic wrapper which performs common actions before delegating to the
// specific downloader implementations
func (c *FileCache) download(dr *DownloadRecord, localPath string) error {
directory := filepath.Dir(localPath)
if directory != "." {
// Make sure the path to the local file exists
log.Debugf("MkdirAll() on %s", filepath.Dir(localPath))
err := os.MkdirAll(filepath.Dir(localPath), 0755)
if err != nil {
return fmt.Errorf("could not create local directory: %s", err)
}
}
localFile, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("could not create local file: %s", err)
}
defer localFile.Close()
if downloader, ok := c.downloaders[dr.Manager]; ok {
return downloader(dr, localFile)
}
return fmt.Errorf("no dowloader found for %q", dr.Path)
}
// New returns a properly configured cache. Bubbles up errors from the Hashicrorp
// LRU library when something goes wrong there. The configured cache will have a
// noop DownloadFunc, which should be replaced if you want to actually get files
// from somewhere. Or, look at NewS3Cache() which is backed by Amazon S3.
func New(size int, baseDir string, opts ...option) (*FileCache, error) {
fCache := &FileCache{
Waiting: make(map[string]chan struct{}),
downloaders: make(map[DownloadManager]RecordDownloaderFunc),
}
fCache.DownloadFunc = fCache.download
if err := setSize(size)(fCache); err != nil {
return nil, err
}
if err := setBaseDir(baseDir)(fCache); err != nil {
return nil, err
}
for _, opt := range opts {
err := opt(fCache)
if err != nil {
return nil, fmt.Errorf("invalid option: %s", err)
}
}
return fCache, nil
}
// FetchNewerThan will look in the cache for a file, make sure it's newer than
// timestamp, and if so return true. Otherwise it will possibly download the file
// and only return false if it's unable to do so.
func (c *FileCache) FetchNewerThan(dr *DownloadRecord, timestamp time.Time) bool {
if !c.Contains(dr) {
return c.Fetch(dr)
}
storagePath := c.GetFileName(dr)
stat, err := times.Stat(storagePath)
if err != nil {
return c.Fetch(dr)
}
// We use mtime because the file could have been overwritten with new data
// Compare the timestamp, and need to check the cache again... could have changed
if c.Contains(dr) && timestamp.Before(stat.ModTime()) {
return true
}
return c.Reload(dr)
}
// Fetch will return true if we have the file, or will go download the file and
// return true if we can. It will return false only if it's unable to fetch the
// file from the backing store (S3).
func (c *FileCache) Fetch(dr *DownloadRecord) bool {
if c.Contains(dr) {
return true
}
err := c.MaybeDownload(dr)
if err != nil {
log.Errorf("Tried to fetch file %s, got '%s'", dr.Path, err)
return false
}
return true
}
// Reload will remove a file from the cache and attempt to reload from the
// backing store, calling MaybeDownload().
func (c *FileCache) Reload(dr *DownloadRecord) bool {
c.Cache.Remove(dr.GetUniqueName())
err := c.MaybeDownload(dr)
if err != nil {
log.Errorf("Tried to fetch file %s, got '%s'", dr.Path, err)
return false
}
return true
}
// Contains looks to see if we have an entry in the cache for this file.
func (c *FileCache) Contains(dr *DownloadRecord) bool {
return c.Cache.Contains(dr.GetUniqueName())
}
// MaybeDownload might go out to the backing store (S3) and get the file if the
// file isn't already being downloaded in another routine. In both cases it will
// block until the download is completed either by this goroutine or another one.
func (c *FileCache) MaybeDownload(dr *DownloadRecord) error {
// See if someone is already downloading
c.WaitLock.Lock()
if waitChan, ok := c.Waiting[dr.GetUniqueName()]; ok {
c.WaitLock.Unlock()
log.Debugf("Awaiting download of %s", dr.Path)
<-waitChan
return nil
}
// The file could have arrived while we were getting here
if c.Contains(dr) {
c.WaitLock.Unlock()
return nil
}
// Still don't have it, let's fetch it.
// This tells other goroutines that we're fetching, and
// lets us signal completion.
log.Debugf("Making channel for %s", dr.Path)
c.Waiting[dr.GetUniqueName()] = make(chan struct{})
c.WaitLock.Unlock()
// Ensure we don't leave the channel open when leaving this function
defer func() {
c.WaitLock.Lock()
log.Debugf("Deleting channel for %s", dr.Path)
close(c.Waiting[dr.GetUniqueName()]) // Notify anyone waiting on us
delete(c.Waiting, dr.GetUniqueName()) // Remove it from the waiting map
c.WaitLock.Unlock()
}()
storagePath := c.GetFileName(dr)
err := c.DownloadFunc(dr, storagePath)
if err != nil {
return err
}
c.Cache.Add(dr.GetUniqueName(), storagePath)
return nil
}
// onEvictDelete is a callback that is triggered when the LRU cache expires an
// entry.
func (c *FileCache) onEvictDelete(key interface{}, value interface{}) {
filename := key.(string)
storagePath := value.(string)
if c.OnEvict != nil {
c.OnEvict(key, value)
}
log.Debugf("Got eviction notice for '%s', removing", key)
err := os.Remove(storagePath)
if err != nil {
log.Errorf("Unable to evict '%s' at local path '%s': %s", filename, storagePath, err)
return
}
}
// Purge clears all the files from the cache (via the onEvict callback for each key).
func (c *FileCache) Purge() {
c.Cache.Purge()
}
// PurgeAsync clears all the files from the cache and takes an optional channel
// to close when the purge has completed.
func (c *FileCache) PurgeAsync(doneChan chan struct{}) {
go func() {
c.Purge()
if doneChan != nil {
close(doneChan)
}
}()
}
// GetFileName returns the full storage path and file name for a file, if it were
// in the cache. This does _not_ check to see if the file is actually _in_ the
// cache. This builds a cache structure of up to 256 directories, each beginning
// with the first 2 letters of the FNV32 hash of the filename. This is then joined
// to the base dir and MD5 hashed filename to form the cache path for each file.
// It preserves the file extension (if present)
//
// e.g. /base_dir/2b/b0804ec967f48520697662a204f5fe72
//
func (c *FileCache) GetFileName(dr *DownloadRecord) string {
hashedFilename := md5.Sum([]byte(dr.Path))
fnvHasher := fnv.New32()
// The current implementation of fnv.New32().Write never returns a non-nil error
_, err := fnvHasher.Write([]byte(dr.Path))
if err != nil {
log.Errorf("Failed to compute the fnv hash: %s", err)
}
hashedDir := fnvHasher.Sum(nil)
// If we don't find an original file extension, we'll default to this one
extension := c.DefaultExtension
// Look in the last 5 characters for a . and extension
lastDot := strings.LastIndexByte(dr.Path, '.')
if lastDot > len(dr.Path)-6 {
extension = dr.Path[lastDot:]
}
var fileName string
if len(dr.Args) != 0 {
// in order to avoid file cache collision on the same filename, if we
// have existing HTTP headers into the dr.Args append their
// hashed value between the hashedFilename and extension with _ prefix
fileName = fmt.Sprintf("%x_%s%s", hashedFilename, dr.HashedArgs, extension)
} else {
fileName = fmt.Sprintf("%x%s", hashedFilename, extension)
}
dir := fmt.Sprintf("%x", hashedDir[:1])
return filepath.Join(c.BaseDir, dir, filepath.FromSlash(path.Clean("/"+fileName)))
}
// getHashedArgs computes the MD5 sum of the arguments existing in a DownloadRecord
// matching HashableArgs array and return the hashed value as a hex-encoded string
func getHashedArgs(args map[string]string) string {
if len(args) == 0 {
return ""
}
var builder strings.Builder
for hashableArg := range HashableArgs {
if arg, ok := args[hashableArg]; ok {
_, err := builder.WriteString(arg)
if err != nil {
continue
}
}
}
if builder.Len() == 0 {
return ""
}
hashedArgs := md5.Sum([]byte(builder.String()))
return fmt.Sprintf("%x", string(hashedArgs[:]))
}
// bucketToDownloadManager matches the given bucket to a suitable download manager
// TODO: Implement this in a more robust / generic way
func bucketToDownloadManager(bucket string) DownloadManager {
switch bucket {
case "dropbox":
return DownloadMangerDropbox
default:
return DownloadMangerS3
}
}
// NewDownloadRecord converts the incoming URL path into a download record containing a cached
// filename (this is the filename on the backing store, not the cached filename locally)
// together with the args needed for authentication
func NewDownloadRecord(url string, args map[string]string) (*DownloadRecord, error) {
pathParts := strings.Split(strings.TrimPrefix(url, "/documents/"), "/")
// We need at least a bucket and filename
if len(pathParts) < 2 {
return nil, errInvalidURLPath
}
path := strings.Join(pathParts, "/")
if path == "" || path == "/" {
return nil, errInvalidURLPath
}
// Make sure all arg names are lower case and contain only the ones we recognise
normalisedArgs := make(map[string]string, len(args))
for arg, value := range args {
normalisedArg := strings.ToLower(arg)
if _, ok := HashableArgs[normalisedArg]; !ok {
continue
}
normalisedArgs[normalisedArg] = value
}
return &DownloadRecord{
Manager: bucketToDownloadManager(pathParts[0]),
Path: path,
Args: normalisedArgs,
HashedArgs: getHashedArgs(normalisedArgs),
}, nil
}
// GetUniqueName returns a *HOPEFULLY* unique name for the download record
func (dr *DownloadRecord) GetUniqueName() string {
if len(dr.Args) > 0 {
return fmt.Sprintf("%s_%s", dr.Path, dr.HashedArgs)
}
return dr.Path
}