workq

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 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 workq is the queue a web process uses to ask a worker process for work (server.role, docs/operations/scaling.md): a row in worker_requests per (kind, key). A request is made with Store.Put; a Consumer in a process that runs the background workers claims it, hands it to the in-process worker that does the job and deletes it when the job is finished.

A repeated request for a key not yet finished refreshes the row instead of adding one, so a burst of uploads costs one regeneration, as it does inside a single process.

Index

Constants

View Source
const (
	// DefaultPollInterval is how often a Consumer looks for new requests. A
	// request waits that long, on top of the debounce of the worker it feeds.
	DefaultPollInterval = time.Second
	// DefaultLease is how long a claim holds without being renewed; the claim of
	// a process that died is taken over after it.
	DefaultLease = 30 * time.Second
	// DefaultBatch is the most requests claimed per poll.
	DefaultBatch = 100
)

Consumer defaults.

Variables

This section is empty.

Functions

This section is empty.

Types

type Consumer

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

Consumer claims the requests of the kinds it has a Handler for and hands them to the process's workers. Register handlers before Start.

func NewConsumer

func NewConsumer(store *Store, log *logger.Logger) *Consumer

NewConsumer returns a Consumer reading store with the default timings.

func (*Consumer) Flush

func (c *Consumer) Flush(ctx context.Context)

Flush applies the Settle calls made so far: finished work deletes its request, work that gave up keeps it, claimed until the lease lapses (a retry backoff).

func (*Consumer) Handle

func (c *Consumer) Handle(kind string, h Handler)

Handle registers the handler of a kind. Call before Start.

func (*Consumer) Release

func (c *Consumer) Release(ctx context.Context)

Release applies the Settle calls made so far, then gives back every claim that is still held or was given up, so another worker takes those requests over at once instead of after the lease lapses. Stop first; call it when the process shuts down.

func (*Consumer) Settle

func (c *Consumer) Settle(kind, key string, done bool)

Settle tells the consumer that the work of (kind, key) it handed over with Later is finished (done), or gave up and is to be retried by a later claim (not done). It never blocks and is safe to call under a worker's own lock. A key that no claim holds is ignored.

func (*Consumer) Start

func (c *Consumer) Start(ctx context.Context)

Start begins polling; it runs until Stop or ctx ends. Only the first call counts.

func (*Consumer) Stop

func (c *Consumer) Stop()

Stop ends the polling, so no new request is claimed, and waits for the loop. Work already handed over runs on; its Settle calls are applied by Flush. Safe without Start.

func (*Consumer) WithPollInterval

func (c *Consumer) WithPollInterval(d time.Duration) *Consumer

WithPollInterval sets how often the consumer looks for requests. Call before Start.

type Handler

type Handler func(ctx context.Context, r Request) Result

Handler starts the work a request asks for. It runs on the consumer's poll loop: it must hand the work to a worker and return, not do it.

type Request

type Request struct {
	// Kind is what to do, e.g. "regen:npm".
	Kind string
	// Key is what to do it to: an id, or a hash of Payload.
	Key string
	// Payload is the JSON subject of kinds whose key is only a hash; nil otherwise.
	Payload []byte
	// RequestedAt is when the request was last made.
	RequestedAt time.Time
}

Request is a row of worker_requests.

type Result

type Result int

Result is what a Handler says about a request it was handed.

const (
	// Done: the handler did all it will do with the request; delete it now.
	Done Result = iota
	// Later: the work was handed to a worker that calls Consumer.Settle when it
	// finishes; the request is deleted then, and is kept (and its claim renewed)
	// until then.
	Later
	// Retry: the worker cannot take the work now (its queue is full); keep the
	// request, and claim it again once the lease of this claim lapses.
	Retry
)

type Store

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

Store reads and writes worker_requests.

func NewStore

func NewStore(db *sql.DB) *Store

NewStore returns a Store over db.

func (*Store) Put

func (s *Store) Put(ctx context.Context, kind, key string, payload []byte) error

Put records a request for (kind, key), refreshing the one already there. The payload is JSON or nil.

func (*Store) PutIfAbsent

func (s *Store) PutIfAbsent(ctx context.Context, kind, key string, payload []byte) error

PutIfAbsent records a request unless one for (kind, key) is already there; for triggers that fire on every read and only need the work to happen soon.

Jump to

Keyboard shortcuts

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