bus

package
v2.0.0-...-3280c16 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package bus provides communication tools used by Gakisitor's services.

Index

Constants

This section is empty.

Variables

View Source
var ErrChannelNotFound = errors.New("channel not found")

ErrChannelNotFound occurs when the requested channel doesn't exist.

View Source
var ErrChannelSubscriberAlreadyExists = errors.New("channel subscriber already exists")

ErrChannelSubscriberAlreadyExists occurs when a subscriber already subscribed to the channel.

View Source
var ErrChannelSubscriberNotFound = errors.New("channel subscriber not found")

ErrChannelSubscriberNotFound occurs when a channel exists but no subscribers in.

View Source
var ErrPublishTimeout = errors.New("publish timeout")

ErrPublishTimeout occurs when the event publishing timeout.

View Source
var ErrReplyTimeout = errors.New("reply has timeout")

ErrReplyTimeout occurs when the subscriber (ak. the Bus.Subscribe handler) not sent reply or if it takes to mush time before sent it.

View Source
var ErrSubscriberDeleted = errors.New("subscriber closed")

ErrSubscriberDeleted occurs when the subscriber was deleted (ak. unsubscribe).

Functions

This section is empty.

Types

type Bus

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

Bus is an implementation of the Sub/Pub design pattern. It provides a simple way to send data to several handlers, at same times. It also manages the handlers to prevent some errors like handler's crash.

func New

func New() *Bus

New create a new instance of Bus.

func (*Bus) Publish

func (bus *Bus) Publish(channel string, data interface{}, handler ReplyHandler)

Publish publish a message to a channel. A reply handler can be provided in order to receive reply and/or catch errors.

func (*Bus) Subscribe

func (bus *Bus) Subscribe(channel string, handler EventConsumer) error

Subscribe links an handler to a channel. See EventConsumer for more information about the handler.

func (*Bus) Unsubscribe

func (bus *Bus) Unsubscribe(channel string, handler EventConsumer) error

Unsubscribe removes a subscriber linked with a channel. If all subscribers linked with a channel are removed, the channel will be removed (and can create ErrChannelNotFound errors during publishing on the channel).

type Event

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

Event represents a message sent through the Bus. It provides methods to get the payload (ak. message) and to reply to the sender.

func (*Event) Message

func (event *Event) Message() interface{}

Message return the payload (ak. message) of the event.

func (*Event) Reply

func (event *Event) Reply() chan<- interface{}

Reply provide a channel to reply directly to the publisher.

type EventConsumer

type EventConsumer func(event *Event, err error)

EventConsumer is the handler in charge of receiving the message. The message is contained in the Event and, if an error occurs, the consumer was notified. Warning: an EventConsumer is only called when an event was sent, in a goroutine. DO NOT LOOP INFINITELY AND TAKE CARE OF CONCURRENCY.

type ReplyConsumer

type ReplyConsumer func(data interface{}, err error)

ReplyConsumer is the handler in charge of receiving the reply. It call only once during the publishing process (after receiving reply or if an error occurs). DO NOT LOOP INFINITELY.

type ReplyHandler

type ReplyHandler interface {
	// contains filtered or unexported methods
}

ReplyHandler provides an interface for handling replies during publishing (for Bus.Publish). It can't be overloaded, but two way are provides to generate ReplyHandler.

func AsyncReplyHandler

func AsyncReplyHandler(consumer ReplyConsumer) ReplyHandler

AsyncReplyHandler generate an asynchronous ReplyHandler. It means that if you use this ReplyHandler with Bus.Publish, your Bus.Publish call can be finished before the handler ending. TAKE CARE OF CONCURRENCY.

func SyncReplyHandler

func SyncReplyHandler(consumer ReplyConsumer) ReplyHandler

SyncReplyHandler generate a synchronous ReplyHandler. It means that if you use this ReplyHandler with Bus.Publish, your Bus.Publish call will never finished before the handler ending.

Jump to

Keyboard shortcuts

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