apitask

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package apitask provides a background execution context for background work that limits the execution time to the current request.

Index

Constants

This section is empty.

Variables

View Source
var ErrTask = errors.New("apitask")

ErrTask is the base of all errors originating from apitasks.

Functions

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware wraps next with an http.Handler which adds apitasks handling to the request context and waits for all tasks to exit before returning.

func Run

func Run(ctx context.Context, task Task) error

Run will run a request-scoped background task in a separate goroutine immediately if the current context supports it. Otherwise it makes an immediate blocking call to task.Run(ctx).

It is invalid to call Run within a tasks Run method.

func Wait

func Wait(ctx context.Context)

Wait will wait for all currently running request-scoped background tasks to complete before returning.

func With

func With(ctx context.Context) context.Context

With sets up the given context for adding request-scoped background tasks.

Types

type Task

type Task interface {

	// Type return a basic name for a task. It is not expected to be consistent
	// with the underlying type, but it should be low cardinality.
	Type() string

	// Run should run this task.
	Run(context.Context) error
}

Task is implemented by objects which may be ran in the background.

func Func

func Func(typ string, fn func(context.Context) error) Task

Jump to

Keyboard shortcuts

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