event

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package event provides types for publishing and consuming events from event buses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal[T Payload](envelope Envelope) (T, error)

Unmarshal the event payload into the type specified via the type parameter.

Types

type Envelope

type Envelope struct {
	// A unique identifier for the event.
	ID string `json:"id"`
	// The time the event was published.
	Timestamp time.Time `json:"timestamp"`
	// Denotes the structure of the Payload field and the type to use for decoding.
	Type string `json:"type"`
	// The raw JSON of the event payload.
	Payload json.RawMessage `json:"payload"`
}

The Envelope type describes the structure of events published to and read from an event bus.

type Handler

type Handler func(ctx context.Context, e Envelope) error

The Handler type is a function used to handle a single inbound event.

type Payload

type Payload interface {
	// Type should return a string unique to the event type.
	Type() string
	// Key should return a string used to preserve ordering of events.
	Key() string
}

The Payload interface describes types that can be used as event payloads.

type Reader

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

The Reader type is used to consume messages from an event bus.

func NewReader

func NewReader(ctx context.Context, url string) (*Reader, error)

NewReader returns a new instance of the Reader type that will consume events as described in its URL string. See the gocloud.dev documentation for more information on provider specific urls.

func (*Reader) Check added in v0.9.0

func (r *Reader) Check(_ context.Context) error

Check returns the last non-nil error when trying to read an event. This method is used to implement the operation.Checker interface for use in health checks.

func (*Reader) Close

func (r *Reader) Close() error

Close the connection to the event bus.

func (*Reader) Name added in v0.9.0

func (r *Reader) Name() string

Name returns "event-reader" This method is used to implement the operation.Checker interface for use in health checks.

func (*Reader) Read

func (r *Reader) Read(ctx context.Context, types []string, h Handler) error

Read messages from the event bus whose types appear in the types parameter. For each event, the Handler is invoked. If the handler returns an error, the message is nack'd where supported by the event bus. This method blocks until the Handler returns an error or the provided context is cancelled.

type Writer

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

The Writer type is used to publish messages onto an event bus.

func NewWriter

func NewWriter(ctx context.Context, url string) (*Writer, error)

NewWriter returns a new instance of the Writer type that will publish events to the bus described in its URL string. See the gocloud.dev documentation for more information on provider specific urls.

func (*Writer) Check added in v0.9.0

func (w *Writer) Check(_ context.Context) error

Check returns the last non-nil error when trying to write an event. This method is used to implement the operation.Checker interface for use in health checks.

func (*Writer) Close

func (w *Writer) Close() error

Close the connection to the event bus.

func (*Writer) Name added in v0.9.0

func (w *Writer) Name() string

Name returns "event-writer" This method is used to implement the operation.Checker interface for use in health checks.

func (*Writer) Write

func (w *Writer) Write(ctx context.Context, e Payload) error

Write an event onto the bus. Messages must implement the Payload interface and are wrapped in an Envelope before publishing. If the event bus supports message keys/partitioning the Payload.Key method will be used to populate it.

Jump to

Keyboard shortcuts

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