-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataloader.go.gotmpl
251 lines (209 loc) · 6.04 KB
/
dataloader.go.gotmpl
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
{{- /*
Accept data:
Key: type without import path, can be slice or pointer.
KeyImport: key type import path,.
Value: type without import path, can be slice or pointer.
ValueImport: value type import path,.
*/ -}}
{{- $key := .Key }}
{{- $value := .Value}}
{{- $isSlice := hasPrefix "[]" $value }}
{{- if $isSlice }}
{{- $value = substr 2 -1 $value }}
{{- end }}
{{- $isPointer := hasPrefix "*" $value }}
{{- if $isPointer }}
{{- $value = substr 1 -1 $value }}
{{- end }}
{{- if .KeyImport }}
{{- $key = printf "pkg1.%s" $key }}
{{- end }}
{{- if .ValueImport }}
{{- $value = printf "pkg2.%s" $value }}
{{- end }}
{{- if $isPointer }}
{{- $value = printf "*%s" $value }}
{{- end }}
{{- if $isSlice }}
{{- $value = printf "[]%s" $value }}
{{- end -}}
// Code generated from {{ templateFiles }}, DO NOT EDIT.
// referenced https://github.com/vektah/dataloaden/blob/master/pkg/generator/template.go
package {{.Package}}
import (
"sync"
"time"
{{- if or .KeyImport .ValueImport }}
{{ end }}
{{- with .KeyImport}}
pkg1 "{{.}}"
{{- end }}
{{- with .ValueImport }}
pkg2 "{{.}}"
{{- end }}
)
type {{.Name|lowerFirst}}Batch struct {
keys []{{ $key }}
data []{{ $value }}
error []error
closing bool
done chan struct{}
}
// {{.Name}} batches and caches requests
type {{.Name}} struct {
// this method provides the data for the loader
fetch func(keys []{{ $key }}) ([]{{ $value }}, []error)
// how long to done before sending a batch
wait time.Duration
// this will limit the maximum number of keys to send in one batch, 0 = no limit
maxBatch int
// INTERNAL
// lazily created cache
cache map[{{ $key }}]{{ $value }}
// the current batch. keys will continue to be collected until timeout is hit,
// then everything will be sent to the fetch method and out to the listeners
batch *{{.Name|lowerFirst}}Batch
// mutex to prevent races
mu sync.Mutex
}
// Load a {{.Value}} by key, batching and caching will be applied automatically
func (l *{{.Name}}) Load(key {{ $key }}) ({{ $value }}, error) {
return l.LoadThunk(key)()
}
// LoadThunk returns a function that when called will block waiting for a {{.Value}}.
// This method should be used if you want one goroutine to make requests to many
// different data loaders without blocking until the thunk is called.
func (l *{{.Name}}) LoadThunk(key {{ $key }}) func() ({{ $value }}, error) {
l.mu.Lock()
defer l.mu.Unlock()
if it, ok := l.cache[key]; ok {
return func() ({{ $value }}, error) {
return it, nil
}
}
if l.batch == nil {
l.batch = &{{.Name|lowerFirst}}Batch{done: make(chan struct{})}
}
batch := l.batch
pos := batch.keyIndex(l, key)
return func() ({{ $value }}, error) {
<-batch.done
var data {{ $value }}
if pos < len(batch.data) {
data = batch.data[pos]
}
var err error
// its convenient to be able to return a single error for everything
if len(batch.error) == 1 {
err = batch.error[0]
} else if batch.error != nil {
err = batch.error[pos]
}
if err == nil {
l.mu.Lock()
l.unsafeSet(key, data)
l.mu.Unlock()
}
return data, err
}
}
// LoadAll fetches many keys at once. It will be broken into appropriate sized
// sub batches depending on how the loader is configured
func (l *{{.Name}}) LoadAll(keys []{{ $key }}) ([]{{ $value }}, []error) {
results := make([]func() ({{ $value }}, error), len(keys))
for i, key := range keys {
results[i] = l.LoadThunk(key)
}
ret := make([]{{ $value }}, len(keys))
errors := make([]error, len(keys))
for i, thunk := range results {
ret[i], errors[i] = thunk()
}
return ret, errors
}
// LoadAllThunk returns a function that when called will block waiting for many {{$value}}.
// This method should be used if you want one goroutine to make requests to many
// different data loaders without blocking until the thunk is called.
func (l *{{.Name}}) LoadAllThunk(keys []{{ $key }}) func() ([]{{ $value }}, []error) {
results := make([]func() ({{ $value }}, error), len(keys))
for i, key := range keys {
results[i] = l.LoadThunk(key)
}
return func() ([]{{ $value }}, []error) {
ret := make([]{{ $value }}, len(keys))
errors := make([]error, len(keys))
for i, thunk := range results {
ret[i], errors[i] = thunk()
}
return ret, errors
}
}
// Set the cache with the provided key and value.
// It is the caller's responsibility to avoid pass same pointer from a loop.
func (l *{{.Name}}) Set(key {{ $key }}, value {{ $value }}) {
l.mu.Lock()
defer l.mu.Unlock()
l.unsafeSet(key, value)
}
// Prime the cache with the provided key and value. If the key already exists, no change is made
// and false is returned.
// It is the caller's responsibility to avoid pass same pointer from a loop.
// (To forcefully prime the cache, use Set.)
func (l *{{.Name}}) Prime(key {{ $key }}, value {{ $value }}) bool {
l.mu.Lock()
defer l.mu.Unlock()
var found bool
if _, found = l.cache[key]; !found {
l.unsafeSet(key, value)
}
return !found
}
// Clear the value at key from the cache, if it exists
func (l *{{.Name}}) Clear(key {{ $key }}) {
l.mu.Lock()
defer l.mu.Unlock()
delete(l.cache, key)
}
func (l *{{.Name}}) unsafeSet(key {{ $key }}, value {{ $value }}) {
if l.cache == nil {
l.cache = map[{{ $key }}]{{ $value }}{}
}
l.cache[key] = value
}
// keyIndex will return the location of the key in the batch, if its not found
// it will add the key to the batch
func (b *{{.Name|lowerFirst}}Batch) keyIndex(l *{{.Name}}, key {{ $key }}) int {
for i, existingKey := range b.keys {
if key == existingKey {
return i
}
}
pos := len(b.keys)
b.keys = append(b.keys, key)
if pos == 0 {
go b.startTimer(l)
}
if l.maxBatch != 0 && pos >= l.maxBatch-1 {
if !b.closing {
b.closing = true
l.batch = nil
go b.end(l)
}
}
return pos
}
func (b *{{.Name|lowerFirst}}Batch) startTimer(l *{{.Name}}) {
time.Sleep(l.wait)
l.mu.Lock()
defer l.mu.Unlock()
// we must have hit a batch limit and are already finalizing this batch
if b.closing {
return
}
l.batch = nil
b.end(l)
}
func (b *{{.Name|lowerFirst}}Batch) end(l *{{.Name}}) {
b.data, b.error = l.fetch(b.keys)
close(b.done)
}