pubsub

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pubsub is used for events publishing and subscribing for them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	// Name returns event name.
	Name() string

	// Data returns event payload.
	Data() []byte
}

Event is something that happens (sorry for the tautology).

func NewAllRequestsDeletedEvent

func NewAllRequestsDeletedEvent() Event

NewAllRequestsDeletedEvent creates an event, that means "all requests was deleted".

func NewRequestDeletedEvent

func NewRequestDeletedEvent(requestID string) Event

NewRequestDeletedEvent creates an event, that means "request with passed ID was deleted".

func NewRequestRegisteredEvent

func NewRequestRegisteredEvent(requestID string) Event

NewRequestRegisteredEvent creates an event, that means "new request with passed ID was registered".

type InMemory

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

InMemory publisher/subscriber uses memory for events publishing and delivering to the subscribers. Useful for application "single node" mode running or unit testing.

Publishing/subscribing events order is NOT guaranteed.

Node: Do not forget to Close it after all. Closed publisher/subscriber cannot be opened back.

func NewInMemory

func NewInMemory() *InMemory

NewInMemory creates new inmemory publisher/subscriber.

func (*InMemory) Close

func (ps *InMemory) Close() error

Close this publisher/subscriber. This function can be called only once.

func (*InMemory) Publish

func (ps *InMemory) Publish(channelName string, event Event) error

Publish an event into passed channel. Publishing is non-blocking operation.

func (*InMemory) Subscribe

func (ps *InMemory) Subscribe(channelName string, channel chan<- Event) error

Subscribe to the named channel and receive Event's into the passed channel. Channel must be created on the calling side and NOT to be closed until subscription is not Unsubscribe*ed.

Note: do not forget to call Unsubscribe when all is done.

func (*InMemory) Unsubscribe

func (ps *InMemory) Unsubscribe(channelName string, channel chan Event) error

Unsubscribe the subscription to the named channel for the passed events channel. Be careful with channel closing, this can call the panics if some Event's scheduled for publishing.

type Publisher

type Publisher interface {
	// Publish an event into passed channel.
	Publish(channelName string, event Event) error
}

Publisher allows to publish Event*s.

type Redis

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

Redis publisher/subscriber uses redis server for events publishing and delivering to the subscribers. Useful for application "distributed" mode running.

Publishing/subscribing events order and delivering (in cases then there is no one active subscriber for the channel) is NOT guaranteed.

Node: Do not forget to Close it after all. Closed publisher/subscriber cannot be opened back.

func NewRedis

func NewRedis(ctx context.Context, rdb *redis.Client) *Redis

NewRedis creates new redis publisher/subscriber.

func (*Redis) Close

func (ps *Redis) Close() error

Close this publisher/subscriber. This function can be called only once.

func (*Redis) Publish

func (ps *Redis) Publish(channelName string, event Event) error

Publish an event into passed channel.

func (*Redis) Subscribe

func (ps *Redis) Subscribe(channelName string, channel chan<- Event) error

Subscribe to the named channel and receive Event's into the passed channel.

Note: that this function does not wait on a response from redis server, so the subscription may not be active immediately.

func (*Redis) Unsubscribe

func (ps *Redis) Unsubscribe(channelName string, channel chan Event) error

Unsubscribe the subscription to the named channel for the passed events channel. Be careful with channel closing, this can call the panics if some Event's scheduled for publishing.

type Subscriber

type Subscriber interface {
	// Subscribe to the named channel and receive Event's into the passed channel.
	//
	// Keep in mind - passed channel (chan) must be created on the caller side and channels without active readers
	// (or closed too early) can block application working (or break it at all).
	//
	// Also do not forget to Unsubscribe from the channel.
	Subscribe(channelName string, channel chan<- Event) error

	// Unsubscribe the subscription to the named channel for the passed events channel.
	Unsubscribe(channelName string, channel chan Event) error
}

Subscriber allows to Subscribe and Unsubscribe for Event*s.

Jump to

Keyboard shortcuts

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