notification

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package notification defines tenant-scoped notification messages, notifier decorators, in-memory delivery, SMTP email delivery, Resend email API delivery, and webhook delivery.

包 notification 定义租户范围的通知消息、通知器装饰器、内存投递、SMTP 邮件投递、 Resend 邮件 API 投递和 Webhook 投递。

Index

Constants

View Source
const (
	// ChannelWebhook is the conventional notification channel for webhook delivery.
	ChannelWebhook = "webhook"

	// WebhookSignatureHeader is the default HMAC signature header.
	WebhookSignatureHeader = "X-SaaS-Signature"

	// WebhookTimestampHeader is the default HMAC timestamp header.
	WebhookTimestampHeader = "X-SaaS-Timestamp"
)
View Source
const (
	// ChannelEmail is the conventional notification channel for email delivery.
	ChannelEmail = "email"
)

Variables

View Source
var (
	ErrInvalidMessage        = errors.New("saas/notification: invalid message")
	ErrInvalidNotifierConfig = errors.New("saas/notification: invalid notifier config")
	ErrInvalidResendConfig   = errors.New("saas/notification: invalid resend config")
	ErrInvalidSMTPConfig     = errors.New("saas/notification: invalid smtp config")
	ErrInvalidWebhookConfig  = errors.New("saas/notification: invalid webhook config")
	ErrNilNotifier           = errors.New("saas/notification: nil notifier")
	ErrResendDelivery        = errors.New("saas/notification: resend delivery failed")
	ErrUnsupportedChannel    = errors.New("saas/notification: unsupported channel")
	ErrWebhookDelivery       = errors.New("saas/notification: webhook delivery failed")
	ErrTLSRequired           = errors.New("saas/notification: tls required")
)

Functions

func DefaultRetryIf

func DefaultRetryIf(err error) bool

DefaultRetryIf reports whether err should be retried by default.

Types

type ChannelRouter

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

ChannelRouter dispatches messages to a notifier registered for message.Channel.

func NewChannelRouter

func NewChannelRouter(routes map[string]Notifier) (*ChannelRouter, error)

NewChannelRouter creates a channel-based router.

func (*ChannelRouter) Send

func (router *ChannelRouter) Send(ctx context.Context, message Message) error

Send dispatches message to its channel notifier.

type FanoutNotifier

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

FanoutNotifier sends each message to multiple notifiers in order.

func NewFanoutNotifier

func NewFanoutNotifier(notifiers ...Notifier) (*FanoutNotifier, error)

NewFanoutNotifier creates a sequential fanout notifier.

func (*FanoutNotifier) Send

func (notifier *FanoutNotifier) Send(ctx context.Context, message Message) error

Send delivers message to all configured notifiers and joins delivery errors.

type MemoryNotifier

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

func NewMemoryNotifier

func NewMemoryNotifier() *MemoryNotifier

func (*MemoryNotifier) Messages

func (notifier *MemoryNotifier) Messages() []Message

func (*MemoryNotifier) Send

func (notifier *MemoryNotifier) Send(ctx context.Context, message Message) error

type Message

type Message struct {
	ID       string
	TenantID types.TenantID
	Channel  string
	To       string
	Subject  string
	Body     string
	Metadata map[string]string
	Tags     map[string]string
}

func (Message) Clone

func (message Message) Clone() Message

Clone returns a deep copy of message metadata and provider-visible tags.

func (Message) Validate

func (message Message) Validate() error

Validate verifies the fields required by all notification senders.

type Notifier

type Notifier interface {
	Send(ctx context.Context, message Message) error
}

type NotifierFunc

type NotifierFunc func(ctx context.Context, message Message) error

NotifierFunc adapts a function to Notifier.

func (NotifierFunc) Send

func (fn NotifierFunc) Send(ctx context.Context, message Message) error

Send calls fn.

type ResendBodyFormat

type ResendBodyFormat string

ResendBodyFormat controls how Message.Body is mapped to Resend.

const (
	// ResendBodyText sends Message.Body as the Resend text body.
	ResendBodyText ResendBodyFormat = "text"

	// ResendBodyHTML sends Message.Body as the Resend html body.
	ResendBodyHTML ResendBodyFormat = "html"
)

type ResendConfig

type ResendConfig struct {
	APIKey            string
	From              string
	Channel           string
	Endpoint          string
	BodyFormat        ResendBodyFormat
	Timeout           time.Duration
	MaxResponseBytes  int64
	Client            *http.Client
	AllowInsecureHTTP bool
}

ResendConfig configures ResendNotifier.

type ResendNotifier

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

ResendNotifier sends email notifications through the Resend HTTP API.

func NewResendNotifier

func NewResendNotifier(config ResendConfig) (*ResendNotifier, error)

NewResendNotifier creates a Resend-backed email notifier.

func (*ResendNotifier) Send

func (notifier *ResendNotifier) Send(ctx context.Context, message Message) error

Send sends message through Resend and discards the returned provider ID.

func (*ResendNotifier) SendEmail

func (notifier *ResendNotifier) SendEmail(ctx context.Context, message Message) (ResendResult, error)

SendEmail sends message through Resend and returns the provider ID.

type ResendResult

type ResendResult struct {
	ID string `json:"id"`
}

ResendResult is the successful Resend API response.

type ResendStatusError

type ResendStatusError struct {
	StatusCode int
	Body       string
}

ResendStatusError describes a non-2xx Resend response.

func (*ResendStatusError) Error

func (err *ResendStatusError) Error() string

Error returns a safe delivery error without embedding provider response body.

func (*ResendStatusError) Retryable

func (err *ResendStatusError) Retryable() bool

Retryable reports whether the status is normally safe to retry.

func (*ResendStatusError) Unwrap

func (err *ResendStatusError) Unwrap() error

Unwrap returns the sentinel Resend delivery error.

type RetryConfig

type RetryConfig struct {
	Attempts   int
	Backoff    time.Duration
	MaxBackoff time.Duration
	RetryIf    func(error) bool
	Sleep      func(context.Context, time.Duration) error
}

RetryConfig configures RetryNotifier.

type RetryNotifier

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

RetryNotifier retries transient notification delivery failures.

func NewRetryNotifier

func NewRetryNotifier(notifier Notifier, config RetryConfig) (*RetryNotifier, error)

NewRetryNotifier wraps notifier with bounded retry behavior.

func (*RetryNotifier) Send

func (notifier *RetryNotifier) Send(ctx context.Context, message Message) error

Send retries message delivery according to config.

type SMTPConfig

type SMTPConfig struct {
	Host       string
	Port       int
	ServerName string
	Username   string
	Password   string
	From       string
	Channel    string
	TLSMode    SMTPTLSMode
	Timeout    time.Duration
}

SMTPConfig configures SMTPNotifier.

type SMTPNotifier

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

SMTPNotifier sends email notifications through SMTP.

func NewSMTPNotifier

func NewSMTPNotifier(config SMTPConfig) (*SMTPNotifier, error)

NewSMTPNotifier creates an SMTP-backed notifier.

func (*SMTPNotifier) Send

func (notifier *SMTPNotifier) Send(ctx context.Context, message Message) (err error)

Send delivers a notification message through SMTP.

type SMTPTLSMode

type SMTPTLSMode string

SMTPTLSMode controls how SMTPNotifier secures the SMTP connection.

const (
	// SMTPTLSModeStartTLS starts plaintext, then requires STARTTLS before auth or delivery.
	SMTPTLSModeStartTLS SMTPTLSMode = "starttls"

	// SMTPTLSModeImplicitTLS uses TLS from the first byte, commonly on port 465.
	SMTPTLSModeImplicitTLS SMTPTLSMode = "tls"

	// SMTPTLSModeNone disables TLS. Use only for trusted local relays.
	SMTPTLSModeNone SMTPTLSMode = "none"
)

func (SMTPTLSMode) String

func (mode SMTPTLSMode) String() string

type TimeoutNotifier

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

TimeoutNotifier applies a per-message timeout to a notifier.

func NewTimeoutNotifier

func NewTimeoutNotifier(notifier Notifier, timeout time.Duration) (*TimeoutNotifier, error)

NewTimeoutNotifier wraps notifier with a send timeout.

func (*TimeoutNotifier) Send

func (notifier *TimeoutNotifier) Send(ctx context.Context, message Message) error

Send applies the configured timeout around the wrapped notifier.

type WebhookConfig

type WebhookConfig struct {
	Endpoint          string
	Channel           string
	Headers           map[string]string
	Secret            []byte
	SignatureHeader   string
	TimestampHeader   string
	Timeout           time.Duration
	MaxResponseBytes  int64
	Client            *http.Client
	Now               func() time.Time
	AllowInsecureHTTP bool
}

WebhookConfig configures WebhookNotifier.

type WebhookNotifier

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

WebhookNotifier sends notifications to an HTTP endpoint as JSON.

func NewWebhookNotifier

func NewWebhookNotifier(config WebhookConfig) (*WebhookNotifier, error)

NewWebhookNotifier creates an HTTP webhook notifier.

func (*WebhookNotifier) Send

func (notifier *WebhookNotifier) Send(ctx context.Context, message Message) error

Send delivers message to the configured webhook endpoint.

type WebhookPayload

type WebhookPayload struct {
	ID       string            `json:"id,omitempty"`
	TenantID string            `json:"tenant_id"`
	Channel  string            `json:"channel"`
	To       string            `json:"to"`
	Subject  string            `json:"subject,omitempty"`
	Body     string            `json:"body,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty"`
}

WebhookPayload is the JSON request body sent by WebhookNotifier.

type WebhookStatusError

type WebhookStatusError struct {
	StatusCode int
	Body       string
}

WebhookStatusError describes a non-2xx webhook response.

func (*WebhookStatusError) Error

func (err *WebhookStatusError) Error() string

Error returns a safe delivery error without embedding provider response body.

func (*WebhookStatusError) Retryable

func (err *WebhookStatusError) Retryable() bool

Retryable reports whether the status is normally safe to retry.

func (*WebhookStatusError) Unwrap

func (err *WebhookStatusError) Unwrap() error

Unwrap returns the sentinel webhook delivery error.

Jump to

Keyboard shortcuts

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