throttle

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: BSD-3-Clause Imports: 3 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package throttle allows calls to a function to be coalesced among multiple concurrent goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func[V any] func(context.Context) (V, error)

Func is a function that can be managed by a Throttle.

func Adapt added in v0.5.5

func Adapt[V any](fn any) Func[V]

Adapt converts a function with a compatible signature to a Func. The concrete value of fn must be a function with one of the following type schemes:

func()
func(context.Context)
func() error
func(context.Context) error
func() V
func(context.Context) V
func() (V, error)
func(context.Context) (V, error)

If fn is not a function or does not have one of these forms, Adapt panics. If fn is already a Func[V], it is returned unmodified. Wrapped functions with no error result report a nil error. Wrapped functions with no value result report a zero value.

type Set

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

Set is a collection of Throttle values indexed by key. A zero value is ready for use, but must not be copied after first use.

func NewSet

func NewSet[Key comparable, V any]() *Set[Key, V]

NewSet constructs a new empty Set.

func (*Set[Key, V]) Call

func (s *Set[Key, V]) Call(ctx context.Context, key Key, run Func[V]) (V, error)

Call calls the throttle associated with key, constructing a new one if necessary. Call is safe for use by multiple concurrent goroutines.

All concurrent callers of Call with a given key share a single Throttle.

type Throttle

type Throttle[V any] struct {
	// contains filtered or unexported fields
}

A Throttle coalesces function calls so that all goroutines concurrently executing Throttle.Call share the result of a single function execution made by one of the participants. This behaviour is sometimes also described as "single-flighting".

A Throttle is initially idle. The first goroutine to execute Throttle.Call on an idle throttle begins a new session and executes its function. All other goroutines that call the throttle during an active session block until either:

  • The context governing that goroutine's call ends, in which case it reports a zero value and the error that ended the context.

  • The goroutine leading the session completes its execution of its function, and reports a value and error, which is then shared among all the goroutines participating in the session.

If the leading goroutine's execution ends because the context governing its calling goroutine ended, another waiting goroutine (if any) is woken up and given an opportunity to use the throttle. Once all concurrent goroutines have returned, the throttle is once again idle, and the next caller will begin a new session.

Within a given session, a proposed function may partially execute multiple times, if one or more goroutines leading the session return early due to context termination. At most one goroutine will be active in the throttle at a time, however; and if any completes before its context ends (even with an error) no further run functions will be executed within the scope of that session.

A zero Throttle is ready for use, but must not be copied after first use.

func New

func New[V any]() *Throttle[V]

New constructs a new idle Throttle.

func (*Throttle[V]) Call

func (t *Throttle[V]) Call(ctx context.Context, run Func[V]) (V, error)

Call proposes to invoke run guarded by t.

If ctx ends before a value is resolved, Call returns a zero value and the context error. Otherwise, it returns the value and error from the first proposed run function to complete among all goroutines concurrently active in t during the execution of Call.

Concurrent goroutines are permitted to propose different run functions to t, so the values returned to a caller on one goroutine may not come from the function passed to Call by that goroutine.

Call is safe for concurrent use by multiple goroutines.

Jump to

Keyboard shortcuts

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