requestbuffer

package
v0.78.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: BSD-3-Clause, AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package requestbuffer coalesces concurrent reads of the same expensive resource into a single fetch.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Interval

func Interval(ctx context.Context, envVar string, def time.Duration) time.Duration

Interval reads a buffer interval from envVar, falling back to def.

Types

type Buffer

type Buffer[T any] struct {
	// contains filtered or unexported fields
}

Buffer batches requests per key: the first request opens a window, every request arriving within it joins the batch, and a single fetch serves them all. The fetch starts only after the window closed, so a caller never observes data read before its own request.

func New

func New[T any](ctx context.Context, name string, interval time.Duration, fetch FetchFunc[T]) *Buffer[T]

New returns a Buffer serving batched requests through fetch. ctx bounds the fetches, not the callers, and must outlive them.

func (*Buffer[T]) Get

func (b *Buffer[T]) Get(ctx context.Context, key string) (T, error)

Get returns the value for key, sharing one fetch with the other callers of the current batch. The value is shared as is, so callers must treat it as read-only unless the fetch hands out copies.

type FetchFunc

type FetchFunc[T any] func(ctx context.Context, key string) (T, error)

FetchFunc reads the resource identified by key.

Jump to

Keyboard shortcuts

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