-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
73 lines (58 loc) · 1.36 KB
/
storage.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
package rpcsrv
import (
"bytes"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"github.com/sigex-kz/ddc"
"github.com/patrickmn/go-cache"
)
const (
constStorageCleanupInterval = 30
constStorageEntryTTL = 5 * 60
)
type builderEntry struct {
di ddc.DocumentInfo
embeddedFileName string
embeddedFileBuffer bytes.Buffer
ddcFileBuffer bytes.Buffer
}
type extractorEntry struct {
ddcFileBuffer bytes.Buffer
documentOriginal *ddc.AttachedFile
documentOriginalBytesRead int
signatures []ddc.AttachedFile
}
type entry struct {
created time.Time
mutex sync.Mutex
be *builderEntry
ee *extractorEntry
}
var store *cache.Cache = cache.New(time.Duration(constStorageEntryTTL)*time.Second, time.Duration(constStorageCleanupInterval)*time.Second)
func newStoreEntry(be *builderEntry, ee *extractorEntry) string {
/* #nosec */
id := fmt.Sprint(rand.Int())
for _, used := store.Get(id); used; _, used = store.Get(id) {
/* #nosec */
id = fmt.Sprint(rand.Int())
}
store.Set(id, &entry{
created: time.Now(),
be: be,
ee: ee,
}, cache.DefaultExpiration)
return id
}
func getStoreEntry(id string) (e *entry, err error) {
o, ok := store.Get(id)
if !ok {
return nil, errors.New("unknown id")
}
return o.(*entry), nil
}
func deleteStoreEntry(id string) {
store.Delete(id)
}