events

package
v0.0.0-...-3c02646 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// NATS connects to an external NATS cluster to provide event subscribing and publishing across
	// multiple Response instances each connected to the same NATS cluster. This is the preferred
	// production system.
	NATS = "nats"
)

Variables

This section is empty.

Functions

func NewMessage

func NewMessage(body interface{}, events ...Event) ([]*pubsub.Message, error)

NewMessage creates a new Message containing the event and body. This message can be sent using the Publish method.

func Unmarshal

func Unmarshal(msg *pubsub.Message, into interface{}) error

Unmarshal unmarshals the *pubsub.Message into the provided interface.

Types

type Driver

type Driver string

Driver is a type of Driver available to use as a backing pubsub implementation.

const (
	// Embedded is an embedded pubsub driver that provides in-memory event subscribing and publishing
	// and is acceptable for testing and development purposes. In general you should use a more robust
	// pubsub implementation in production.
	Embedded Driver = "embedded"
)

type Event

type Event string

Event is the type of event being published.

type Listener

type Listener struct {
	Event Event
	ID    string
	// contains filtered or unexported fields
}

Listener is a single subscribed listener for an event.

func (*Listener) Channel

func (l *Listener) Channel() <-chan *pubsub.Message

Channel returns the channel that emits pubsub.Message's whenever they are received. Channel will also

func (*Listener) Unsubscribe

func (l *Listener) Unsubscribe()

Unsubscribe unsubscribes the listener. The listener will no longer receive messages from the subscribed event.

type Options

type Options struct {
	Logger  log.ComponentLogger
	Driver  Driver
	Weight  int64
	Subject string
	URL     string
}

Options configures all of the options for the PubSub implementation.

type PubSub

type PubSub interface {
	Publisher
	Subscriber

	// Shutdown should be called once before the program exits to close all existing connections
	// and finish processing any events already in-flight.
	Close(ctx context.Context)
}

PubSub is a Publisher/Subscriber implementation.

func New

func New(ctx context.Context, options *Options) (PubSub, error)

New creates a new PubSub implementation that can be used to publish and subscribe to events in Response.

type Publisher

type Publisher interface {
	// Publish publishes events to the Response cluster using the configured pubsub implementation.
	Publish(ctx context.Context, messages ...*pubsub.Message) error
}

Publisher is implemented by pubsub implementations that can publish events.

type Subscriber

type Subscriber interface {
	// On subscribes to an event until the listener is unsubscibed either internally or by calling Subscriber.Unsubscribe()
	// with the Listener or calling Listener.Unsubscribe() directly.
	On(ctx context.Context, event Event, buffer int) *Listener

	// Once works similarly to On, however nlike On, when Once receives its first event the listener will be closed automatically.
	Once(ctx context.Context, event Event) *Listener

	// Unsubscribe unsubscribes the given Listener by calling the Listener.Unsubscribe() method.
	Unsubscribe(lis *Listener)
}

Subscriber is implemented by pubsub implementations and supports subscribing to events.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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