queue

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package queue provides a small lifecycle-aware in-process work queue.

It is deliberately an application primitive, not a pretend durable broker: messages in memory are lost when the process exits. Use the outbox package (or a real broker) when a job must survive a crash.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(context.Context, Message) error

Handler processes a message. Returning an error retries it until MaxAttempts is reached.

type Message

type Message struct {
	ID       string
	Body     []byte
	Attempts int
}

Message is one queued payload. Attempts starts at one when a worker begins processing it.

type Options

type Options struct {
	Name         string
	Workers      int
	Buffer       int
	MaxAttempts  int
	RetryDelay   time.Duration
	OnDeadLetter func(context.Context, Message, error)
}

Options configures a Queue.

type Queue

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

Queue is safe for concurrent producers and consumers.

func New

func New(opt Options, h Handler) (*Queue, error)

New constructs a queue. It starts when registered with a lifecycle registry and OnReady runs, or when Start is called directly.

func (*Queue) Enqueue

func (q *Queue) Enqueue(ctx context.Context, body []byte) (string, error)

Enqueue adds a payload, returning when it has entered the queue buffer. Enqueue is valid before Start, which makes boot-time producers convenient.

func (*Queue) Name

func (q *Queue) Name() string

Name identifies the queue in logs and diagnostics.

func (*Queue) OnReady

func (q *Queue) OnReady(ctx context.Context) error

OnReady implements lifecycle.Ready.

func (*Queue) OnShutdown

func (q *Queue) OnShutdown(ctx context.Context) error

OnShutdown stops accepting work and drains messages already buffered.

func (*Queue) Start

func (q *Queue) Start(parent context.Context) error

Start launches workers. The parent is detached from request cancellation.

func (*Queue) Stop

func (q *Queue) Stop(ctx context.Context) error

Stop cancels workers after the input buffer is drained. A handler should honor its context so shutdown remains bounded by the application deadline.

Jump to

Keyboard shortcuts

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