2020-06-02 15:45:21 +00:00
|
|
|
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
|
|
|
|
|
2021-05-10 21:15:55 +00:00
|
|
|
package dataloader
|
2020-06-02 15:45:21 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-05 17:32:45 +00:00
|
|
|
"github.com/tribalwarshelp/shared/tw/twmodel"
|
2020-06-02 15:45:21 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TribeLoaderConfig captures the config to create a new TribeLoader
|
|
|
|
type TribeLoaderConfig struct {
|
|
|
|
// Fetch is a method that provides the data for the loader
|
2021-05-05 17:32:45 +00:00
|
|
|
Fetch func(keys []int) ([]*twmodel.Tribe, []error)
|
2020-06-02 15:45:21 +00:00
|
|
|
|
|
|
|
// Wait is how long wait before sending a batch
|
|
|
|
Wait time.Duration
|
|
|
|
|
|
|
|
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
|
|
|
|
MaxBatch int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTribeLoader creates a new TribeLoader given a fetch, wait, and maxBatch
|
|
|
|
func NewTribeLoader(config TribeLoaderConfig) *TribeLoader {
|
|
|
|
return &TribeLoader{
|
|
|
|
fetch: config.Fetch,
|
|
|
|
wait: config.Wait,
|
|
|
|
maxBatch: config.MaxBatch,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TribeLoader batches and caches requests
|
|
|
|
type TribeLoader struct {
|
|
|
|
// this method provides the data for the loader
|
2021-05-05 17:32:45 +00:00
|
|
|
fetch func(keys []int) ([]*twmodel.Tribe, []error)
|
2020-06-02 15:45:21 +00:00
|
|
|
|
|
|
|
// 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
|
2021-05-05 17:32:45 +00:00
|
|
|
cache map[int]*twmodel.Tribe
|
2020-06-02 15:45:21 +00:00
|
|
|
|
|
|
|
// 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 *tribeLoaderBatch
|
|
|
|
|
|
|
|
// mutex to prevent races
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type tribeLoaderBatch struct {
|
|
|
|
keys []int
|
2021-05-05 17:32:45 +00:00
|
|
|
data []*twmodel.Tribe
|
2020-06-02 15:45:21 +00:00
|
|
|
error []error
|
|
|
|
closing bool
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load a Tribe by key, batching and caching will be applied automatically
|
2021-05-05 17:32:45 +00:00
|
|
|
func (l *TribeLoader) Load(key int) (*twmodel.Tribe, error) {
|
2020-06-02 15:45:21 +00:00
|
|
|
return l.LoadThunk(key)()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadThunk returns a function that when called will block waiting for a Tribe.
|
|
|
|
// 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.
|
2021-05-05 17:32:45 +00:00
|
|
|
func (l *TribeLoader) LoadThunk(key int) func() (*twmodel.Tribe, error) {
|
2020-06-02 15:45:21 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
if it, ok := l.cache[key]; ok {
|
|
|
|
l.mu.Unlock()
|
2021-05-05 17:32:45 +00:00
|
|
|
return func() (*twmodel.Tribe, error) {
|
2020-06-02 15:45:21 +00:00
|
|
|
return it, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l.batch == nil {
|
|
|
|
l.batch = &tribeLoaderBatch{done: make(chan struct{})}
|
|
|
|
}
|
|
|
|
batch := l.batch
|
|
|
|
pos := batch.keyIndex(l, key)
|
|
|
|
l.mu.Unlock()
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
return func() (*twmodel.Tribe, error) {
|
2020-06-02 15:45:21 +00:00
|
|
|
<-batch.done
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
var data *twmodel.Tribe
|
2020-06-02 15:45:21 +00:00
|
|
|
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
|
2021-05-05 17:32:45 +00:00
|
|
|
func (l *TribeLoader) LoadAll(keys []int) ([]*twmodel.Tribe, []error) {
|
|
|
|
results := make([]func() (*twmodel.Tribe, error), len(keys))
|
2020-06-02 15:45:21 +00:00
|
|
|
|
|
|
|
for i, key := range keys {
|
|
|
|
results[i] = l.LoadThunk(key)
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
tribes := make([]*twmodel.Tribe, len(keys))
|
2020-06-02 15:45:21 +00:00
|
|
|
errors := make([]error, len(keys))
|
|
|
|
for i, thunk := range results {
|
|
|
|
tribes[i], errors[i] = thunk()
|
|
|
|
}
|
|
|
|
return tribes, errors
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAllThunk returns a function that when called will block waiting for a Tribes.
|
|
|
|
// 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.
|
2021-05-05 17:32:45 +00:00
|
|
|
func (l *TribeLoader) LoadAllThunk(keys []int) func() ([]*twmodel.Tribe, []error) {
|
|
|
|
results := make([]func() (*twmodel.Tribe, error), len(keys))
|
2020-06-02 15:45:21 +00:00
|
|
|
for i, key := range keys {
|
|
|
|
results[i] = l.LoadThunk(key)
|
|
|
|
}
|
2021-05-05 17:32:45 +00:00
|
|
|
return func() ([]*twmodel.Tribe, []error) {
|
|
|
|
tribes := make([]*twmodel.Tribe, len(keys))
|
2020-06-02 15:45:21 +00:00
|
|
|
errors := make([]error, len(keys))
|
|
|
|
for i, thunk := range results {
|
|
|
|
tribes[i], errors[i] = thunk()
|
|
|
|
}
|
|
|
|
return tribes, errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
|
|
|
// and false is returned.
|
|
|
|
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
2021-05-05 17:32:45 +00:00
|
|
|
func (l *TribeLoader) Prime(key int, value *twmodel.Tribe) bool {
|
2020-06-02 15:45:21 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
var found bool
|
|
|
|
if _, found = l.cache[key]; !found {
|
|
|
|
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
|
|
|
|
// and end up with the whole cache pointing to the same value.
|
|
|
|
cpy := *value
|
|
|
|
l.unsafeSet(key, &cpy)
|
|
|
|
}
|
|
|
|
l.mu.Unlock()
|
|
|
|
return !found
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the value at key from the cache, if it exists
|
|
|
|
func (l *TribeLoader) Clear(key int) {
|
|
|
|
l.mu.Lock()
|
|
|
|
delete(l.cache, key)
|
|
|
|
l.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (l *TribeLoader) unsafeSet(key int, value *twmodel.Tribe) {
|
2020-06-02 15:45:21 +00:00
|
|
|
if l.cache == nil {
|
2021-05-05 17:32:45 +00:00
|
|
|
l.cache = map[int]*twmodel.Tribe{}
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
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 *tribeLoaderBatch) keyIndex(l *TribeLoader, key int) 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 *tribeLoaderBatch) startTimer(l *TribeLoader) {
|
|
|
|
time.Sleep(l.wait)
|
|
|
|
l.mu.Lock()
|
|
|
|
|
|
|
|
// we must have hit a batch limit and are already finalizing this batch
|
|
|
|
if b.closing {
|
|
|
|
l.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.batch = nil
|
|
|
|
l.mu.Unlock()
|
|
|
|
|
|
|
|
b.end(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *tribeLoaderBatch) end(l *TribeLoader) {
|
|
|
|
b.data, b.error = l.fetch(b.keys)
|
|
|
|
close(b.done)
|
|
|
|
}
|