-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataloader.go
208 lines (168 loc) · 4.46 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
package dataloader
import (
"context"
"time"
)
type DataLoader[K any, V any, C comparable] interface {
Load(context.Context, K) *Thunk[V]
LoadMany(context.Context, []K) []*Thunk[V]
Clear(context.Context, K) DataLoader[K, V, C]
ClearAll(ctx context.Context) DataLoader[K, V, C]
Prime(context.Context, K, V) DataLoader[K, V, C]
Dispatch()
}
type Result[V any] struct {
Value V
Error error
}
type BatchLoadFn[K any, V any] func(context.Context, []K) []Result[V]
type BatchScheduleFn func(ctx context.Context, batch Batch, callback func())
type CacheKeyFn[K any, C comparable] func(ctx context.Context, key K) (C, error)
func New[K any, V any, C comparable](ctx context.Context, batchLoadFn BatchLoadFn[K, V], options ...option[K, V, C]) DataLoader[K, V, C] {
l := &loader[K, V, C]{
ctx: ctx,
batches: make(chan []*batch[K, V], 1),
batchLoadFn: batchLoadFn,
batchScheduleFn: NewTimeWindowScheduler(16 * time.Millisecond),
cacheKeyFn: NewMirrorCacheKey[K, C](),
cacheMap: make(chan CacheMap[C, *Thunk[V]], 1),
maxBatchSize: 100,
}
l.cacheMap <- NewInMemoryCache[C, *Thunk[V]]()
l.batches <- []*batch[K, V]{}
for _, option := range options {
option(l)
}
return l
}
type loader[K any, V any, C comparable] struct {
ctx context.Context
batches chan []*batch[K, V]
batchLoadFn BatchLoadFn[K, V]
batchScheduleFn BatchScheduleFn
cacheKeyFn CacheKeyFn[K, C]
cacheMap chan CacheMap[C, *Thunk[V]]
maxBatchSize int
}
type batch[K any, V any] struct {
full chan struct{}
dispatch chan struct{}
keys []K
thunks []*Thunk[V]
}
func (b *batch[K, V]) Full() <-chan struct{} {
return b.full
}
func (b *batch[K, V]) Dispatch() <-chan struct{} {
return b.dispatch
}
type Batch interface {
Full() <-chan struct{}
Dispatch() <-chan struct{}
}
func (l *loader[K, V, C]) Load(ctx context.Context, key K) *Thunk[V] {
cacheKey, err := l.cacheKeyFn(ctx, key)
if err != nil {
thunk := NewThunk[V]()
thunk.error(ctx, err)
return thunk
}
cacheMap := <-l.cacheMap
cached, err := cacheMap.Get(ctx, cacheKey)
if err != nil {
l.cacheMap <- cacheMap
thunk := NewThunk[V]()
thunk.error(ctx, err)
return thunk
}
if cached != nil {
l.cacheMap <- cacheMap
return cached
}
thunk := NewThunk[V]()
err = cacheMap.Set(ctx, cacheKey, thunk)
if err != nil {
l.cacheMap <- cacheMap
thunk.error(ctx, err)
return thunk
}
l.cacheMap <- cacheMap
batches := <-l.batches
if len(batches) == 0 || len(batches[len(batches)-1].keys) >= l.maxBatchSize {
b := &batch[K, V]{
full: make(chan struct{}),
dispatch: make(chan struct{}),
keys: []K{},
thunks: []*Thunk[V]{},
}
batches = append(batches, b)
go l.batchScheduleFn(l.ctx, b, l.dispatch)
}
bat := batches[len(batches)-1]
bat.keys = append(bat.keys, key)
bat.thunks = append(bat.thunks, thunk)
if len(batches) != 0 && len(batches[len(batches)-1].keys) >= l.maxBatchSize {
close(batches[len(batches)-1].full)
}
l.batches <- batches
return thunk
}
func (l *loader[K, V, C]) LoadMany(ctx context.Context, keys []K) []*Thunk[V] {
thunks := make([]*Thunk[V], len(keys))
for index, key := range keys {
thunks[index] = l.Load(ctx, key)
}
return thunks
}
func (l *loader[K, V, C]) Dispatch() {
batches := <-l.batches
for _, batch := range batches {
select {
case <-batch.full:
case <-batch.dispatch:
default:
close(batch.dispatch)
}
}
l.batches <- batches
}
func (l *loader[K, V, C]) dispatch() {
ctx := l.ctx
batches := <-l.batches
if len(batches) == 0 {
l.batches <- batches
return
}
batch := batches[0]
l.batches <- batches[1:]
results := l.batchLoadFn(ctx, batch.keys)
for index, res := range results {
if res.Error != nil {
batch.thunks[index].error(ctx, res.Error)
} else {
batch.thunks[index].set(ctx, res.Value)
}
}
}
func (l *loader[K, V, C]) Clear(ctx context.Context, key K) DataLoader[K, V, C] {
cacheKey, _ := l.cacheKeyFn(ctx, key)
cacheMap := <-l.cacheMap
cacheMap.Delete(ctx, cacheKey)
l.cacheMap <- cacheMap
return l
}
func (l *loader[K, V, C]) ClearAll(ctx context.Context) DataLoader[K, V, C] {
cacheMap := <-l.cacheMap
cacheMap.Clear(ctx)
l.cacheMap <- cacheMap
return l
}
func (l *loader[K, V, C]) Prime(ctx context.Context, key K, value V) DataLoader[K, V, C] {
cacheKey, _ := l.cacheKeyFn(ctx, key)
thunk := NewThunk[V]()
thunk.set(ctx, value)
cacheMap := <-l.cacheMap
cacheMap.Set(ctx, cacheKey, thunk)
l.cacheMap <- cacheMap
return l
}