events

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package events is hearth's event runtime: 1 event is a CloudEvents 1.0 envelope with no data, its author is the caller of the change's context, and its topic names the app, the object, the verb and the segments of the object's path.

An app writes 1 event into the outbox of the change's transaction (Writer), and a relay publishes it to a Transport: in process (package inproc), or to the broker on lodge's socket (package broker). A generated publisher (protoc-gen-hearth-events) gives the app 1 Spec and 2 functions for each event message of its Protobuf files.

Index

Constants

View Source
const MaxEnvelopeLen = 32 << 10

MaxEnvelopeLen is the longest envelope, in bytes: well below the 64 KiB payload that lodge's broker takes, which also carries the message's headers, so that 1 event can never be a row that no transport accepts.

View Source
const MaxTopicLen = 1024

MaxTopicLen is the longest topic, in bytes. A longer topic is an error, so that 1 event can never block the relay.

View Source
const SpecVersion = "1.0"

SpecVersion is the CloudEvents version of every event of hearth.

Variables

View Source
var ErrNoCaller = errors.New("events: no caller in the context, so the event has no author")

ErrNoCaller is the error of an event that has no author: the caller of the context is the event's only author.

Functions

func EncodeSegment

func EncodeSegment(segment string) string

EncodeSegment returns 1 path segment as 1 topic token: every byte outside `[A-Za-z0-9-]` is percent-encoded with upper-case hex.

func Match

func Match(filter, topic string) bool

Match reports whether 1 broker filter takes 1 topic: the 2 split on ".", "*" matches exactly 1 token, and ">" as the filter's last token matches 1 or more tokens.

func ReadFilters

func ReadFilters(specs []Spec, item authz.Item) ([]string, error)

ReadFilters returns the broker filters that a consumer subscribes to, to read the events that 1 grant item's capability covers: 1 filter for each event spec whose read permission is the item's capability, in the order of the specs.

An event spec with no selector carries no path, so its filter is its topic alone. A spec with a selector reads the item's path for that selector: the root path gives the spec's topic and the events below it, and any other path adds its 1 segment for each segment of the path. 1 item that names another selector at a path that is not the root covers no event of that spec, because the event carries no value of that selector, which is the root.

A capability that no spec has gives an empty, non-nil list. Each spec that the item's capability covers is checked, and a path that is no path is an error.

func TopicOf

func TopicOf(spec Spec, path string) (string, string, error)

TopicOf returns the topic of 1 event and the canonical form of its path: the spec's topic, and 1 segment for each segment of the path. The root path adds no segment.

It checks the spec, the path and the topic's length. An event that carries no selector has no path, so a path is then an error.

func ValidFilter

func ValidFilter(filter string) error

ValidFilter checks 1 broker filter: it is not empty, no token is empty, ">" is the last token only, and it holds no space.

Types

type Event

type Event struct {
	// ID is a UUIDv7: the event's own id, never the object's.
	ID string
	// Source is the app's name: "/" and the app's id.
	Source string
	// Type is the event's CloudEvents type.
	Type string
	// Subject is the object's id.
	Subject string
	// Time is when the event was made, in UTC.
	Time time.Time
	// Author is the caller's principal, or "local" for the app's own user.
	Author string
	// Selector is the object's canonical path, or "" for an event that
	// carries no selector.
	Selector string
	// Topic is the broker's topic. It is not in the envelope.
	Topic string
}

Event is 1 CloudEvents 1.0 event with no data.

func New

func New(ctx context.Context, spec Spec, subject, path string) (Event, error)

New returns the event of 1 change, made now.

It is NewAt with the wall clock and the system's source of random bytes, so that a test makes an event of a fixed time and a fixed id with NewAt.

func NewAt

func NewAt(ctx context.Context, spec Spec, subject, path string, now time.Time, random io.Reader) (Event, error)

NewAt returns the event of 1 change at 1 time, with the id that random gives the extra bytes of.

The author is the caller of the context, and nothing else: a context with no caller, or a caller with no principal, is an error that wraps ErrNoCaller. The event's selector is the canonical form of path, or "" when the event carries no selector.

func Parse

func Parse(data []byte) (Event, error)

Parse reads 1 event from its CloudEvents structured JSON form.

It is strict: an unknown member, a member that the form requires and the data lacks, a duplicate member, a specversion other than SpecVersion and a bad time are errors, and a member's name is case-sensitive. The event's topic is not in the form, so it is "".

func (Event) MarshalJSON

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

MarshalJSON implements json.Marshaler: the event's CloudEvents structured JSON form, with no data.

func (Event) Validate

func (e Event) Validate() error

Validate checks 1 event before its change commits, and before a transport publishes it: every required member is there, the topic and the envelope are within their limits, and the id is a lower-case UUID.

A writer of the change's transaction calls it, so an event that no transport could take fails inside the transaction, and no row blocks the relay.

type Spec

type Spec struct {
	// App is the app's id, such as "demo".
	App string
	// Type is the event's CloudEvents type, such as
	// "org.frostyard.demo.note.created".
	Type string
	// Topic is the event's topic without the path: "<app>.<object>.<verb>",
	// and ".<selector>" when the event carries a selector.
	Topic string
	// Selector is the selector's name, such as "group", or "" when the event
	// carries none.
	Selector string
	// ReadPermission is the capability that a consumer of the event needs.
	ReadPermission string
}

Spec is 1 event type of an app; the plugin generates 1 for each event message.

type Transport

type Transport interface {
	// Publish publishes 1 event.
	Publish(ctx context.Context, e Event) error
}

Transport publishes 1 event. Publish returns nil only once the transport holds the event: for the broker, that is JetStream's acknowledgement.

type Writer

type Writer interface {
	// Write writes 1 event in the transaction of the context.
	Write(ctx context.Context, e Event) error
}

Writer writes 1 event in the change's transaction: the outbox.

Directories

Path Synopsis
Package broker is hearth's transport to the broker that lodge embeds: it publishes each event to the NATS server, through lodge's Unix socket, and waits for JetStream's acknowledgement before it returns (lodge ADR-0022).
Package broker is hearth's transport to the broker that lodge embeds: it publishes each event to the NATS server, through lodge's Unix socket, and waits for JetStream's acknowledgement before it returns (lodge ADR-0022).
Package inproc is hearth's in-process event transport: 1 bus delivers each event to the subscribers of the same process, on the goroutine that published it, and it drops an event that it has already delivered.
Package inproc is hearth's in-process event transport: 1 bus delivers each event to the subscribers of the same process, on the goroutine that published it, and it drops an event that it has already delivered.
Package outbox is hearth's transactional outbox: an app writes each event in the transaction that makes the change, and a relay publishes the events in order, after the transaction has committed.
Package outbox is hearth's transactional outbox: an app writes each event in the transaction that makes the change, and a relay publishes the events in order, after the transaction has committed.

Jump to

Keyboard shortcuts

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