dataloader

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package dataloader provides a generic per-request batch loader. The Loader accumulates keys from concurrent Load calls that arrive within a short window and fires one fetch per window, distributing results back to all waiters. This is the standard gqlgen dataloader pattern.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

type Loader[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Loader batches concurrent Load calls into one fetch call. K is the key type and V is the value type. The zero value is not usable; construct with New.

func New

func New[K comparable, V any](fetch func(context.Context, []K) (map[K]V, error), wait time.Duration) *Loader[K, V]

New returns a Loader that coalesces Load calls arriving within wait into a single fetch call.

func (*Loader[K, V]) Load

func (l *Loader[K, V]) Load(ctx context.Context, key K) (V, error)

Load registers key in the current batch window and blocks until the batch fires. If key was seeded via Prime the cached value is returned immediately without touching the fetch function.

func (*Loader[K, V]) Prime

func (l *Loader[K, V]) Prime(key K, val V)

Prime seeds the value for key so that future Load calls return it immediately without calling fetch. Any goroutines already waiting on key are unblocked.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL