runner

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunCommand

func RunCommand[T any](ctx context.Context, h *Handler, c command.Commander[T], msg T) error

func RunQuery

func RunQuery[T any, R any](ctx context.Context, h *Handler, q command.Querier[T, R], msg T) (R, error)

Types

type DeadlineGetter added in v0.1.0

type DeadlineGetter interface {
	GetDeadline() time.Time
}

type DoneHandlerGetter added in v0.1.0

type DoneHandlerGetter interface {
	GetDoneHandler() func(*Handler)
}

type ErrorHandlerGetter added in v0.1.0

type ErrorHandlerGetter interface {
	GetErrorHandler() func(error)
}

type ExitOnErrorGetter added in v0.3.0

type ExitOnErrorGetter interface {
	GetExitOnError() bool
}

type ExponentialBackoffStrategy

type ExponentialBackoffStrategy struct {
	// Base is the starting delay (e.g., 100ms)
	Base time.Duration
	// Factor is multiplied each iteration (e.g., 2 => 100ms, 200ms, 400ms, ...)
	Factor float64
	// Max is the maximum delay allowed (caps the exponential growth)
	Max time.Duration
}

ExponentialBackoffStrategy implements a backoff strategy. Usage example:

WithRetryStrategy(ExponentialBackoffStrategy{
    Base:   100 * time.Millisecond,
    Factor: 2,
    Max:    5 * time.Second,
})

func (ExponentialBackoffStrategy) SleepDuration

func (e ExponentialBackoffStrategy) SleepDuration(attempt int, _ error) time.Duration

SleepDuration implements an exponential backoff with a cap at Max.

type Handler

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

Handler wraps configuration options so that we can later call a function with known behavior in terms of handling errors, rtries, timeouts, etc We do so by providing a Run method:

Run(ctx context.Context, fn func(context.Context) error)

This ensures consistent execution behavior

func NewHandler

func NewHandler(opts ...Option) *Handler

NewHandler constructs a Runner from various options, applying defaults if unset.

func (*Handler) Run

func (h *Handler) Run(ctx context.Context, fn func(context.Context) error) error

func (*Handler) ShouldStopOnErr added in v0.3.0

func (h *Handler) ShouldStopOnErr() bool

ShouldStopOnError returns whether execution should stop on first error

type Logger

type Logger interface {
	Info(msg string, args ...any)
	Error(msg string, args ...any)
}

Logger interface shared across packages

type LoggerGetter added in v0.1.0

type LoggerGetter interface {
	GetLogger() Logger
}

type MaxRetriesGetter added in v0.1.0

type MaxRetriesGetter interface {
	GetMaxRetries() int
}

type MaxRunsGetter added in v0.1.0

type MaxRunsGetter interface {
	GetMaxRuns() int
}

type Middleware added in v0.3.0

type Middleware func(next func(context.Context) error) func(context.Context) error

Middleware defines a function that wraps/enhances the behavior of a handler function. It allows you to intercept and modify the execution of the function passed to Handler.Run (business logic) without changing its implementation. Each middleware receives the next function in the chain and returns a new function that can perform additional actions before or after calling the next function. This pattern enables composable concerns such as logging, telemetry, authentication, retry logic, in a clean and decoupled manner, similar to middleware in HTTP routers or RPC frameworks.

type NoDelayStrategy

type NoDelayStrategy struct{}

NoDelayStrategy is a simple retry strategy that performs all retries immediately without waiting.

func (NoDelayStrategy) SleepDuration

func (n NoDelayStrategy) SleepDuration(_ int, _ error) time.Duration

SleepDuration always returns zero, causing immediate retries.

type Option

type Option func(*Handler)

func WithConfigurator added in v0.1.0

func WithConfigurator(i interface{}) Option

WithConfigurator sets multiple options from a single configuration struct that implements one or more Getter interfaces

func WithDeadline

func WithDeadline(d time.Time) Option

func WithDeadlineSetter added in v0.1.0

func WithDeadlineSetter(s DeadlineGetter) Option

func WithDoneHandler

func WithDoneHandler(d func(*Handler)) Option

func WithDoneHandlerSetter added in v0.1.0

func WithDoneHandlerSetter(s DoneHandlerGetter) Option

func WithErrorHandler

func WithErrorHandler(h func(error)) Option

func WithErrorHandlerSetter added in v0.1.0

func WithErrorHandlerSetter(s ErrorHandlerGetter) Option

func WithExitOnError added in v0.3.0

func WithExitOnError(exit bool) Option

WithExitOnError configures a runner to stop on first error

func WithExitOnErrorSetter added in v0.3.0

func WithExitOnErrorSetter(s ExitOnErrorGetter) Option

func WithLogger

func WithLogger(l Logger) Option

func WithLoggerSetter added in v0.1.0

func WithLoggerSetter(s LoggerGetter) Option

func WithMaxRetries

func WithMaxRetries(max int) Option

func WithMaxRetriesSetter added in v0.1.0

func WithMaxRetriesSetter(s MaxRetriesGetter) Option

func WithMaxRuns

func WithMaxRuns(max int) Option

func WithMaxRunsSetter added in v0.1.0

func WithMaxRunsSetter(s MaxRunsGetter) Option

func WithMiddleware added in v0.3.0

func WithMiddleware(mw ...Middleware) Option

func WithNoTimeout added in v0.3.0

func WithNoTimeout() Option

func WithRetryStrategy

func WithRetryStrategy(s RetryStrategy) Option

WithRetryStrategy lets you define a custom retry/backoff approach

func WithRetryStrategySetter added in v0.1.0

func WithRetryStrategySetter(s RetryStrategyGetter) Option

func WithRunOnce

func WithRunOnce(once bool) Option

func WithRunOnceSetter added in v0.1.0

func WithRunOnceSetter(s RunOnceGetter) Option

func WithTimeout

func WithTimeout(t time.Duration) Option

func WithTimeoutSetter added in v0.1.0

func WithTimeoutSetter(s TimeoutGetter) Option

type RetryStrategy

type RetryStrategy interface {
	// SleepDuration returns how long to wait before the next retry attempt.
	// The attempt index starts at 0, incrementing after each failure.
	SleepDuration(attempt int, err error) time.Duration
}

RetryStrategy encapsulates the decision and delay between retries.

type RetryStrategyGetter added in v0.1.0

type RetryStrategyGetter interface {
	GetRetryStrategy() RetryStrategy
}

type RunOnceGetter added in v0.1.0

type RunOnceGetter interface {
	GetRunOnce() bool
}

type TimeoutGetter added in v0.1.0

type TimeoutGetter interface {
	GetTimeout() time.Duration
}

Jump to

Keyboard shortcuts

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