queue

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 6 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 queue implements job queues with delayed jobs, a visibility timeout, retries and a dead-letter list.

Every job is in exactly one state:

ready    waiting to be popped, ordered by (at, seq) where at is when it became ready
delayed  not ready before at
inflight popped, must be acked before at (the visibility deadline)
dead     ran out of retries (at = when it died)

Time-driven transitions are pure functions of a job's own fields and the clock, and are applied lazily whenever a queue is touched:

delayed  → ready              at at
inflight → ready (or dead when attempts > retries) at the deadline

Replication works by sending "set this job to this state" operations (Restore, Claim, Ack, Purge) with absolute timestamps, so a replica or an AOF replay converges to the same contents whenever it applies them. The command layer turns each mutation into such operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RestoreArgs

func RestoreArgs(name string, j Job) []string

RestoreArgs is the internal command that recreates one job exactly: q.restore queue id payload retries attempts state at.

Types

type Counts

type Counts struct{ Ready, Delayed, Inflight, Dead int }

Counts is QLEN's reply.

type Job

type Job struct {
	ID       string
	Payload  string
	Retries  int64
	Attempts int64
	State    State
	At       int64 // unix ms, meaning depends on State
}

Job is a snapshot of a job (returned by value; never shared).

type Manager

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

Manager holds every queue. All methods are safe for concurrent use.

func New

func New() *Manager

func (*Manager) AckLocked

func (m *Manager) AckLocked(name, id string, now int64) bool

AckLocked deletes an in-flight job. Returns false if it isn't in flight.

func (*Manager) ClaimLocked

func (m *Manager) ClaimLocked(name, id string, attempts, deadline int64) bool

ClaimLocked puts job id in flight with the given attempts and deadline, whatever its current state (replay of a QPOP). Returns false if unknown.

func (*Manager) DeadJobs

func (m *Manager) DeadJobs(name string, count int) []Job

DeadJobs returns up to count dead jobs (count < 0: all), oldest first.

func (*Manager) DeleteLocked

func (m *Manager) DeleteLocked(name, id string) bool

DeleteLocked deletes a job in any state (replay of QACK).

func (*Manager) Dump

func (m *Manager) Dump(emit func(args []string))

Dump emits q.restore for every job, recreating all queues.

func (*Manager) ExistsLocked

func (m *Manager) ExistsLocked(name, id string) bool

ExistsLocked reports whether a job with id is in the queue (any state).

func (*Manager) Len

func (m *Manager) Len(name string) Counts

func (*Manager) Lock

func (m *Manager) Lock()

Lock/Unlock let the command layer run a mutation and propagate it atomically, so AOF/replicas see operations in execution order. Methods ending in "Locked" require it.

func (*Manager) NackLocked

func (m *Manager) NackLocked(name, id string, delay time.Duration, now int64) (Job, bool)

NackLocked returns an in-flight job to the queue: ready (or delayed by delay), or dead when its attempts exceed its retries. It returns the job's new state, or false if the job isn't in flight.

func (*Manager) Names

func (m *Manager) Names() []string

Names returns every queue name, sorted.

func (*Manager) NewID

func (m *Manager) NewID() string

NewID returns a unique job id.

func (*Manager) PopLocked

func (m *Manager) PopLocked(name string, visibility time.Duration, now int64) (Job, bool)

PopLocked claims the oldest ready job for visibility ms. It returns the job as it is after the claim (Inflight, Attempts incremented).

func (*Manager) PurgeLocked

func (m *Manager) PurgeLocked(name string) int

PurgeLocked deletes the queue. Returns how many jobs it held.

func (*Manager) Restore

func (m *Manager) Restore(name string, j Job)

Restore is RestoreLocked with locking.

func (*Manager) RestoreLocked

func (m *Manager) RestoreLocked(name string, j Job)

RestoreLocked creates or replaces a job with exactly the given fields. This is the replication primitive behind QPUSH, QNACK, QRETRYDEAD and Dump.

func (*Manager) RetryDeadLocked

func (m *Manager) RetryDeadLocked(name string, now int64) []Job

RetryDeadLocked moves every dead job back to ready with attempts reset. Returns the jobs as restored, oldest first.

func (*Manager) Snapshot

func (m *Manager) Snapshot() map[string][]Job

Snapshot returns every job of every queue, ordered by queue name, then state (ready, delayed, inflight, dead), then (at, seq). Restoring them in this order recreates the same queues.

func (*Manager) Unlock

func (m *Manager) Unlock()

func (*Manager) WaitInfoLocked

func (m *Manager) WaitInfoLocked(name string) (<-chan struct{}, int64)

WaitInfoLocked returns a channel closed when a job of name may become ready, and the time of the next time-driven transition (0 if none).

type State

type State uint8
const (
	Ready State = iota + 1
	Delayed
	Inflight
	Dead
)

func ParseState

func ParseState(s string) (State, bool)

ParseState is the inverse of State.String.

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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