Documentation
¶
Index ¶
- Variables
- type Command
- type DurableStateBehavior
- type Engine
- func (engine *Engine) AddProjection(ctx context.Context, name string, handler projection.Handler, ...) error
- func (engine *Engine) DurableStateEntity(ctx context.Context, behavior DurableStateBehavior) error
- func (engine *Engine) Entity(ctx context.Context, behavior EventSourcedBehavior) error
- func (engine *Engine) IsProjectionRunning(ctx context.Context, name string) (bool, error)
- func (engine *Engine) RemoveProjection(ctx context.Context, name string) error
- func (engine *Engine) SendCommand(ctx context.Context, entityID string, cmd Command, timeout time.Duration) (resultingState State, revision uint64, err error)
- func (engine *Engine) Start(ctx context.Context) error
- func (engine *Engine) Started() bool
- func (engine *Engine) Stop(ctx context.Context) error
- func (engine *Engine) Subscribe() (eventstream.Subscriber, error)
- type Event
- type EventSourcedBehavior
- type Option
- type OptionFunc
- type State
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEngineNotStarted is returned when the eGo engine has not started ErrEngineNotStarted = errors.New("eGo engine has not started") // ErrUndefinedEntityID is returned when sending a command to an undefined entity ErrUndefinedEntityID = errors.New("eGo entity id is not defined") // ErrCommandReplyUnmarshalling is returned when unmarshalling command reply failed ErrCommandReplyUnmarshalling = errors.New("failed to parse command reply") // ErrDurableStateStoreRequired is returned when the eGo engine durable store is not set ErrDurableStateStoreRequired = errors.New("durable state store is required") )
Functions ¶
This section is empty.
Types ¶
type DurableStateBehavior ¶ added in v3.2.0
type DurableStateBehavior interface { // ID defines the id that will be used in the event journal. // This helps track the entity in the events store. ID() string // InitialState returns the durable state actor initial state. // This is set as the initial state when there are no snapshots found the entity InitialState() State // HandleCommand processes every command sent to the DurableStateBehavior. One needs to use the command, the priorVersion and the priorState sent to produce a newState and newVersion. // This defines how to handle each incoming command, which validations must be applied, and finally, whether a resulting state will be persisted depending upon the response. // They encode the business rules of your durable state actor and act as a guardian of the actor consistency. // The command handler must first validate that the incoming command can be applied to the current model state. // Any decision should be solely based on the data passed in the command, the priorVersion and the priorState. // In case of successful validation and processing , the new state will be stored in the durable store depending upon response. // The actor state will be updated with the newState only if the newVersion is 1 more than the already existing state. HandleCommand(ctx context.Context, command Command, priorVersion uint64, priorState State) (newState State, newVersion uint64, err error) }
DurableStateBehavior represents a type of Actor that persists its full state after processing each command instead of using event sourcing. This type of Actor keeps its current state in memory during command handling and based upon the command response persists its full state into a durable store. The store can be a SQL or NoSQL database. The whole concept is given the current state of the actor and a command produce a new state with a higher version as shown in this diagram: (State, Command) => State DurableStateBehavior reacts to commands which result in a new version of the actor state. Only the latest version of the actor state is persisted to the durable store. There is no concept of history regarding the actor state since this is not an event sourced actor. However, one can rely on the version number of the actor state and exactly know how the actor state has evolved overtime. State actor version number are numerically incremented by the command handler which means it is imperative that the newer version of the state is greater than the current version by one.
DurableStateBehavior will attempt to recover its state whenever available from the durable state. During a normal shutdown process, it will persist its current state to the durable store prior to shutting down. This behavior help maintain some consistency across the actor state evolution.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine represents the engine that empowers the various entities
func NewEngine ¶
func NewEngine(name string, eventsStore persistence.EventsStore, opts ...Option) *Engine
NewEngine creates an instance of Engine
func (*Engine) AddProjection ¶
func (engine *Engine) AddProjection(ctx context.Context, name string, handler projection.Handler, offsetStore offsetstore.OffsetStore, opts ...projection.Option) error
AddProjection add a projection to the running eGo engine and starts it
func (*Engine) DurableStateEntity ¶ added in v3.2.0
func (engine *Engine) DurableStateEntity(ctx context.Context, behavior DurableStateBehavior) error
DurableStateEntity creates a durable state entity. A DurableStateEntity persists its full state into a durable store without any history of the state evolution. A durable state entity receives a (non-persistent) command which is first validated if it can be applied to the current state. Here validation can mean anything, from simple inspection of a command message’s fields up to a conversation with several external services, for instance. If validation succeeds, a new state is generated from the command, representing the outcome of the command. The new state is persisted and, after successful persistence, used to change the actor’s state. During a normal shutdown process, it will persist its current state to the durable store prior to shutting down. One can use the SendCommand to send a command a durable state entity.
func (*Engine) Entity ¶
func (engine *Engine) Entity(ctx context.Context, behavior EventSourcedBehavior) error
Entity creates an event sourced entity. Entity persists its full state into an events store that tracks the history based upon events that occurred. An event sourced entity receives a (non-persistent) command which is first validated if it can be applied to the current state. Here validation can mean anything, from simple inspection of a command message’s fields up to a conversation with several external services, for instance. If validation succeeds, events are generated from the command, representing the outcome of the command. These events are then persisted and, after successful persistence, used to change the actor’s state. When the event sourced entity needs to be recovered, only the persisted events are replayed of which we know that they can be successfully applied. In other words, events cannot fail when being replayed to a persistent actor, in contrast to commands. When there are no events to persist the event sourced entity will return the current state of the entity. One can use the SendCommand to send a command a durable state entity.
func (*Engine) IsProjectionRunning ¶ added in v3.1.1
IsProjectionRunning returns true when the projection is active and running One needs to check the error to see whether this function does not return a false negative
func (*Engine) RemoveProjection ¶ added in v3.1.1
RemoveProjection stops and removes a given projection from the engine
func (*Engine) SendCommand ¶
func (engine *Engine) SendCommand(ctx context.Context, entityID string, cmd Command, timeout time.Duration) (resultingState State, revision uint64, err error)
SendCommand sends command to a given entity ref. This will return: 1. the resulting state after the command has been handled and the emitted event/durable state persisted 2. nil when there is no resulting state or no event persisted 3. an error in case of error
func (*Engine) Subscribe ¶
func (engine *Engine) Subscribe() (eventstream.Subscriber, error)
Subscribe creates an events subscriber
type EventSourcedBehavior ¶ added in v3.2.0
type EventSourcedBehavior interface { // ID defines the id that will be used in the event journal. // This helps track the entity in the events store. ID() string // InitialState returns the event sourced actor initial state. // This is set as the initial state when there are no snapshots found the entity InitialState() State // HandleCommand helps handle commands received by the event sourced actor. The command handlers define how to handle each incoming command, // which validations must be applied, and finally, which events will be persisted if any. When there is no event to be persisted a nil can // be returned as a no-op. Command handlers are the meat of the event sourced actor. // They encode the business rules of your event sourced actor and act as a guardian of the event sourced actor consistency. // The command must first validate that the incoming command can be applied to the current model state. // Any decision should be solely based on the data passed in the commands and the state of the Behavior. // In case of successful validation, one or more events expressing the mutations are persisted. // Once the events are persisted, they are applied to the state producing a new valid state. // Every event emitted are processed one after the other in the same order they were emitted to guarantee consistency. // It is at the discretion of the application developer to know in which order a given command should return the list of events // This is really powerful when a command needs to return two events. For instance, an OpenAccount command can result in two events: one is AccountOpened and the second is AccountCredited HandleCommand(ctx context.Context, command Command, priorState State) (events []Event, err error) // HandleEvent handle events emitted by the command handlers. The event handlers are used to mutate the state of the event sourced actor by applying the events to it. // Event handlers must be pure functions as they will be used when instantiating the event sourced actor and replaying the event journal. HandleEvent(ctx context.Context, event Event, priorState State) (state State, err error) }
EventSourcedBehavior defines an event sourced behavior when modeling a CQRS EventSourcedBehavior.
type Option ¶
type Option interface { // Apply sets the Option value of a config. Apply(e *Engine) }
Option is the interface that applies a configuration option.
func WithCluster ¶
func WithCluster(provider discovery.Provider, partitionCount uint64, minimumPeersQuorum uint16, host string, remotingPort, gossipPort, peersPort int) Option
WithCluster enables cluster mode
func WithStateStore ¶ added in v3.2.0
func WithStateStore(stateStore persistence.StateStore) Option
WithStateStore sets the durable store. This is necessary when creating a durable state entity