stream

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Overview

Package stream provides interfaces and implementations for publishing events to streaming services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeSNSEvent

func NormalizeSNSEvent(raw json.RawMessage) (events.SNSEvent, error)

NormalizeSNSEvent accepts a raw Lambda invocation payload and returns an events.SNSEvent regardless of whether the Lambda was invoked by a direct SNS subscription (prod) or via an SQS ingress queue with raw_message_delivery=true (dev).

In the SQS path each record's Body contains the raw CloudWatchEvent JSON (the same content that SNS would place in SNSEntity.Message), so we wrap it in a synthetic SNSEventRecord to keep downstream handlers unchanged.

Types

type DynamoDBHandler

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

DynamoDBHandler filters DynamoDB stream events for INSERT records only.

func NewDynamoDBHandler

func NewDynamoDBHandler(loggers ...*slog.Logger) *DynamoDBHandler

NewDynamoDBHandler creates a new DynamoDB handler.

func (*DynamoDBHandler) Handle

Handle filters DynamoDB stream events for INSERT records only. Non-INSERT events (MODIFY, REMOVE) are ignored.

type GroupIDExtractor

type GroupIDExtractor func(event evt.SerializedEvent) (groupID string, ok bool)

GroupIDExtractor maps a serialized domain event to an SNS FIFO MessageGroupId. Returning ("", false) skips FIFO publish for the event, leaving the standard topic as the sole delivery path.

type Handler

type Handler[Event, Record any] interface {
	// Handle processes an event and returns records to publish.
	Handle(ctx context.Context, event Event) ([]Record, error)
}

Handler processes events and converts them to records for publishing.

type Option

type Option func(*SNSPublisher) error

Option customizes SNSPublisher construction.

func WithFIFOTarget

func WithFIFOTarget(topicARN string, extractor GroupIDExtractor) Option

WithFIFOTarget configures an SNS FIFO companion topic. Events for which the extractor returns ok=true are published to the FIFO topic with the returned MessageGroupId; the serialized event ID is used as MessageDeduplicationId. Events where the extractor returns ok=false are not forwarded to FIFO.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger configures the logger used by the publisher. If unset, slog.Default() is used.

type PublishFromStream

type PublishFromStream[Event any, Record any] struct {
	// contains filtered or unexported fields
}

PublishFromStream combines a Handler and Publisher to process events from one stream and publish to another.

func NewPublishFromStream

func NewPublishFromStream[Event any, Record any](handler Handler[Event, Record], publisher Publisher[Record]) *PublishFromStream[Event, Record]

NewPublishFromStream creates a new PublishFromStream.

func (*PublishFromStream[Event, Record]) Handle

func (p *PublishFromStream[Event, Record]) Handle(ctx context.Context, event Event) error

Handle handles an event and publishes the resulting records.

type PublishResult

type PublishResult struct {
	// FailedIndices contains the indices of records that failed to publish.
	// These can be used to construct DynamoDB batch item failures.
	FailedIndices []int
	// PublishedCount is the number of records successfully published.
	PublishedCount int
	// SkippedCount is the number of records skipped (e.g., empty NewImage).
	SkippedCount int
	// DroppedMalformedCount is the number of malformed records explicitly dropped.
	DroppedMalformedCount int
}

PublishResult contains the outcome of publishing a batch of records.

type Publisher

type Publisher[Record any] interface {
	// Publish publishes records to the stream.
	Publish(ctx context.Context, records []Record) error
}

Publisher publishes records to a stream.

type SNSPublisher

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

SNSPublisher converts event-log INSERT records into CloudWatchEvent envelopes and publishes them to SNS. WithFIFOTarget, eligible events also go to a FIFO topic with a group ID and event-ID deduplication key.

func NewSNSPublisher

func NewSNSPublisher(client snsPublishClient, topicARN string, source string, opts ...Option) (*SNSPublisher, error)

NewSNSPublisher creates a new SNS publisher. source is the logical event source (e.g. "example.events").

func (*SNSPublisher) Publish

func (p *SNSPublisher) Publish(ctx context.Context, records []events.DynamoDBEventRecord) (*PublishResult, error)

Publish converts DynamoDB records to domain events and publishes them to SNS using batched PublishBatch calls (up to 10 messages per call). Returns a PublishResult with indices of failed records for batch failure handling.

Jump to

Keyboard shortcuts

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