notify

package
v0.0.11 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrManagerStarted = errors.New("manager already started")

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 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 contains target-specific response details.
	//
	// Generic Notifykit loggers do not log this value because target responses
	// may contain sensitive data such as webhook URLs, tokens, echoed payloads,
	// or authentication diagnostics.
	Response string
}

DeliveryResult captures target response details returned by delivery targets.

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, opts ...ManagerOption) (*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.

Start launches the configured worker count. Each worker reads from the same queue, so different notifications may be delivered concurrently. Start may only be called once. Calling Start again returns ErrManagerStarted.

type ManagerOption added in v0.0.8

type ManagerOption func(*managerConfig)

func WithWorkers added in v0.0.8

func WithWorkers(workers int) ManagerOption

WithWorkers configures how many queued notifications may be processed concurrently.

Values less than 1 are ignored. The default is one worker. Targets used with more than one worker must be safe for concurrent calls.

type Notification

type Notification interface {
	ID() string
	Data(receiver string, customData 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

	// CustomData contains receiver-scoped custom template data.
	CustomData 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

	// CustomData contains receiver-scoped custom template data.
	CustomData 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) WithCustomData added in v0.0.11

func (r *Receiver) WithCustomData(customData map[string]any) *Receiver

WithCustomData sets receiver-scoped custom template data and returns r.

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.

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 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 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