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
- type Consumer
- func (c *Consumer) Flush(ctx context.Context)
- func (c *Consumer) Handle(kind string, h Handler)
- func (c *Consumer) Release(ctx context.Context)
- func (c *Consumer) Settle(kind, key string, done bool)
- func (c *Consumer) Start(ctx context.Context)
- func (c *Consumer) Stop()
- func (c *Consumer) WithPollInterval(d time.Duration) *Consumer
- type Handler
- type Request
- type Result
- type Store
Constants ¶
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 ¶
NewConsumer returns a Consumer reading store with the default timings.
func (*Consumer) Flush ¶
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) Release ¶
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 ¶
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 ¶
Start begins polling; it runs until Stop or ctx ends. Only the first call counts.
type Handler ¶
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 )