-
Notifications
You must be signed in to change notification settings - Fork 75
/
dataloader.go
505 lines (427 loc) · 13.1 KB
/
dataloader.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Package dataloader is an implementation of facebook's dataloader in go.
// See https://github.com/facebook/dataloader for more information
package dataloader
import (
"context"
"errors"
"fmt"
"log"
"runtime"
"sync"
"time"
)
// Interface is a `DataLoader` Interface which defines a public API for loading data from a particular
// data back-end with unique keys such as the `id` column of a SQL table or
// document name in a MongoDB database, given a batch loading function.
//
// Each `DataLoader` instance should contain a unique memoized cache. Use caution when
// used in long-lived applications or those which serve many users with
// different access permissions and consider creating a new instance per
// web request.
type Interface[K comparable, V any] interface {
Load(context.Context, K) Thunk[V]
LoadMany(context.Context, []K) ThunkMany[V]
Clear(context.Context, K) Interface[K, V]
ClearAll() Interface[K, V]
Prime(ctx context.Context, key K, value V) Interface[K, V]
}
// BatchFunc is a function, which when given a slice of keys (string), returns a slice of `results`.
// It's important that the length of the input keys matches the length of the output results.
//
// The keys passed to this function are guaranteed to be unique
type BatchFunc[K comparable, V any] func(context.Context, []K) []*Result[V]
// Result is the data structure that a BatchFunc returns.
// It contains the resolved data, and any errors that may have occurred while fetching the data.
type Result[V any] struct {
Data V
Error error
}
// ResultMany is used by the LoadMany method.
// It contains a list of resolved data and a list of errors.
// The lengths of the data list and error list will match, and elements at each index correspond to each other.
type ResultMany[V any] struct {
Data []V
Error []error
}
// PanicErrorWrapper wraps the error interface.
// This is used to check if the error is a panic error.
// We should not cache panic errors.
type PanicErrorWrapper struct {
panicError error
}
func (p *PanicErrorWrapper) Error() string {
return p.panicError.Error()
}
// Loader implements the dataloader.Interface.
type Loader[K comparable, V any] struct {
// the batch function to be used by this loader
batchFn BatchFunc[K, V]
// the maximum batch size. Set to 0 if you want it to be unbounded.
batchCap int
// the internal cache. This packages contains a basic cache implementation but any custom cache
// implementation could be used as long as it implements the `Cache` interface.
cacheLock sync.Mutex
cache Cache[K, V]
// should we clear the cache on each batch?
// this would allow batching but no long term caching
clearCacheOnBatch bool
// count of queued up items
count int
// the maximum input queue size. Set to 0 if you want it to be unbounded.
inputCap int
// the amount of time to wait before triggering a batch
wait time.Duration
// lock to protect the batching operations
batchLock sync.Mutex
// current batcher
curBatcher *batcher[K, V]
// used to close the sleeper of the current batcher
endSleeper chan bool
// used by tests to prevent logs
silent bool
// can be set to trace calls to dataloader
tracer Tracer[K, V]
}
// Thunk is a function that will block until the value (*Result) it contains is resolved.
// After the value it contains is resolved, this function will return the result.
// This function can be called many times, much like a Promise is other languages.
// The value will only need to be resolved once so subsequent calls will return immediately.
type Thunk[V any] func() (V, error)
// ThunkMany is much like the Thunk func type but it contains a list of results.
type ThunkMany[V any] func() ([]V, []error)
// type used to on input channel
type batchRequest[K comparable, V any] struct {
key K
channel chan *Result[V]
}
// Option allows for configuration of Loader fields.
type Option[K comparable, V any] func(*Loader[K, V])
// WithCache sets the BatchedLoader cache. Defaults to InMemoryCache if a Cache is not set.
func WithCache[K comparable, V any](c Cache[K, V]) Option[K, V] {
return func(l *Loader[K, V]) {
l.cache = c
}
}
// WithBatchCapacity sets the batch capacity. Default is 0 (unbounded).
func WithBatchCapacity[K comparable, V any](c int) Option[K, V] {
return func(l *Loader[K, V]) {
l.batchCap = c
}
}
// WithInputCapacity sets the input capacity. Default is 1000.
func WithInputCapacity[K comparable, V any](c int) Option[K, V] {
return func(l *Loader[K, V]) {
l.inputCap = c
}
}
// WithWait sets the amount of time to wait before triggering a batch.
// Default duration is 16 milliseconds.
func WithWait[K comparable, V any](d time.Duration) Option[K, V] {
return func(l *Loader[K, V]) {
l.wait = d
}
}
// WithClearCacheOnBatch allows batching of items but no long term caching.
// It accomplishes this by clearing the cache after each batch operation.
func WithClearCacheOnBatch[K comparable, V any]() Option[K, V] {
return func(l *Loader[K, V]) {
l.cacheLock.Lock()
l.clearCacheOnBatch = true
l.cacheLock.Unlock()
}
}
// withSilentLogger turns of log messages. It's used by the tests
func withSilentLogger[K comparable, V any]() Option[K, V] {
return func(l *Loader[K, V]) {
l.silent = true
}
}
// WithTracer allows tracing of calls to Load and LoadMany
func WithTracer[K comparable, V any](tracer Tracer[K, V]) Option[K, V] {
return func(l *Loader[K, V]) {
l.tracer = tracer
}
}
// NewBatchedLoader constructs a new Loader with given options.
func NewBatchedLoader[K comparable, V any](batchFn BatchFunc[K, V], opts ...Option[K, V]) *Loader[K, V] {
loader := &Loader[K, V]{
batchFn: batchFn,
inputCap: 1000,
wait: 16 * time.Millisecond,
}
// Apply options
for _, apply := range opts {
apply(loader)
}
// Set defaults
if loader.cache == nil {
loader.cache = NewCache[K, V]()
}
if loader.tracer == nil {
loader.tracer = NoopTracer[K, V]{}
}
return loader
}
// Load load/resolves the given key, returning a channel that will contain the value and error.
// The first context passed to this function within a given batch window will be provided to
// the registered BatchFunc.
func (l *Loader[K, V]) Load(originalContext context.Context, key K) Thunk[V] {
ctx, finish := l.tracer.TraceLoad(originalContext, key)
c := make(chan *Result[V], 1)
var result struct {
mu sync.RWMutex
value *Result[V]
}
// lock to prevent duplicate keys coming in before item has been added to cache.
l.cacheLock.Lock()
if v, ok := l.cache.Get(ctx, key); ok {
defer finish(v)
defer l.cacheLock.Unlock()
return v
}
thunk := func() (V, error) {
result.mu.RLock()
resultNotSet := result.value == nil
result.mu.RUnlock()
if resultNotSet {
result.mu.Lock()
if v, ok := <-c; ok {
result.value = v
}
result.mu.Unlock()
}
result.mu.RLock()
defer result.mu.RUnlock()
var ev *PanicErrorWrapper
if result.value.Error != nil && errors.As(result.value.Error, &ev) {
l.Clear(ctx, key)
}
return result.value.Data, result.value.Error
}
defer finish(thunk)
l.cache.Set(ctx, key, thunk)
l.cacheLock.Unlock()
// this is sent to batch fn. It contains the key and the channel to return
// the result on
req := &batchRequest[K, V]{key, c}
l.batchLock.Lock()
// start the batch window if it hasn't already started.
if l.curBatcher == nil {
l.curBatcher = l.newBatcher(l.silent, l.tracer)
// start the current batcher batch function
go l.curBatcher.batch(originalContext)
// start a sleeper for the current batcher
l.endSleeper = make(chan bool)
go l.sleeper(l.curBatcher, l.endSleeper)
}
l.curBatcher.input <- req
// if we need to keep track of the count (max batch), then do so.
if l.batchCap > 0 {
l.count++
// if we hit our limit, force the batch to start
if l.count == l.batchCap {
// end the batcher synchronously here because another call to Load
// may concurrently happen and needs to go to a new batcher.
l.curBatcher.end()
// end the sleeper for the current batcher.
// this is to stop the goroutine without waiting for the
// sleeper timeout.
close(l.endSleeper)
l.reset()
}
}
l.batchLock.Unlock()
return thunk
}
// LoadMany loads multiple keys, returning a thunk (type: ThunkMany) that will resolve the keys passed in.
func (l *Loader[K, V]) LoadMany(originalContext context.Context, keys []K) ThunkMany[V] {
ctx, finish := l.tracer.TraceLoadMany(originalContext, keys)
var (
length = len(keys)
data = make([]V, length)
errors = make([]error, length)
c = make(chan *ResultMany[V], 1)
wg sync.WaitGroup
)
resolve := func(ctx context.Context, i int) {
defer wg.Done()
thunk := l.Load(ctx, keys[i])
result, err := thunk()
data[i] = result
errors[i] = err
}
wg.Add(length)
for i := range keys {
go resolve(ctx, i)
}
go func() {
wg.Wait()
// errs is nil unless there exists a non-nil error.
// This prevents dataloader from returning a slice of all-nil errors.
var errs []error
for _, e := range errors {
if e != nil {
errs = errors
break
}
}
c <- &ResultMany[V]{Data: data, Error: errs}
close(c)
}()
var result struct {
mu sync.RWMutex
value *ResultMany[V]
}
thunkMany := func() ([]V, []error) {
result.mu.RLock()
resultNotSet := result.value == nil
result.mu.RUnlock()
if resultNotSet {
result.mu.Lock()
if v, ok := <-c; ok {
result.value = v
}
result.mu.Unlock()
}
result.mu.RLock()
defer result.mu.RUnlock()
return result.value.Data, result.value.Error
}
defer finish(thunkMany)
return thunkMany
}
// Clear clears the value at `key` from the cache, it it exists. Returns self for method chaining
func (l *Loader[K, V]) Clear(ctx context.Context, key K) Interface[K, V] {
l.cacheLock.Lock()
l.cache.Delete(ctx, key)
l.cacheLock.Unlock()
return l
}
// ClearAll clears the entire cache. To be used when some event results in unknown invalidations.
// Returns self for method chaining.
func (l *Loader[K, V]) ClearAll() Interface[K, V] {
l.cacheLock.Lock()
l.cache.Clear()
l.cacheLock.Unlock()
return l
}
// Prime adds the provided key and value to the cache. If the key already exists, no change is made.
// Returns self for method chaining
func (l *Loader[K, V]) Prime(ctx context.Context, key K, value V) Interface[K, V] {
if _, ok := l.cache.Get(ctx, key); !ok {
thunk := func() (V, error) {
return value, nil
}
l.cache.Set(ctx, key, thunk)
}
return l
}
func (l *Loader[K, V]) reset() {
l.count = 0
l.curBatcher = nil
if l.clearCacheOnBatch {
l.cache.Clear()
}
}
type batcher[K comparable, V any] struct {
input chan *batchRequest[K, V]
batchFn BatchFunc[K, V]
finished bool
silent bool
tracer Tracer[K, V]
}
// newBatcher returns a batcher for the current requests
// all the batcher methods must be protected by a global batchLock
func (l *Loader[K, V]) newBatcher(silent bool, tracer Tracer[K, V]) *batcher[K, V] {
return &batcher[K, V]{
input: make(chan *batchRequest[K, V], l.inputCap),
batchFn: l.batchFn,
silent: silent,
tracer: tracer,
}
}
// stop receiving input and process batch function
func (b *batcher[K, V]) end() {
if !b.finished {
close(b.input)
b.finished = true
}
}
// execute the batch of all items in queue
func (b *batcher[K, V]) batch(originalContext context.Context) {
var (
keys = make([]K, 0)
reqs = make([]*batchRequest[K, V], 0)
items = make([]*Result[V], 0)
panicErr interface{}
)
for item := range b.input {
keys = append(keys, item.key)
reqs = append(reqs, item)
}
ctx, finish := b.tracer.TraceBatch(originalContext, keys)
defer finish(items)
func() {
defer func() {
if r := recover(); r != nil {
panicErr = r
if b.silent {
return
}
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
log.Printf("Dataloader: Panic received in batch function: %v\n%s", panicErr, buf)
}
}()
items = b.batchFn(ctx, keys)
}()
if panicErr != nil {
for _, req := range reqs {
req.channel <- &Result[V]{Error: &PanicErrorWrapper{panicError: fmt.Errorf("Panic received in batch function: %v", panicErr)}}
close(req.channel)
}
return
}
if len(items) != len(keys) {
err := &Result[V]{Error: fmt.Errorf(`
The batch function supplied did not return an array of responses
the same length as the array of keys.
Keys:
%v
Values:
%v
`, keys, items)}
for _, req := range reqs {
req.channel <- err
close(req.channel)
}
return
}
for i, req := range reqs {
req.channel <- items[i]
close(req.channel)
}
}
// wait the appropriate amount of time for the provided batcher
func (l *Loader[K, V]) sleeper(b *batcher[K, V], close chan bool) {
select {
// used by batch to close early. usually triggered by max batch size
case <-close:
return
// this will move this goroutine to the back of the callstack?
case <-time.After(l.wait):
}
// reset
// this is protected by the batchLock to avoid closing the batcher input
// channel while Load is inserting a request
l.batchLock.Lock()
b.end()
// We can end here also if the batcher has already been closed and a
// new one has been created. So reset the loader state only if the batcher
// is the current one
if l.curBatcher == b {
l.reset()
}
l.batchLock.Unlock()
}