Help us shape the future of Go! Take the 2026 Go Developer Survey

notifyworker

package
v1.149.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package notifyworker drains the notification queue: it claims due rows under a lease, renders them, delivers them over SMTP or a channel's own transport, and resolves each batch to sent, retried, or failed.

It owns the delivery policy — which transports can deliver right now, the retry budget and its backoff, and the retention purge that bounds the table — and holds the rendering and transport layers behind the collaborators in Config.

Index

Constants

View Source
const (
	// DefaultPollEvery is the fallback poll interval when LISTEN/NOTIFY does
	// not wake the worker.
	DefaultPollEvery = 30 * time.Second
	// DefaultLease bounds one delivery attempt; an expired lease returns the
	// row to claimable state for crash recovery.
	DefaultLease = 2 * time.Minute
	// DefaultMaxAttempts is the delivery attempt budget per row.
	DefaultMaxAttempts = 5

	// DefaultResolvedRetention keeps sent/failed rows for operator
	// inspection before the purge removes them.
	DefaultResolvedRetention = 30 * 24 * time.Hour
	// DefaultPendingTTL bounds how long an undelivered row stays relevant.
	// Beyond it the event is stale (nobody wants a share email from last
	// month when SMTP is finally configured) and the purge drops it.
	DefaultPendingTTL = 7 * 24 * time.Hour
)

Worker defaults.

Variables

This section is empty.

Functions

func DeliveryID added in v1.138.3

func DeliveryID(id int64) string

DeliveryID is the id a channel delivery of queue row id carries: the same on every attempt of that row, so a receiver recognizes a retry (#1997).

Types

type AttachmentLoader added in v1.143.0

type AttachmentLoader interface {
	Load(ctx context.Context, att notification.Attachment) (notification.File, error)
}

AttachmentLoader reads one attachment's content. notifyattach.Loader implements it.

type ChannelSender added in v1.134.0

type ChannelSender interface {
	Send(ctx context.Context, ch notification.Channel, d notification.Delivery) error
}

ChannelSender posts one document to one channel, dispatching on its kind. notifypost.Senders implements it.

type Config

type Config struct {
	Queue    notification.QueueStore
	Settings smtp.SettingsStore
	Renderer *notifyrender.Renderer
	Sender   notifysend.Sender
	// Channels reads the destination a channel row names. nil disables
	// channel delivery: those rows stay pending, as email rows do on a
	// deployment with no mail server.
	Channels notification.ChannelStore
	// ChannelSenders delivers to the three HTTP channel kinds. nil disables
	// channel delivery with Channels.
	//
	// It is an interface rather than *notifypost.Senders so the worker's
	// own behavior -- which transport a row goes to, and how a failure
	// resolves -- is testable without standing up an upstream for each kind.
	ChannelSenders ChannelSender
	// Attachments reads a document's files at delivery. nil sends every
	// attachment as its link.
	Attachments AttachmentLoader
	// PollEvery, Lease, and MaxAttempts default to the package constants
	// when zero.
	PollEvery   time.Duration
	Lease       time.Duration
	MaxAttempts int
}

Config configures the send worker.

type Worker

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

Worker drains the notification queue: it claims due rows, renders them, and delivers each over the transport its destination names. It follows the indexjobs worker shape (poll ticker + LISTEN/NOTIFY wakeup, lease-based claiming, retry with exponential backoff). A transport that cannot deliver right now — SMTP unconfigured, or no channel senders wired — is excluded from the claim, so its rows stay pending without burning delivery attempts while the other transport keeps draining.

func New

func New(cfg Config) *Worker

New creates a send worker, applying defaults for zero config values.

func (*Worker) Notify

func (w *Worker) Notify()

Notify wakes the worker without waiting for the next poll tick. Safe to call from any goroutine; a flurry of calls coalesces into one wakeup.

func (*Worker) Start

func (w *Worker) Start(ctx context.Context)

Start launches the worker loop. Idempotent. The loop carries ctx's values but not its cancellation: it runs until Stop, however short-lived the context that started it.

func (*Worker) Stop

func (w *Worker) Stop()

Stop terminates the worker loop and waits for in-flight work. Idempotent. An abandoned claimed row is safe: its lease expires and it is reclaimed.

Jump to

Keyboard shortcuts

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