broker

package
v0.0.0-...-06c19a4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2014 License: MIT Imports: 5 Imported by: 13

Documentation

Index

Constants

View Source
const (
	ErrorWindowSize             = time.Second
	NumAllowedConsecutiveErrors = 3
)
View Source
const Version = "0.0.1"

Variables

View Source
var DefaultBroker = New()

Default package-level Broker instance.

Functions

func ListenAndServe

func ListenAndServe()

ListenAndServe calls DefaultBroker.ListenAndServe

func Monitor

func Monitor(monitorCh chan<- *EndpointCrashReport)

Monitor calls DefaultBroker.Monitor

func RegisterEndpointFactory

func RegisterEndpointFactory(factoryId string, factory EndpointFactory)

RegisterEndpointFactory calls DefaultBroker.RegisterEndpointFactory

func Terminate

func Terminate()

Terminate calls DefaultBroker.Terminate

func Terminated

func Terminated() <-chan struct{}

Terminated calls DefaultBroker.Terminated

func Wait

func Wait()

Wait calls DefaultBroker.Wait

Types

type Broker

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

Broker functions as a supervisor for the registered Endpoints. It uses the registered endpoint factory methods to create instances, which are then supervised and restarted on crash, if that does not happen too often.

It gives some guarantees to the endpoint objects, namely

  • ListenAndServe is called at most once.
  • Terminate is called at most once.

func New

func New() *Broker

Broker constructor function.

func (*Broker) ListenAndServe

func (b *Broker) ListenAndServe()

ListenAndServe instantiates service endpoints by invoking their respective factory methods that were added by RegisterEndpointFactory. Once an endpoint is instantiated, its ListenAndServe is called. If an error is returned from the endpoint-level ListenAndServe, the relevant endpoint is replaced by a new instance using the same factory function as before.

The broker keeps restarting crashed endpoints as long as it is not happening to frequently.

This method panics if it is called multiple times.

func (*Broker) Monitor

func (b *Broker) Monitor(monitorCh chan<- *EndpointCrashReport)

Monitor tells Broker to send all internal endpoint crash reports to the requested channel. This is the only way how to get endpoint errors out of Broker.

If there is no interest in the internal errors. Wait can be used to block until all the registered endpoints crashed or Close is called.

The channel passed to Monitor is closed at the same time Closed() channel is closed. It can be used interchangeably.

This methods panics if called after ListenAndServe.

func (*Broker) RegisterEndpointFactory

func (b *Broker) RegisterEndpointFactory(identifier string, factory EndpointFactory)

RegisterEndpointFactory adds the Endpoint factory to the set of factories that are invoked by the broker in the call to ListenAndServe to instantiate service endpoints.

Endpoints are restarted when they crash, provided that it is not happening too often and too frequently.

This methods panics if called after ListenAndServe.

func (*Broker) Terminate

func (b *Broker) Terminate()

Terminate terminates all the registered endpoints by invoking their respective Terminate methods. This way of shutting down is the clean one, the endpoints are supposed to perform some cleanup optionally and stop.

Terminate does not block, use Wait for blocking until all the endpoints are terminated.

func (*Broker) Terminated

func (b *Broker) Terminated() <-chan struct{}

Terminated returns a channel that is closed when Terminate returns.

func (*Broker) Wait

func (b *Broker) Wait()

Wait blocks until Terminated channel is closed.

type Endpoint

type Endpoint interface {
	// ListenAndServe puts the endpoint into the serving state.
	//
	// This method shall block until the endpoint is terminated.
	ListenAndServe() error

	// Close signals the endpoint to shut down gracefully.
	//
	// This method shall block until the endpoint is terminated.
	Close() error
}

Endpoint represents an endpoint providing certain service over certain transport. Every service is represented by a single Exchange where all transport endpoints for that particular service are interconnected.

type EndpointCrashReport

type EndpointCrashReport struct {
	FactoryId string
	Dropped   bool
	Error     error
}

type EndpointFactory

type EndpointFactory func() (Endpoint, error)

type ErrInvalidState

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

ErrInvalidState is returned when Broker methods are called in a wrong order.

func (ErrInvalidState) Error

func (err ErrInvalidState) Error() string

type ErrTerminated

type ErrTerminated struct {
	What string
}

func (*ErrTerminated) Error

func (err *ErrTerminated) Error() string

Jump to

Keyboard shortcuts

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