tasks

package
v0.85.0-pre.6 Latest Latest
Warning

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

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

Documentation

Overview

Package tasks provides memoized, concurrency-safe task execution.

A Task is a function that takes input, returns data (or an error), and runs at most once per Ctx/input pairing, even if invoked repeatedly.

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 *Ctx, input any) (any, error)
}

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

type BoundTask

type BoundTask interface {
	Run(ctx *Ctx) error
}

BoundTask is a task with pre-bound input, ready for execution.

type Ctx

type Ctx struct {
	// contains filtered or unexported fields
}

Ctx is a task execution context that memoizes results by task/input pair.

func NewCtx

func NewCtx(parent context.Context) *Ctx

NewCtx creates a Ctx with no TTL (results cached indefinitely).

func NewCtxWithTTL

func NewCtxWithTTL(parent context.Context, ttl time.Duration) *Ctx

NewCtxWithTTL creates a Ctx whose cached results expire after ttl. Expired entries are lazily cleaned up during cache access.

func (*Ctx) NativeContext

func (c *Ctx) NativeContext() context.Context

NativeContext returns the underlying context.Context.

func (*Ctx) RunParallel

func (c *Ctx) RunParallel(tasks ...BoundTask) error

RunParallel executes all provided BoundTasks concurrently, returning the first non-nil error (if any). All tasks share this Ctx's cache.

func (*Ctx) WithNativeContext

func (c *Ctx) WithNativeContext(native context.Context) *Ctx

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

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 *Ctx, input I) (O, error),
) *Task[I, O]

NewTask creates a Task from the provided function. Returns nil if fn is nil.

func (*Task[I, O]) Bind

func (t *Task[I, O]) Bind(input I, dest ...*O) BoundTask

Bind creates a BoundTask that captures the input and an optional destination pointer for the result.

func (*Task[I, O]) Run

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

Run executes the task with a typed input.

func (*Task[I, O]) RunWithAnyInput

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

RunWithAnyInput executes the task with a type-erased input.

Jump to

Keyboard shortcuts

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