pubsub

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package pubsub contains implementations for eventing providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewID

func NewID[T any]() string

NewID returns a unique identifier for the given type.

The string is a [TypeID](https://github.com/jetify-com/typeid), with the TopicName as the prefix.

func TopicName

func TopicName[T any]() string

TopicName returns the name of the topic for a type.

The name is a lower_snake_case string derived from the type name.

Types

type DeadLetter

type DeadLetter[T any] struct {
	// Error that resulted in the event being dead-lettered.
	Error string `json:"error"`
	Event T      `json:"event"`
}

DeadLetter represents a dead-lettered event.

It is itself an event.

func (DeadLetter[T]) EventID

func (d DeadLetter[T]) EventID() string

type Event

type Event[T any] struct {
	// contains filtered or unexported fields
}

Event represents a typed CloudEvent.

Marshals to/from a JSON CloudEvent (https://cloudevents.io/)

eg.

{
  "specversion": "1.0",
  "type": "github.com/alecthomas/zero.User",
  "source": "github.com/alecthomas/zero.PublishUserEvent",
  "id": "Bob",
  "data": {"name": "Bob", "age": 30}
}

func NewEvent

func NewEvent[T any](payload T) Event[T]

func (Event[T]) Created

func (e Event[T]) Created() time.Time

func (Event[T]) ID

func (e Event[T]) ID() string

ID returns the ID of the underlying payload.

func (Event[T]) MarshalJSON

func (e Event[T]) MarshalJSON() ([]byte, error)

func (Event[T]) Payload

func (e Event[T]) Payload() T

func (Event[T]) Source

func (e Event[T]) Source() string

func (*Event[T]) UnmarshalJSON

func (e *Event[T]) UnmarshalJSON(data []byte) error

type EventPayload

type EventPayload interface {
	// EventID returns the unique identifier for the event.
	//
	// This is required for idempotence and deduplication in the face of multiple retries.
	EventID() string
}

EventPayload _may_ be implemented by an event to specify an ID.

If not present, a unique TypeID will be generated using NewID.

type InMemoryTopic

type InMemoryTopic[T any] struct {
	// contains filtered or unexported fields
}

func (*InMemoryTopic[T]) Close

func (i *InMemoryTopic[T]) Close() error

func (*InMemoryTopic[T]) Publish

func (i *InMemoryTopic[T]) Publish(ctx context.Context, msg T) error

func (*InMemoryTopic[T]) Subscribe

func (i *InMemoryTopic[T]) Subscribe(ctx context.Context, handler func(context.Context, T) error) error

type Topic

type Topic[T any] interface {
	// Publish publishes a message to the topic.
	Publish(ctx context.Context, msg T) error
	// Subscribe subscribes to a topic.
	Subscribe(ctx context.Context, handler func(ctx context.Context, event T) error) error
	// Close the topic.
	Close() error
}

Topic represents a PubSub topic.

func NewMemoryTopic

func NewMemoryTopic[T any](logger *slog.Logger) Topic[T]

NewMemoryTopic creates a new in-memory Topic.

Directories

Path Synopsis
Package pubsubtest contains helper functions for testing pubsub.
Package pubsubtest contains helper functions for testing pubsub.

Jump to

Keyboard shortcuts

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