Documentation
¶
Overview ¶
Package servicehooks provides ordered, synchronous dispatch of typed service events to hooks registered during application startup.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrContextRequired = errors.New("service hook context is required") ErrCycleDetected = errors.New("service hook invocation cycle detected") ErrCyclePolicyInvalid = errors.New("service hook cycle policy is invalid") ErrHookAlreadyRegistered = errors.New("service hook is already registered") ErrHookNameRequired = errors.New("service hook name is required") ErrHookRequired = errors.New("service hook is required") ErrRegisterOptionInvalid = errors.New("service hook register option is invalid") ErrRegistrySealed = errors.New("service hook registry is sealed") )
Functions ¶
This section is empty.
Types ¶
type CycleError ¶
type CycleError struct {
HookName string
}
CycleError identifies the active registration which caused a nested invocation to be rejected.
func (CycleError) Error ¶
func (e CycleError) Error() string
func (CycleError) Unwrap ¶
func (e CycleError) Unwrap() error
type CyclePolicy ¶
type CyclePolicy int
CyclePolicy controls what happens when an invocation reaches a registration which is already active in the same context call chain.
const ( // CyclePolicyError returns a CycleError before any hook in the nested // invocation runs. This is the default because cycles should be explicit. CyclePolicyError CyclePolicy = iota // CyclePolicySkip skips only the active registration. Other hooks in the // nested invocation still run in registration order. CyclePolicySkip )
type Hook ¶
Hook handles one service event. The event type and its lifecycle semantics belong to the service registering the hook.
type InvocationError ¶
InvocationError adds registration identity to a hook error.
func (InvocationError) Error ¶
func (e InvocationError) Error() string
func (InvocationError) Unwrap ¶
func (e InvocationError) Unwrap() error
type RegisterOption ¶
type RegisterOption func(*registerConfig) error
RegisterOption configures one hook registration.
func WithCyclePolicy ¶
func WithCyclePolicy(policy CyclePolicy) RegisterOption
WithCyclePolicy sets the behavior for a recursive invocation of this registration. CyclePolicyError is used when this option is absent.
type Registry ¶
type Registry[T any] struct { // contains filtered or unexported fields }
Registry invokes typed hooks in registration order. Its zero value is ready for use. A registry is sealed explicitly with Seal or automatically by its first invocation; registrations after sealing fail with ErrRegistrySealed.
Registry must not be copied after first use.
func (*Registry[T]) Invoke ¶
Invoke seals the registry and calls hooks synchronously in registration order, stopping on cancellation or the first hook error. Before a nested invocation runs any hook, Invoke rejects an active CyclePolicyError registration; CyclePolicySkip skips only the active registration.
Hook callbacks run without holding the registry lock. Concurrent invocations are allowed, so hook implementations must provide their own synchronization.