pubsub

package
v0.0.0-...-e1e9d1d Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package pubsub defines the standard interface for pub-sub.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockPublisher

type MockPublisher struct {
	PublishFunc func(context.Context, ...Msg) (int, error)
}

MockPublisher is a test mock for the Publisher interface.

func (*MockPublisher) Publish

func (m *MockPublisher) Publish(ctx context.Context, msgs ...Msg) (int, error)

type MockSubscriber

type MockSubscriber struct {
	SubscribeFunc   func(context.Context, string, bool, chan<- Msg) error
	UnsubscribeFunc func(context.Context, string, bool) error
}

MockSubscriber is a test mock for the Subscriber interface.

func (*MockSubscriber) Subscribe

func (m *MockSubscriber) Subscribe(ctx context.Context, topic string, glob bool, ch chan<- Msg) error

func (*MockSubscriber) Unsubscribe

func (m *MockSubscriber) Unsubscribe(ctx context.Context, topic string, glob bool) error

type Msg

type Msg struct {
	// Topic is the name of the topic for this message, either the one
	// where it will be published or the one it was received from.
	Topic string

	// Body is the payload of the message, unprocessed. No assumption is made
	// on the encoding of the body, those are the bytes as received from
	// the topic - it is up to the subscriber to make sense of it, e.g. by
	// unmarshaling as JSON to a struct - or as sent by the Publisher.
	Body []byte

	// Metadata is the metadata associated with this message. It is
	// implementation-dependent and might be left nil or ignored by
	// some implementations.
	Metadata map[string]interface{}
}

Msg is a pub-sub message. It is used both for the publishers and for the subscribers.

type Publisher

type Publisher interface {
	// Publish sends the msgs to their respective topic, identified by the
	// Topic field.
	//
	// On success, it must return len(msgs), nil. On error it should return
	// the number of msgs it successfully published, if any, and the error.
	// The msgs should be published in the order they are passed in as arguments,
	// so if the call returns e.g. 2 and an error, the caller should be able to
	// assume that the first 2 messages in the list were sent.
	//
	// When ctx is cancelled, publishing should stop and an error be
	// returned.
	Publish(ctx context.Context, msgs ...Msg) (int, error)
}

Publisher defines the method to publish a message to a topic.

type Subscriber

type Subscriber interface {
	// Subscribe registers the caller to the specified topic (allowing glob
	// matching if requested) and starts sending messages over ch. If the
	// implementation does not support glob matching, it should return an
	// error if allowGlob is true.
	//
	// When ctx is cancelled, no more messages should be sent and ch should
	// be closed. This is an important point to consider if the subscriber wants
	// to reuse the same channel for multiple topics.
	Subscribe(ctx context.Context, topic string, allowGlob bool, ch chan<- Msg) error

	// Unsubscribe unregisters the caller from the specified topic (with
	// glob matching if requested) and stops sending messages to its
	// registered channel, closing the channel. If the implementation does
	// not support glob matching, it should return an error if allowGlob
	// is true.
	//
	// When ctx is cancelled, the unsubscribe call should fail and an error
	// should be returned.
	Unsubscribe(ctx context.Context, topic string, allowGlob bool) error
}

Subscriber defines the methods to subscribe to and unsubscribe from pub-sub topics.

Jump to

Keyboard shortcuts

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