event

package
v0.35.6 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: GPL-3.0 Imports: 6 Imported by: 1 module, 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 event is a small in-process hub. Publishing never blocks: each subscriber has a bounded queue and an event that does not fit is dropped for that subscriber and counted, so a slow subscriber costs the publisher nothing and the other subscribers nothing.

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 fans events out to its subscribers. A nil Bus is valid and drops everything.

func NewBus

func NewBus() *Bus

NewBus returns an empty Bus.

func (*Bus) Close

func (b *Bus) Close()

Close ends all subscriptions. Events still queued can be received, then the channels report closed. It is safe to call more than once.

func (*Bus) Publish

func (b *Bus) Publish(eventType string, data any)

Publish hands an event to every subscriber with room for it. After Close it does nothing.

func (*Bus) Subscribe

func (b *Bus) Subscribe(queue int) *Subscription

Subscribe returns a subscription with a queue of the given size, at least one. On a closed Bus its channel is already closed.

type Event

type Event struct {
	ID   string    `json:"id"`
	Type string    `json:"type"`
	Time time.Time `json:"time"`
	Data any       `json:"data,omitempty"`
}

Event is one thing that happened. ID is unique across restarts, so a receiver can use it to ignore a delivery it has seen. Data must not change after publishing: it is read by subscribers later, on their goroutines.

type Subscription

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

Subscription is one subscriber's queue.

func (*Subscription) C

func (s *Subscription) C() <-chan Event

C returns the queue. It is closed when the subscription or the Bus closes.

func (*Subscription) Close

func (s *Subscription) Close()

Close ends the subscription. It is safe to call more than once and after the Bus closed.

func (*Subscription) Dropped

func (s *Subscription) Dropped() uint64

Dropped returns how many events did not fit in the queue.

Jump to

Keyboard shortcuts

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