exchange

package
v0.0.0-...-ebe3feb Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2014 License: MIT Imports: 4 Imported by: 4

Documentation

Overview

go-exchange is an in-process message (or event) exchange, for now featuring the publish-subscribe messaging pattern. The interestng part is, however, that callbacks are not registered for exactly matching event topic, but rather event topic prefixes.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidState    = errors.New("invalid exchange state")
	ErrInvalidHandle   = errors.New("invalid handle")
	ErrHandlesDepleted = errors.New("handles depleted")
)

Functions

This section is empty.

Types

type Event

type Event interface{}

type EventHandler

type EventHandler func(Topic, Event)

type Exchange

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

Exchange is the struct this package is named after, or the other way around.

For now Exchange supports the publish-subscribe messaging pattern. It is based on topics and callbacks can be registered for topic prefixes, not only events that match exactly the chosen topic string.

Exchange is thread-safe.

func New

func New() *Exchange

Constructor function for Exchange, nothing special here.

func (*Exchange) Publish

func (exchange *Exchange) Publish(topic Topic, event Event) error

Publish can be used for inserting the events into the exchange.

This methods triggers all the relevant handlers and runs them in separate goroutines.

func (*Exchange) Subscribe

func (exchange *Exchange) Subscribe(topicPrefix Topic, handler EventHandler) (Handle, error)

Subscribe registers handler to be invoked on any event that matches prefix topicPrefix.

The handlers registered with Subscribe are invoked asynchronously using go when an event matching the relevant prefix is received.

func (*Exchange) Terminate

func (exchange *Exchange) Terminate() error

Terminate manes it impossible to insert any additional events into Exchange. Then it waits for all running handlers to return. Then it returns as well.

func (*Exchange) Unsubscribe

func (exchange *Exchange) Unsubscribe(handle Handle) error

Unsubscribe cancels the event handler represented by handle.

func (*Exchange) Wait

func (exchange *Exchange) Wait() error

Wait blocks until Terminate is called and all running goroutines return.

type Handle

type Handle uint

type Topic

type Topic []byte

Jump to

Keyboard shortcuts

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