worker

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package worker provides a Worker interface and WorkerGroup for managing concurrent background workers with graceful lifecycle control.

Package worker provides a Worker interface and WorkerGroup for managing concurrent background workers.

ref: zeromicro/go-zero core/service/servicegroup.go — ServiceGroup pattern Adopted: parallel Start/Stop, goroutine-per-worker, sync.Once for stop. Deviated: context-based lifecycle (Start/Stop take ctx) instead of bare Start()/Stop(); error returns for diagnostics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LazyWorker

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

LazyWorker is a Worker whose concrete delegate is resolved at runtime (via Set) after it's already wired into a WorkerGroup. Used when a Worker is produced during Bootstrap.Run after WithWorkers already captured this placeholder.

ref: docs/plans/202604191515-auth-federated-whistle.md — S37 WORKER-LAZY-HOIST

Thread safety: Set (writer) and Start/Stop (readers) synchronize via atomic.Pointer. Semantics: nil delegate → Start/Stop are no-op success (preserves lazyBootstrapWorker contract where adminExists==true yields no cleaner).

func Lazy

func Lazy() *LazyWorker

Lazy returns a fresh LazyWorker with nil delegate.

func (*LazyWorker) Set

func (l *LazyWorker) Set(w Worker) (stored bool)

Set atomically assigns the delegate Worker. First call wins: subsequent calls are ignored and return false. Passing nil is rejected and returns false. The single-producer bootstrap sink contract is preserved.

func (*LazyWorker) Start

func (l *LazyWorker) Start(ctx context.Context) error

Start delegates to the underlying Worker. Nil delegate → no-op success.

func (*LazyWorker) Stop

func (l *LazyWorker) Stop(ctx context.Context) error

Stop delegates to the underlying Worker. Nil delegate → no-op success.

type PeriodicWorker

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

PeriodicWorker runs a function at a fixed interval. Panics in the function are isolated and logged, not propagated.

func NewPeriodicWorker

func NewPeriodicWorker(clk clock.Clock, interval time.Duration, fn func(ctx context.Context)) *PeriodicWorker

NewPeriodicWorker creates a PeriodicWorker that runs fn every interval.

func (*PeriodicWorker) Start

func (p *PeriodicWorker) Start(ctx context.Context) error

Start runs the periodic function until ctx is canceled or Stop is called. Each call to Start creates a fresh done channel, so a PeriodicWorker can be restarted after Stop.

func (*PeriodicWorker) Stop

func (p *PeriodicWorker) Stop(_ context.Context) error

Stop signals the worker to exit.

type Worker

type Worker = kworker.Worker

Worker is a type alias for the kernel Worker interface. The authoritative definition lives in kernel/worker; this alias lets the runtime/worker implementations (WorkerGroup, LazyWorker, PeriodicWorker) type-check against Worker without importing kernel/worker from every call site.

This is not a migration shim — runtime/worker is the canonical package for Worker implementations, and the alias is how implementations declare they satisfy the kernel contract.

Guidance for new consumers: code outside runtime/worker (e.g., adapters/, cells/) SHOULD import kernel/worker directly and reference worker.Worker (the kernel contract). This alias exists for historical compatibility and for runtime/worker's own implementations (WorkerGroup, LazyWorker, PeriodicWorker) to satisfy the kernel contract without importing the kernel package from every local file.

ref: kernel/worker/worker.go — authoritative Worker interface definition.

type WorkerGroup

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

WorkerGroup manages multiple workers, starting them concurrently and stopping them when requested.

func NewWorkerGroup

func NewWorkerGroup() *WorkerGroup

NewWorkerGroup creates a new WorkerGroup.

func (*WorkerGroup) Add

func (g *WorkerGroup) Add(w Worker)

Add registers a worker with the group.

func (*WorkerGroup) Start

func (g *WorkerGroup) Start(ctx context.Context) error

Start launches all workers concurrently. It blocks until all workers return. If any worker returns a non-context error, all sibling workers are canceled via a shared context. The first error encountered is returned.

Early-exit handling: a worker that returns nil while the group context is still live has terminated its long-running loop without signaling failure — historically that produced a silent firstErr=nil. The group now records kworker.ErrWorkerExitedEarly so callers can errors.Is-detect the abnormal signal. Returns from a Stop or context cancellation propagate untouched (errors.Is(err, context.Canceled) is honored).

func (*WorkerGroup) Stop

func (g *WorkerGroup) Stop(ctx context.Context) error

Stop stops all workers serially in reverse registration order. Each worker is stopped before the next to ensure safe teardown ordering.

Jump to

Keyboard shortcuts

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