Documentation
¶
Overview ¶
Package tasks provides memoized, concurrency-safe task execution.
A Task takes comparable input and returns data or an error. Calls to the same Task with the same input share one execution within a Cache. The Cache is the sharing boundary: calls using different Cache values do not share results.
Task expiration is configured when the Task is created. With no expiration argument, completed results are retained for the lifetime of the Cache. A positive expiration retains completed results for that duration, measured from when the task function returns. A zero expiration coalesces only concurrent in-flight calls and does not retain completed results.
Completed results include successful values and non-cancellation errors. Errors caused by context cancellation or deadline expiration are not retained.
Tasks are automatically protected from circular dependencies by Go's compile-time "initialization cycle" errors when defined as package-level variables.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache stores memoized task results by task/input pair.
A Cache defines which Task calls share in-flight work and completed results. Its context supplies cancellation and deadline semantics.
func NewCache ¶
NewCache creates a Cache for memoized task results.
NewCache panics if parent is nil.
func (*Cache) RunParallel ¶
RunParallel executes all provided prepared tasks concurrently, returning the first non-nil error (if any). All tasks share this Cache.
Nil Prepared values are ignored.
type Task ¶
type Task[I comparable, O any] struct { // contains filtered or unexported fields }
Task is a typed, memoized unit of work.
func NewTask ¶
func NewTask[I comparable, O any]( fn func(ctx *Cache, input I) (O, error), expiration ...time.Duration, ) *Task[I, O]
NewTask creates a Task from fn.
NewTask(fn) caches completed results for the lifetime of the Cache used to run it. NewTask(fn, d) with d > 0 caches completed results for d, measured from when fn returns. NewTask(fn, 0) only coalesces concurrent in-flight calls and does not retain completed results.
NewTask returns nil if fn is nil. It panics if more than one expiration is provided or if the expiration is negative.
func (*Task[I, O]) BindInput ¶
BindInput creates a Prepared that captures the input and an optional destination pointer for the result.
If a destination is provided, running the Prepared stores the completed result there after the Task succeeds.