Documentation
¶
Overview ¶
Package async provides bounded, lifecycle-owned asynchronous execution for generated Spice applications.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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.
type Observer ¶
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) 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.