-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool.go
224 lines (178 loc) · 4.22 KB
/
pool.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
package runtime
import (
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/artela-network/aspect-runtime/types"
"github.com/google/uuid"
)
type (
Key string
Hash string
)
type Entry struct {
prev *Entry // point to prev entry
next *Entry // point to next entry
sprev *Entry // for sub list
snext *Entry // for sub list
key Key // build with id:hash
runtime types.AspectRuntime
}
func (e *Entry) destroy() {
if e.runtime != nil {
e.runtime.Destroy()
}
}
type EntryList struct {
sync.Mutex
head *Entry
tail *Entry
subs map[Hash]*EntryList
cap int
len int
}
func NewEntryList(cap int) *EntryList {
head := &Entry{}
tail := &Entry{}
head.next = tail
tail.prev = head
head.snext = tail
tail.sprev = head
return &EntryList{
head: head,
tail: tail,
subs: make(map[Hash]*EntryList),
len: 0,
cap: cap,
}
}
func (list *EntryList) Len() int {
list.Lock()
defer list.Unlock()
return list.len
}
func (list *EntryList) PushFront(entry *Entry) {
list.Lock()
defer list.Unlock()
if list.len >= list.cap {
end := list.tail.prev
list.remove(end)
go end.destroy()
}
list.len++
entry.next = list.head.next
entry.prev = list.head
entry.prev.next = entry
entry.next.prev = entry
key := entry.key
_, hash := split(key)
sub, ok := list.subs[hash]
if !ok {
sub = NewEntryList(list.cap)
list.subs[hash] = sub
}
sub.len++
entry.snext = sub.head.snext
entry.sprev = sub.head
entry.sprev.snext = entry
entry.snext.sprev = entry
}
func (list *EntryList) remove(entry *Entry) {
list.len--
entry.prev.next = entry.next
entry.next.prev = entry.prev
entry.sprev.snext = entry.snext
entry.snext.sprev = entry.sprev
key := entry.key
_, hash := split(key)
sub := list.subs[hash]
sub.len--
if sub.len == 0 {
delete(list.subs, hash)
}
}
func (list *EntryList) PopFront(hash Hash) (*Entry, bool) {
list.Lock()
defer list.Unlock()
if sub, ok := list.subs[hash]; ok && sub.len > 0 {
entry := sub.head.snext
list.remove(entry)
return entry, true
}
return nil, false
}
type RuntimePool struct {
sync.Mutex
cache *EntryList
logger types.Logger
ctx context.Context
}
func NewRuntimePool(ctx context.Context, logger types.Logger, capacity int) *RuntimePool {
return &RuntimePool{
cache: NewEntryList(capacity),
logger: logger,
ctx: ctx,
}
}
func (pool *RuntimePool) Len() int {
return pool.cache.Len()
}
func (pool *RuntimePool) Runtime(ctx context.Context, rtType RuntimeType, code []byte, apis *types.HostAPIRegistry) (string, types.AspectRuntime, error) {
startTime := time.Now()
hash := hashOfRuntimeArgs(rtType, code)
key, rt, err := pool.get(hash)
if err == nil && rt.ResetStore(ctx, apis) == nil {
pool.logger.Debug("runtime pool cache hit", "duration", time.Since(startTime).String(),
"hash", hash, "key", key)
return string(key), rt, nil
}
rt, err = NewAspectRuntime(pool.ctx, pool.logger, rtType, code, apis)
if err != nil {
return "", nil, err
}
id := uuid.New()
keyStr := join(id.String(), hash)
pool.logger.Debug("runtime pool cache miss", "duration", time.Since(startTime).String(),
"hash", hash, "key", keyStr)
return keyStr, rt, nil
}
func (pool *RuntimePool) get(hash Hash) (Key, types.AspectRuntime, error) {
entry, ok := pool.cache.PopFront(hash)
if !ok {
return "", nil, errors.New("not found")
}
return entry.key, entry.runtime, nil
}
// Return returns a runtime to the pool
func (pool *RuntimePool) Return(key string, runtime types.AspectRuntime) {
// free the hostapis and ctx injected to types, in case that go runtime GC failed
pool.logger.Debug("returning runtime", "key", key)
runtime.Reset()
pool.logger.Debug("runtime destroyed", "key", key)
entry := &Entry{
key: Key(key),
runtime: runtime,
}
pool.cache.PushFront(entry)
pool.logger.Debug("runtime returned", "key", key)
}
func hashOfRuntimeArgs(runtimeType RuntimeType, code []byte) Hash {
h := sha1.New()
var rttype [1]byte
rttype[0] = byte(runtimeType)
h.Write(rttype[:])
h.Write(code)
return Hash(hex.EncodeToString(h.Sum(nil)))
}
func join(id string, hash Hash) string {
return fmt.Sprintf("%s:%s", id, hash)
}
func split(key Key) (string, Hash) {
s := strings.Split(string(key), ":")
return s[0], Hash(s[1])
}