tasks

package
v0.85.0-pre.12 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

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 AnyTask

type AnyTask interface {
	RunWithAnyInput(ctx *Cache, input any) (any, error)
}

AnyTask is the type-erased interface implemented by all Task instances.

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

func NewCache(parent context.Context) *Cache

NewCache creates a Cache for memoized task results.

NewCache panics if parent is nil.

func (*Cache) Context

func (c *Cache) Context() context.Context

Context returns the underlying context.Context.

func (*Cache) RunParallel

func (c *Cache) RunParallel(tasks ...Prepared) error

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.

func (*Cache) WithContext

func (c *Cache) WithContext(native context.Context) *Cache

WithContext returns a child Cache that shares the same task cache but uses the provided context for cancellation/deadline semantics.

WithContext panics if c or native is nil.

type Prepared

type Prepared interface {
	Run(ctx *Cache) error
}

Prepared is a Task with pre-bound input, ready for RunParallel.

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

func (t *Task[I, O]) BindInput(input I, dest ...*O) Prepared

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.

func (*Task[I, O]) Run

func (t *Task[I, O]) Run(ctx *Cache, input I) (O, error)

Run executes the Task with input using ctx as the sharing boundary.

func (*Task[I, O]) RunWithAnyInput

func (t *Task[I, O]) RunWithAnyInput(ctx *Cache, input any) (any, error)

RunWithAnyInput executes the Task with a type-erased input.

RunWithAnyInput returns an error if input does not have the Task's input type.

Jump to

Keyboard shortcuts

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