Documentation
¶
Overview ¶
Package idletimeout manages engine lifetime for the public demo hosted at https://example.dogmatiq.io. It is hosting infrastructure, separate from the illustrative application and HTTP server setup in cmd/bank.
Inject adds idle shutdown to an otherwise ordinary application setup. It replaces the engine runner and command executor, wraps the HTTP handler to track request activity, and configures database pools to expire idle connections. The engine stops after an idle period, and the next request starts a fresh engine after shutdown completes.
Database pools and the UI handler remain owned by the caller. Expiring idle connections prevents their TCP traffic from keeping the platform awake. Platform sleeping must be enabled separately. Scheduled work waits until the engine wakes; HTTP health checks and other probes also keep it awake.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Inject ¶
func Inject( ctx context.Context, idle string, runEngine *func() error, executor *dogma.CommandExecutor, handler *http.Handler, engine Engine, newEngine func() Engine, logger *slog.Logger, databases ...*sql.DB, ) error
Inject adds idle shutdown to the public demo's engine and HTTP handler. It replaces runEngine, executor, and handler before the caller starts them. The replacement runner manages engine until ctx is canceled or an engine fails, using newEngine to create a fresh engine after each idle shutdown.
idle must be a positive duration. Unused database connections expire after that duration; the caller retains ownership of the pools and UI handler. If idle is invalid, Inject returns an error without changing the arguments.
Types ¶
type Engine ¶
type Engine interface {
dogma.CommandExecutor
Run(context.Context) error
}
Engine is the part of a Dogma engine used by the lifetime manager. Command execution must support waiting for initialization while Engine.Run starts.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager runs one engine at a time and forwards commands to the current engine. Request activity keeps it awake; after an idle shutdown, activity starts a new engine. Database connection pools belong to the caller and remain unchanged.
func New ¶
func New(ctx context.Context, logger *slog.Logger, idleTimeout time.Duration, engine Engine, newEngine func() Engine) *Manager
New returns a manager whose Manager.Run method must be started by the caller. engine is used for the first start. newEngine must return a fresh engine without starting it for each subsequent wake-up.
func (*Manager) ExecuteCommand ¶
func (m *Manager) ExecuteCommand(ctx context.Context, command dogma.Command, options ...dogma.ExecuteCommandOption) error
ExecuteCommand submits a command to the current engine, waking it if needed. All command options, including event observers, are passed through unchanged.