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 ¶
Func is a function that can be managed by a Throttle.
func Adapt ¶ added in v0.5.5
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.
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 (*Throttle[V]) Call ¶
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.