routing

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package routing provides pipeline stages for routing messages to endpoints and within endpoints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	Routes HandlerTable
	Logger twelf.Logger
	// contains filtered or unexported fields
}

Dispatcher is an inbound pipeline stage that routes messages to the appropriate MessageHandler instances according to a "handler table".

func (*Dispatcher) Accept

Accept dispatches env to zero or more message handlers as per the dispatch table.

The context passed to each handler contains the message envelope, such that any messages sent using s within that context are configured as children of env.

Each message handler is invoked on its own goroutine.

func (*Dispatcher) Initialize

func (d *Dispatcher) Initialize(ctx context.Context, ep *endpoint.Endpoint) error

Initialize is called during initialization of the endpoint, after the transport is initialized. It can be used to inspect or further configure the endpoint as per the needs of the pipeline.

type EndpointTable

type EndpointTable []route

EndpointTable is set of rules that determine the destination endpoint for unicast messages.

func NewEndpointTable

func NewEndpointTable(r ...string) (EndpointTable, error)

NewEndpointTable returns an endpoint table that choses a destination endpoint based on a mapping of protocol buffers message name to endpoint name.

r is a sequence of key/value pairs that form the routing rules. The keys may be a fully-qualified protocol buffers message name, or a protocol buffers package name. The corresponding value is the endpoint name that such messages are routed to.

func (EndpointTable) Lookup

func (t EndpointTable) Lookup(mt ax.MessageType) (ep string, ok bool)

Lookup returns the endpoint that messages of type mt are routed to.

Routes are chosen based on the longest match, hence an exact match to the message type is favored, then the longest matching package name, and finally the empty string, which becomes the default route.

If there is no default route and mt is a top-level message, that is, a message with no package name, then ok is false, indicating no route could be found.

type HandlerTable

type HandlerTable map[ax.MessageType][]MessageHandler

HandlerTable is a set of rules that determines which handlers receive a message of a specific type.

func NewHandlerTable

func NewHandlerTable(handlers ...MessageHandler) (HandlerTable, error)

NewHandlerTable returns a handler table that locates message handlers based on the message types that they handle.

func (HandlerTable) Lookup

func (ht HandlerTable) Lookup(mt ax.MessageType) []MessageHandler

Lookup returns the message handlers that handle mt.

type MessageHandler

type MessageHandler interface {
	// MessageTypes returns the set of messages that the handler intends
	// to handle.
	//
	// The return value should be constant as it may be cached by various
	// independent stages in the message pipeline.
	MessageTypes() ax.MessageTypeSet

	// HandleMessage invokes application-defined logic that handles a
	// message.
	//
	// It may panic if env.Message is not one of the types described by
	// MessageTypes().
	HandleMessage(ctx context.Context, s ax.Sender, mctx ax.MessageContext) error
}

MessageHandler is an interface for application-defined message handlers.

Message handlers are typically the last stage in the inbound message pipeline. Each message handler declares its interest in a specific set of message types and is notified when any matching message arrives.

func NewMessageHandler

func NewMessageHandler(v interface{}) MessageHandler

NewMessageHandler returns a new message handler that dispatches messages to methods on an arbitrary value.

For each message type to be handled, the value must implement a "handler" method that adheres to one of the following signatures:

func (msg *<T>)
func (msg *<T>, mctx ax.MessageContext)
func (ctx context.Context, s ax.Sender, msg *<T>) error
func (ctx context.Context, s ax.Sender, msg *<T>, mctx ax.MessageContext) error

Where T is a struct type that implements ax.Message.

The names of handler methods are not meaningful. By convention the methods are named the same as the message they accept, such as:

func (*BankAccount) CreditAccount(*messages.CreditAccount)

type Router

type Router struct {
	// Routes is the table used to determine the destination endpoint.
	Routes EndpointTable

	// Next is the next stage in the pipeline.
	Next endpoint.OutboundPipeline
	// contains filtered or unexported fields
}

Router is an outbound pipeline stage that choses a destination endpoint for unicast messages based on the message type.

func (*Router) Accept

func (r *Router) Accept(ctx context.Context, env endpoint.OutboundEnvelope) error

Accept populates the evn.DestinationEndpoint field of unicast messages that do not already have a DestinationEndpoint specified.

func (*Router) Initialize

func (r *Router) Initialize(ctx context.Context, ep *endpoint.Endpoint) error

Initialize is called during initialization of the endpoint, after the transport is initialized. It can be used to inspect or further configure the endpoint as per the needs of the pipeline.

Jump to

Keyboard shortcuts

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