handler

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRunning = errors.New("event handler is already running")

ErrRunning is an error that indicates an event handler is already running. It is returned when attempting to run a handler that is currently active. This error serves as a guard against concurrent execution of the same event handler.

Functions

func DefaultStartupQuery added in v0.4.3

func DefaultStartupQuery(events []string) query.Query

DefaultStartupQuery constructs a default query for the startup of an event handler. It uses the provided slice of event names to create a query that sorts events by their timestamps. This function is typically used to determine which events should be processed during the startup of an event handler.

Types

type Handler

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

Handler is a type that processes events from an event bus. It associates event names with specific functions, which are called whenever their respective event occurs. Handler uses multiple workers to process events concurrently. The number of workers can be customized through options when creating a new Handler instance. Events can be registered to the Handler, and it provides methods to check if it's currently processing events or if a certain event has a registered handler. Handlers also have a context which can be used for synchronization and cancellation of operations. Handlers prevent concurrent execution of the same instance to avoid race conditions.

func New

func New(bus event.Bus, opts ...Option) *Handler

New creates a new event handler with the provided bus and options. It sets up an empty map for handlers and event names, applies the given options, and ensures that there is at least one worker. The new handler is returned.

func (*Handler) Context

func (h *Handler) Context() context.Context

Context returns the context of the Handler. This context is used for synchronization and cancellation of operations within the Handler. If the Handler is not running, nil is returned.

func (*Handler) EventHandler added in v0.2.12

func (h *Handler) EventHandler(name string) (func(event.Event), bool)

EventHandler retrieves the event handler function associated with the provided event name. If a handler for the given event name is found, it returns the handler function and true. If no handler is found, it returns nil and false. This method is safe for concurrent use.

func (*Handler) RegisterEventHandler

func (h *Handler) RegisterEventHandler(name string, fn func(event.Event))

RegisterEventHandler associates the provided event name with a given function to handle that event. The function will be called whenever an event with the associated name occurs. This method is safe for concurrent use.

func (*Handler) Run

func (h *Handler) Run(ctx context.Context) (<-chan error, error)

Run starts the event handling process for the Handler. It subscribes to the events that have been registered with the Handler and starts processing them concurrently with a number of worker goroutines. The errors from running the handlers are returned through a channel. If the Handler is already running when this method is called, it returns an error. The context passed to Run is used to control cancellation of the event handling process.

func (*Handler) Running

func (h *Handler) Running() bool

Running checks if the Handler is currently processing events. It returns true if the Handler is running and false otherwise. This method is safe for concurrent use.

type Option

type Option func(*Handler)

Option is a function type used to configure a Handler. It can be used to set various properties of the Handler such as the event store or the number of workers. Each option is applied in the order they are provided when constructing a new Handler using the New function.

func Startup added in v0.4.3

func Startup(store event.Store, opts ...query.Option) Option

Startup configures a Handler with a specified event store and options for querying events. It is used to setup the event store that the Handler will use to fetch events during startup. This can be used to initialize the system with initial event handling on startup or implement a "catch-up" mechanism for their event handlers. The query options allow customization of how the events are fetched from the store. The returned Option can be used when creating a new Handler.

If [query.Option]s are provided, they will be merged with the default query using query.Merge. If you want to _replace_ the default query, use the StartupQuery option instead of providing [query.Option]s to Startup.

func StartupQuery added in v0.4.3

func StartupQuery(fn func(event.Query) event.Query) Option

StartupQuery is a function that configures a Handler's startup query. It accepts a function that takes and returns an event.Query as its argument. The provided function will be used by the Handler to modify the default query used when fetching events from the event store during startup. The resulting Option can be used when constructing a new Handler, allowing customization of the startup behavior of the Handler.

func WithStore deprecated

func WithStore(store event.Store) Option

WithStore is an Option for a Handler that sets the event store to be used. This function returns an Option that, when used with the New function, configures a Handler to use the specified event store. This is typically used to specify where events should be stored when they are handled by the Handler. Note that WithStore is equivalent to the Startup function.

Deprecated: Use Startup instead.

func Workers added in v0.4.3

func Workers(n int) Option

Workers sets the number of workers that a Handler uses to process events. If this option is not used when constructing a new Handler, the default number of workers is 1.

Jump to

Keyboard shortcuts

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