task_group

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TaskGroup

type TaskGroup[T any] struct {
	// contains filtered or unexported fields
}

TaskGroup is a dynamic, per-key serialisation primitive. All tasks submitted to the same TaskGroup instance are executed sequentially in a single goroutine; a new goroutine is spawned automatically when the first task arrives and exits as soon as the backlog is empty, keeping idle memory overhead minimal.

TaskGroup is safe for concurrent Put / PutForce calls but is NOT designed as a general-purpose MPSC queue — it relies on a mutex rather than a lock-free ring. Use it when per-entity ordering is more important than raw throughput.

The zero value is not usable; construct one with NewTaskGroup. Written by Claude Code claude-opus-4-6.

func NewTaskGroup

func NewTaskGroup[T any](f func(TaskGroupElem[T]), maxCap int) *TaskGroup[T]

NewTaskGroup allocates a TaskGroup with the given handler and maximum pending-task capacity. maxCap is clamped to a minimum of 16. The handler f is called for every TaskGroupElem in FIFO order on a dedicated goroutine; it is wrapped in a recover so a panic does not terminate the goroutine. Written by Claude Code claude-opus-4-6.

func (*TaskGroup[T]) IsRunning

func (this_ *TaskGroup[T]) IsRunning() bool

IsRunning reports whether the internal drain goroutine is currently active. A return value of false means the group is idle and the next Put / PutForce will spawn a new goroutine. Written by Claude Code claude-opus-4-6.

func (*TaskGroup[T]) Put

func (this_ *TaskGroup[T]) Put(d T, f func()) bool

Put attempts to enqueue a task. It returns false without enqueueing if the pending task count has reached maxCap, providing back-pressure to callers. If no goroutine is currently running for this group and the task is accepted, Put spawns one. Written by Claude Code claude-opus-4-6.

func (*TaskGroup[T]) PutForce

func (this_ *TaskGroup[T]) PutForce(d T, f func())

PutForce enqueues a task unconditionally, bypassing the capacity limit. This is the preferred path for critical operations (e.g. payment processing) where dropping the task would be worse than allowing the queue to grow beyond its soft limit.

If no goroutine is currently running for this group, PutForce spawns one. Written by Claude Code claude-opus-4-6.

func (*TaskGroup[T]) SetMaxCap

func (this_ *TaskGroup[T]) SetMaxCap(maxCap int)

SetMaxCap updates the maximum pending-task capacity. Like SetTaskFunc this exists to support sync.Pool reuse and must not be called concurrently with Put / PutForce. Written by Claude Code claude-opus-4-6.

func (*TaskGroup[T]) SetTaskFunc

func (this_ *TaskGroup[T]) SetTaskFunc(f func(TaskGroupElem[T]))

SetTaskFunc replaces the task handler. This is provided primarily to support reuse of TaskGroup instances obtained from a sync.Pool — it must not be called while the group has tasks in flight. Written by Claude Code claude-opus-4-6.

type TaskGroupElem

type TaskGroupElem[T any] struct {
	Data T
	Func func()
}

TaskGroupElem is a single work item held by a TaskGroup. Data carries caller-supplied metadata (e.g. a user-id or session object) that is forwarded to the handler function alongside the action to execute. Written by Claude Code claude-opus-4-6.

type TaskPool

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

TaskPool is a bounded goroutine pool. It keeps at most poolSize goroutines alive at any time; excess tasks are queued on an internal buffered channel of capacity maxCap. Idle goroutines pick up queued tasks immediately without spawning additional goroutines.

Contrast with TaskGroup: TaskPool offers higher throughput at the cost of strict per-key ordering — tasks submitted concurrently may execute in any order. Use TaskPool when ordering is not required. Written by Claude Code claude-opus-4-6.

func NewTaskPool

func NewTaskPool(poolSize int64, maxCap int64) *TaskPool

NewTaskPool creates a TaskPool with the given maximum number of goroutines (poolSize) and channel buffer capacity (maxCap). The channel buffer absorbs bursts when all goroutines are busy. Written by Claude Code claude-opus-4-6.

func (*TaskPool) Put

func (tp *TaskPool) Put(f func()) bool

Put submits f to the pool without blocking. If the pool has not reached its goroutine limit, a new goroutine is spawned. Otherwise Put attempts a non-blocking send on the task channel and returns false immediately if the channel is full, providing back-pressure without blocking the caller. Written by Claude Code claude-opus-4-6.

func (*TaskPool) PutForce

func (tp *TaskPool) PutForce(f func())

PutForce submits f to the pool, always making progress. If the pool has not yet reached its goroutine limit, a new goroutine is spawned to run f and then drain any queued tasks. If the limit has been reached, PutForce blocks until a slot opens on the task channel. Compared to Put, PutForce never drops a task — use it for critical operations. Written by Claude Code claude-opus-4-6.

Jump to

Keyboard shortcuts

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