forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rerunner.go
397 lines (327 loc) · 8.64 KB
/
rerunner.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
package reactive
import (
"context"
"errors"
"sync"
"time"
)
var (
// Sentinel error to tell the rerunner to not dump the current
// computation cache and let the error'd function retry.
RetrySentinelError = errors.New("retry")
// WriteThenReadDelay is how long to wait after hearing a change
// was made, before reading that change by rerunning.
WriteThenReadDelay = 200 * time.Millisecond
)
// locker is a collection of mutexes indexed by arbitrary keys
type locker struct {
mu sync.Mutex
m map[interface{}]*lock
}
// newLocker creates a new locker instance.
func newLocker() *locker {
return &locker{
m: make(map[interface{}]*lock),
}
}
// lock is a single mutex in a locker
type lock struct {
ref int
mu sync.Mutex
}
// Lock locks a locker by (optionally) allocating, increasing the ref count,
// and locking
func (l *locker) Lock(k interface{}) {
l.mu.Lock()
m, ok := l.m[k]
if !ok {
m = new(lock)
l.m[k] = m
}
m.ref++
l.mu.Unlock()
m.mu.Lock()
}
// Unlock unlocks a locker by unlocking, decreasing the ref count, and
// (optionally) deleting
func (l *locker) Unlock(k interface{}) {
l.mu.Lock()
m := l.m[k]
m.mu.Unlock()
m.ref--
if m.ref == 0 {
delete(l.m, k)
}
l.mu.Unlock()
}
type computation struct {
node node
value interface{}
}
// cache caches computations
type cache struct {
mu sync.Mutex
locker *locker
computations map[interface{}]*computation
}
func (c *cache) get(key interface{}) *computation {
c.mu.Lock()
defer c.mu.Unlock()
return c.computations[key]
}
// set adds a computation to the cache for the given key
func (c *cache) set(key interface{}, computation *computation) {
c.mu.Lock()
defer c.mu.Unlock()
if c.computations[key] == nil {
c.computations[key] = computation
}
}
func (c *cache) cleanInvalidated() {
c.mu.Lock()
defer c.mu.Unlock()
for key, computation := range c.computations {
if computation.node.Invalidated() {
delete(c.computations, key)
}
}
}
// Resource represents a leaf-level dependency in a computation
type Resource struct {
node
}
// NewResource creates a new Resource
func NewResource() *Resource {
return &Resource{
node: node{},
}
}
// Invalidate permanently invalidates r
func (r *Resource) Invalidate() {
go r.invalidate()
}
// Store invalidates all computations currently depending on r
func (r *Resource) Strobe() {
go r.strobe()
}
// Cleanup registers a handler to be called when all computations using r stop
//
// NOTE: For f to be called, at least one computation must AddDependency r!
func (r *Resource) Cleanup(f func()) {
r.node.handleRelease(f)
}
type computationKey struct{}
type cacheKey struct{}
type dependencySetKey struct{}
type dependencySet struct {
mu sync.Mutex
dependencies []Dependency
}
func (ds *dependencySet) add(dep Dependency) {
ds.mu.Lock()
defer ds.mu.Unlock()
ds.dependencies = append(ds.dependencies, dep)
}
func (ds *dependencySet) get() []Dependency {
ds.mu.Lock()
defer ds.mu.Unlock()
return ds.dependencies
}
type Dependency interface{}
type DependencyCallbackFunc func(context.Context, Dependency)
type dependencyCallbackKey struct{}
func AddDependency(ctx context.Context, r *Resource, dep Dependency) {
if !HasRerunner(ctx) {
r.node.addOut(&node{released: true})
return
}
computation := ctx.Value(computationKey{}).(*computation)
r.node.addOut(&computation.node)
if dep != nil {
depSet, ok := ctx.Value(dependencySetKey{}).(*dependencySet)
if ok && depSet != nil {
depSet.add(dep)
}
if callback, ok := ctx.Value(dependencyCallbackKey{}).(DependencyCallbackFunc); ok && callback != nil {
callback(ctx, dep)
}
}
}
// WithDependencyCallback registers a callback that is invoked when
// AddDependency is called with non-nil serializable dependency.
func WithDependencyCallback(ctx context.Context, f DependencyCallbackFunc) context.Context {
return context.WithValue(ctx, dependencyCallbackKey{}, f)
}
func Dependencies(ctx context.Context) []Dependency {
depSet := ctx.Value(dependencySetKey{}).(*dependencySet)
if depSet == nil {
return nil
}
return depSet.get()
}
type ComputeFunc func(context.Context) (interface{}, error)
func run(ctx context.Context, f ComputeFunc) (*computation, error) {
// build result computation and local computation Ctx
c := &computation{
// this node will be freed either when the computation fails, or by our
// caller
node: node{},
}
childCtx := context.WithValue(ctx, computationKey{}, c)
// Compute f and write the results to the c
value, err := f(childCtx)
if err != nil {
go c.node.release()
return nil, err
}
c.value = value
return c, nil
}
func Cache(ctx context.Context, key interface{}, f ComputeFunc) (interface{}, error) {
if !HasRerunner(ctx) {
val, err := f(ctx)
return val, err
}
cache := ctx.Value(cacheKey{}).(*cache)
computation := ctx.Value(computationKey{}).(*computation)
cache.locker.Lock(key)
defer cache.locker.Unlock(key)
if child := cache.get(key); child != nil {
child.node.addOut(&computation.node)
return child.value, nil
}
child, err := run(ctx, f)
if err != nil {
return nil, err
}
cache.set(key, child)
child.node.addOut(&computation.node)
return child.value, nil
}
// Rerunner automatically reruns a computation whenever its dependencies
// change.
//
// The computation stops when it returns an error or after calling Stop. There
// is no way to get the output value from a computation. Instead, the
// computation should communicate its result before returning.
type Rerunner struct {
ctx context.Context
cancelCtx context.CancelFunc
f ComputeFunc
cache *cache
minRerunInterval time.Duration
retryDelay time.Duration
// flushed tracks if the next computation should run without delay. It is set
// to false as soon as the next computation starts. flushCh is closed when
// flushed is set to true.
flushMu sync.Mutex
flushCh chan struct{}
flushed bool
mu sync.Mutex
computation *computation
stop bool
lastRun time.Time
}
// NewRerunner runs f continuously
func NewRerunner(ctx context.Context, f ComputeFunc, minRerunInterval time.Duration) *Rerunner {
ctx, cancelCtx := context.WithCancel(ctx)
r := &Rerunner{
ctx: ctx,
cancelCtx: cancelCtx,
f: f,
cache: &cache{
computations: make(map[interface{}]*computation),
locker: newLocker(),
},
minRerunInterval: minRerunInterval,
retryDelay: minRerunInterval,
flushCh: make(chan struct{}, 0),
}
go r.run()
return r
}
// RerunImmediately removes the delay from the next recomputation.
func (r *Rerunner) RerunImmediately() {
r.flushMu.Lock()
defer r.flushMu.Unlock()
if !r.flushed {
close(r.flushCh)
r.flushed = true
}
}
// run performs an actual computation
func (r *Rerunner) run() {
// Wait for the minimum rerun interval. Exit early if the computation is stopped.
delta := r.retryDelay - time.Now().Sub(r.lastRun)
t := time.NewTimer(delta)
select {
case <-r.ctx.Done():
case <-t.C:
case <-r.flushCh:
}
t.Stop()
if r.ctx.Err() != nil {
return
}
r.flushMu.Lock()
if r.flushed {
r.flushCh = make(chan struct{}, 0)
r.flushed = false
}
r.flushMu.Unlock()
r.mu.Lock()
defer r.mu.Unlock()
// Bail out if the computation has been stopped.
if r.stop {
return
}
if !r.lastRun.IsZero() {
// Delay the rerun in order to emulate write-then-read consistency.
time.Sleep(WriteThenReadDelay)
}
r.cache.cleanInvalidated()
ctx := context.WithValue(r.ctx, cacheKey{}, r.cache)
ctx = context.WithValue(ctx, dependencySetKey{}, &dependencySet{})
computation, err := run(ctx, r.f)
r.lastRun = time.Now()
if err != nil {
if err == RetrySentinelError {
r.retryDelay = r.retryDelay * 2
// Max out the retry delay to at 1 minute.
if r.retryDelay > time.Minute {
r.retryDelay = time.Minute
}
go r.run()
} else {
// If we encountered an error that is not the retry sentinel,
// we should stop the rerunner.
return
}
} else {
// If we succeeded in the computation, we can release the old computation
// and reset the retry delay.
if r.computation != nil {
go r.computation.node.release()
r.computation = nil
}
r.computation = computation
r.retryDelay = r.minRerunInterval
// Schedule a rerun whenever our node becomes invalidated (which might already
// have happened!)
computation.node.handleInvalidate(r.run)
}
}
func (r *Rerunner) Stop() {
// Call cancelCtx before acquiring the lock as the lock might be held for a long time during a running computation.
r.cancelCtx()
r.mu.Lock()
r.stop = true
if r.computation != nil {
go r.computation.node.release()
r.computation = nil
}
r.mu.Unlock()
}
func HasRerunner(ctx context.Context) bool {
return ctx.Value(computationKey{}) != nil
}