Documentation ¶
Index ¶
- Variables
- func Handle(ctx context.Context, bus Bus, name string, handler func(Context) error) (<-chan error, error)
- func MustHandle(ctx context.Context, bus Bus, name string, handler func(Context) error) <-chan error
- type Bus
- type Cmd
- type Command
- type Context
- type Data
- type DispatchConfig
- type DispatchOption
- type Handler
- type Option
- type Reporter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAlreadyFinished is returned when a Command is finished multiple times. ErrAlreadyFinished = errors.New("command already finished") )
Functions ¶
Types ¶
type Bus ¶
type Bus interface { // Dispatch sends the Command to the appropriate subscriber. Dispatch must // only return nil if the Command has been successfully received by a // subscriber. Dispatch(context.Context, Command, ...DispatchOption) error // Subscribe subscribes to Commands with the given names and returns a // channel of Contexts. Implementations of Bus must ensure that Commands // aren't received by multiple subscribers. Subscribe(ctx context.Context, names ...string) (<-chan Context, <-chan error, error) }
A Bus dispatches Commands to appropriate handlers.
type Cmd ¶
type Cmd struct {
Data Data
}
Cmd is the implementation of Command.
type Command ¶
type Command interface { // ID returns the Command ID. ID() uuid.UUID // Name returns the Command name. Name() string // Payload returns the Command Payload. Payload() interface{} // Aggregate returns the attached aggregate data. Aggregate() (id uuid.UUID, name string) }
A Command represents a command in the business model of an application or service. Commands can be dispatched through a Bus to handlers of such Commands.
type Context ¶
type Context interface { context.Context Command // AggregateID returns the UUID of the attached aggregate, or uuid.Nil. AggregateID() uuid.UUID // AggregateName returns the name of the attached aggregate, or an empty string. AggregateName() string // Finish should be called after the Command has been handled so that the // Bus that dispatched the Command can be notified about the execution // result. Finish(context.Context, ...finish.Option) error }
Context is the context for handling Commands.
type Data ¶
type Data struct { ID uuid.UUID Name string Payload interface{} AggregateName string AggregateID uuid.UUID }
Data contains the actual fields of Cmd.
type DispatchConfig ¶
type DispatchConfig struct { // A synchronous dispatch waits for the execution of the Command to finish // and returns the execution error if there was any. // // A dispatch is automatically made synchronous when Repoter is non-nil. Synchronous bool // If Reporter is not nil, the Bus will report the execution result of a // Command to Reporter by calling Reporter.Report(). // // A non-nil Reporter makes the dispatch synchronous. Reporter Reporter }
Config is the configuration for dispatching a Command.
type DispatchOption ¶
type DispatchOption func(*DispatchConfig)
DispatchOption is an option for dispatching Commands.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler can be used to subscribe to and handle Commands.
func NewHandler ¶
NewHandler returns a Handler for Commands that uses the provided Bus to subscribe to Commands.
func (*Handler) Handle ¶
func (h *Handler) Handle(ctx context.Context, name string, handler func(Context) error) (<-chan error, error)
Handle registers the function handler as a handler for the given Command name. Handle subscribes to the underlying Bus for Commands with that name. When Handler is selected as the handler for a dispatched Command, handler is called with a Command Context which contains the Command and its Payload.
If Handle fails to subscribe to the Command, a nil channel and the error from the Bus is returned. Otherwise a channel of asynchronous Command errors and a nil error are returned.
When handler returns a non-nil error, that error is pushed into the returned error channel. Asynchronous errors from the underlying Command Bus are pushed into the error channel as well. Callers must receive from the returned error channel to prevent the handler from blocking indefinitely.
When ctx is canceled, the returned error channel is closed.
Directories ¶
Path | Synopsis |
---|---|
Package cmdbus provides a distributed & event-driven Command Bus.
|
Package cmdbus provides a distributed & event-driven Command Bus. |
Package mock_command is a generated GoMock package.
|
Package mock_command is a generated GoMock package. |