events

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package events is the in-process bus services use to reach each other.

It exists so they need not: a bucket knows nothing about functions, and a function knows nothing about buckets. Both know this bus, which is the only way anything here crosses a service boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

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

Bus delivers events to subscribers.

func New

func New() *Bus

New returns an empty bus.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, e Event)

Publish delivers an event to every interested subscriber, asynchronously.

The publisher is never blocked: an object write must not wait on whatever a function does with the news. Use Sync to wait for delivery.

func (*Bus) Started

func (b *Bus) Started() uint64

Started counts deliveries ever launched. A drain compares it across a Sync to tell whether that pass triggered new work, and so whether to drain again.

func (*Bus) Subscribe

func (b *Bus) Subscribe(match Match, fn Handler) func()

Subscribe registers a handler for events match accepts, and returns a function that removes it.

func (*Bus) Sync

func (b *Bus) Sync()

Sync waits for every event published so far to be delivered.

Delivery is asynchronous, so without this a test would have to poll or sleep to see an effect. It is what makes an event-driven assertion deterministic.

type Event

type Event struct {
	// Type is the event name a trigger matches on, e.g.
	// google.storage.object.finalize.
	Type string

	// Source identifies what produced it, e.g.
	// //storage.googleapis.com/projects/_/buckets/my-bucket. A trigger matches
	// its resource against the tail of this.
	Source string

	// Subject names the thing within the source, e.g. objects/logs/app.log.
	Subject string

	// Resource, Service and Kind describe the affected resource the way a
	// first-generation function's context reports it.
	Resource string
	Service  string
	Kind     string

	Time time.Time

	// Data is the payload, in the shape the service's API returns it.
	Data map[string]any
}

Event is something that happened.

type Handler

type Handler func(context.Context, Event)

Handler receives an event. It runs on the bus's own goroutine, not the publisher's, so a slow subscriber cannot stall an upload.

type Match

type Match func(Event) bool

Match decides whether a subscriber wants an event.

Jump to

Keyboard shortcuts

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