notify

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send(ctx context.Context, notification Notification, receivers Receivers, logger *slog.Logger) error

Send synchronously delivers notification to the configured receivers.

Send is a convenience helper for simple use cases that do not need Manager's in-memory queue or asynchronous dispatcher. It resolves receivers using the same routing rule as Manager: ReceiverRouter.ReceiverIDs returns receiver map keys, and nil or empty receiver IDs send to all configured receivers.

If logger is nil, a discard logger is used. Send returns an error when the context, notification, delivery, or resolved receiver set is invalid, or when one or more targets fail.

func SendTo

func SendTo(ctx context.Context, notification Notification, receivers ...*Receiver) error

SendTo synchronously delivers notification to receivers without requiring a map.

SendTo is a convenience wrapper around Send for simple usage. It builds a Receivers map from receivers, uses a discard logger, resolves receiver routing the same way as Send, and returns after delivery completes.

Types

type Delivery

type Delivery interface {
	Dispatch(ctx context.Context, payload Payload, receivers []*Receiver) error
}

Delivery sends a receiver-scoped payload to receivers.

func NewDelivery

func NewDelivery(logger *slog.Logger) Delivery

NewDelivery constructs the default receiver delivery engine.

type DeliveryResult

type DeliveryResult struct {
	// Status is a target-specific delivery status such as sent or failed.
	Status string

	// StatusCode is the target response status code when one exists.
	StatusCode int

	// Response is a short target response summary suitable for logs.
	Response string
}

DeliveryResult captures target response details for logs and errors.

func WithRetry

func WithRetry(
	ctx context.Context,
	logger *slog.Logger,
	cfg RetryConfig,
	fn func() (DeliveryResult, error),
) (DeliveryResult, int, error)

WithRetry executes fn with retry attempts and exponential backoff.

type Dispatcher

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

Dispatcher dequeues notifications and delivers them.

func NewDispatcher

func NewDispatcher(
	store *Store,
	mailbox <-chan string,
	delivery Delivery,
	receivers Receivers,
	logger *slog.Logger,
) (*Dispatcher, error)

NewDispatcher constructs a dispatcher for an existing store, mailbox, delivery engine, and receiver map.

Most applications should use NewManager for queued asynchronous delivery or Send for simple synchronous delivery. NewDispatcher is useful when callers need to provide their own store, mailbox, or delivery implementation.

func (*Dispatcher) Start

func (d *Dispatcher) Start(ctx context.Context)

Start processes queued notifications until ctx is canceled or the mailbox closes.

type Manager

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

Manager owns notification queueing and dispatch infrastructure.

func NewManager

func NewManager(receivers Receivers, logger *slog.Logger) (*Manager, error)

NewManager constructs a notification manager for queued asynchronous delivery.

The manager owns an in-memory store, buffered mailbox, dispatcher, and delivery engine. Enqueued notifications are routed by ReceiverRouter.ReceiverIDs: returned values are matched against the receiver map keys, and nil or empty receiver IDs send to all configured receivers.

If receivers is nil, the manager starts with no configured receivers. If logger is nil, a discard logger is used.

func (*Manager) Enqueue

func (m *Manager) Enqueue(ctx context.Context, n Notification) (string, error)

Enqueue stores a notification and queues it for delivery.

func (*Manager) Receivers

func (m *Manager) Receivers() []*Receiver

Receivers returns configured receivers sorted by ID.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context) error

Start begins asynchronous delivery processing.

type Notification

type Notification interface {
	ID() string
	Data(receiver string, vars map[string]any, subject string) any
}

Notification describes one notification and its template data.

A notification may optionally implement ReceiverRouter to select specific receivers. Notifications that do not implement ReceiverRouter are sent to all configured receivers.

type Notifier

type Notifier interface {
	Enqueue(ctx context.Context, notification Notification) (string, error)
}

Notifier enqueues notifications for delivery.

type Payload

type Payload struct {
	// Notification is the original application notification.
	Notification Notification

	// Receiver is the receiver display name passed to template data.
	Receiver string

	// Vars contains receiver-scoped template variables.
	Vars map[string]any
}

Payload is the receiver-scoped notification payload given to targets.

func (Payload) Data

func (p Payload) Data(subject string) any

Data returns notification template data for this receiver and subject.

func (Payload) ID

func (p Payload) ID() string

ID returns the notification ID.

type Receiver

type Receiver struct {
	// ID is the receiver map key used for routing.
	//
	// NewManager and Send fill this from the Receivers map key when it is unset.
	ID ReceiverID

	// Name is the display name passed to notification template data.
	//
	// NewManager and Send default Name to the receiver ID when it is unset.
	Name string

	// Retry controls retry behavior for all targets on this receiver.
	Retry RetryConfig

	// Targets contains the delivery targets for this receiver.
	Targets []Target

	// Vars contains receiver-scoped template variables.
	Vars map[string]any
}

Receiver describes runtime delivery configuration.

func NewReceiver

func NewReceiver(id ReceiverID, targets ...Target) *Receiver

NewReceiver constructs a receiver with id and optional targets.

The receiver ID is the routing key used by ReceiverRouter.ReceiverIDs and Receivers maps. Name defaults to the ID during delivery when it is not set.

func (*Receiver) WithName

func (r *Receiver) WithName(name string) *Receiver

WithName sets the receiver display name and returns r.

The display name is passed to notification template data as the receiver name. It is not used as the routing ID.

func (*Receiver) WithRetry

func (r *Receiver) WithRetry(cfg RetryConfig) *Receiver

WithRetry sets the receiver retry configuration and returns r.

func (*Receiver) WithTargets

func (r *Receiver) WithTargets(targets ...Target) *Receiver

WithTargets appends targets to the receiver and returns r.

func (*Receiver) WithVars

func (r *Receiver) WithVars(vars map[string]any) *Receiver

WithVars sets receiver-scoped template variables and returns r.

type ReceiverID

type ReceiverID string

ReceiverID identifies a receiver in a Receivers map.

Notification routing uses receiver IDs, not Receiver.Name. Receiver.Name is a display/runtime payload value, while ReceiverID is the stable map key used by ReceiverIDs and receiver lookup.

type ReceiverRouter

type ReceiverRouter interface {
	ReceiverIDs() []ReceiverID
}

ReceiverRouter optionally routes a notification to receiver IDs.

Returning nil or an empty slice sends to all configured receivers.

type Receivers

type Receivers map[ReceiverID]*Receiver

Receivers maps receiver IDs to receiver configuration.

func NewReceivers

func NewReceivers(receivers ...*Receiver) Receivers

NewReceivers constructs a receiver map from receiver values.

Nil receivers are ignored. If duplicate IDs are provided, the later receiver wins. Receivers with an empty ID are included under the empty key and will only be selected when all receivers are used or an empty ID is requested.

type ResultTarget

type ResultTarget interface {
	SendResult(ctx context.Context, payload Payload) (DeliveryResult, error)
}

ResultTarget returns target-specific delivery details.

type RetryConfig

type RetryConfig struct {
	// Count is the number of retries after the initial attempt.
	//
	// For example, Count 2 means up to 3 total attempts.
	Count int

	// Backoff is the initial wait duration before retrying.
	//
	// Each later retry wait doubles this duration until MaxBackoff caps it.
	// If Backoff is zero or negative, retries happen immediately.
	Backoff time.Duration

	// MaxBackoff caps retry wait durations.
	//
	// If MaxBackoff is zero or negative, retry waits are not capped.
	MaxBackoff time.Duration
}

RetryConfig defines retry behavior for a receiver.

type Store

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

Store keeps queued notifications by queue id.

func NewStore

func NewStore() *Store

NewStore initializes an empty in-memory notification store.

func (*Store) Delete

func (s *Store) Delete(id string)

Delete removes a notification by queue id.

func (*Store) Get

func (s *Store) Get(id string) (Notification, bool)

Get returns a notification by queue id.

func (*Store) Put

func (s *Store) Put(id string, n Notification)

Put stores a notification by queue id.

type Target

type Target interface {
	Send(ctx context.Context, payload Payload) (DeliveryResult, error)
	Type() string
}

Target delivers a notification payload to one destination.

Jump to

Keyboard shortcuts

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