Documentation ¶
Index ¶
- Constants
- Variables
- func ListenAndServe()
- func Monitor(monitorCh chan<- *EndpointCrashReport)
- func RegisterEndpointFactory(factoryId string, factory EndpointFactory)
- func Terminate()
- func Terminated() <-chan struct{}
- func Wait()
- type Broker
- type Endpoint
- type EndpointCrashReport
- type EndpointFactory
- type ErrInvalidState
- type ErrTerminated
Constants ¶
const ( ErrorWindowSize = time.Second NumAllowedConsecutiveErrors = 3 )
const Version = "0.0.1"
Variables ¶
var DefaultBroker = New()
Default package-level Broker instance.
Functions ¶
func Monitor ¶
func Monitor(monitorCh chan<- *EndpointCrashReport)
Monitor calls DefaultBroker.Monitor
func RegisterEndpointFactory ¶
func RegisterEndpointFactory(factoryId string, factory EndpointFactory)
RegisterEndpointFactory calls DefaultBroker.RegisterEndpointFactory
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 (*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.
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 EndpointFactory ¶
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