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 ¶
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 NewSerialized ¶ added in v0.82.0
func NewSerialized[T any](ctx context.Context, name string, interval time.Duration, fetch FetchFunc[T]) *Buffer[T]
NewSerialized returns a Buffer like New that runs at most one fetch per key at a time. Requests arriving during a fetch form the next batch, which is fetched once both its window closed and the running fetch ended.