async

package
v0.1.0-preview.3 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package async provides bounded, lifecycle-owned asynchronous execution for generated Spice applications.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is returned when a task is submitted after shutdown starts.
	ErrClosed = errors.New("async executor is closed")
	// ErrPanicked identifies a task panic contained at the asynchronous
	// boundary.
	ErrPanicked = errors.New("async task panicked")
)

Functions

This section is empty.

Types

type Definition

type Definition struct {
	ID     string
	Module string
}

Definition identifies one compiler-owned asynchronous task and its module.

type Executor

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

Executor admits at most one goroutine per concurrency slot. Submit applies backpressure instead of building a hidden queue.

Example
package main

import (
	"context"
	"fmt"
	"sync/atomic"

	"github.com/spice-framework/spice/async"
)

func main() {
	executor, err := async.NewExecutor(context.Background(), 4)
	if err != nil {
		fmt.Printf("construct: %v\n", err)
		return
	}
	var completed atomic.Bool
	err = executor.Submit(
		context.Background(),
		async.Definition{ID: "orders.Notify", Module: "example.com/shop/orders"},
		func(context.Context) error {
			completed.Store(true)
			return nil
		},
	)
	if err != nil {
		fmt.Printf("submit: %v\n", err)
		return
	}
	err = executor.Shutdown(context.Background())
	fmt.Printf("completed=%v err=%v\n", completed.Load(), err)
}
Output:
completed=true err=<nil>

func NewExecutor

func NewExecutor(
	ctx context.Context,
	maxConcurrent int,
	observers ...Observer,
) (*Executor, error)

NewExecutor constructs an executor with a caller-owned lifetime context. It starts no goroutine until a task is accepted.

func (*Executor) Done

func (executor *Executor) Done() <-chan struct{}

Done closes after shutdown starts and every accepted task returns.

func (*Executor) Shutdown

func (executor *Executor) Shutdown(ctx context.Context) error

Shutdown stops admission and waits for accepted tasks. If ctx ends first, execution contexts are canceled and Shutdown returns without waiting for tasks that ignore cancellation. Concurrent calls share one terminal result.

func (*Executor) Snapshot

func (executor *Executor) Snapshot() Snapshot

Snapshot returns bounded executor statistics.

func (*Executor) Submit

func (executor *Executor) Submit(
	admission context.Context,
	definition Definition,
	task Task,
) error

Submit blocks until a concurrency slot is available or an admission, lifetime, or shutdown context ends. Once accepted, the task owns a bounded worker goroutine.

type Observer

type Observer func(context.Context, Result)

Observer receives task completion on the worker goroutine. It must not panic or block indefinitely.

type PanicError

type PanicError struct {
	Definition Definition
}

PanicError reports a contained task panic without exposing the recovered value, which may contain application data.

func (*PanicError) Error

func (err *PanicError) Error() string

Error describes the panicked task.

func (*PanicError) Unwrap

func (err *PanicError) Unwrap() error

Unwrap supports errors.Is(err, ErrPanicked).

type Result

type Result struct {
	Definition Definition
	Duration   time.Duration
	Err        error
	Panicked   bool
}

Result describes one completed asynchronous task.

type Snapshot

type Snapshot struct {
	Submitted uint64
	Running   int
	Completed uint64
	Failed    uint64
	Panicked  uint64
	Closed    bool
}

Snapshot is a concurrency-safe executor view.

type Task

type Task func(context.Context) error

Task executes with the executor's caller-owned lifetime context.

Jump to

Keyboard shortcuts

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