Skip to content

Stop the hosted application's engine while HTTP is idle - #259

Merged
jmalloc merged 29 commits into
mainfrom
hosted-idle
Nov 17, 2026
Merged

jmalloc merged 29 commits into
mainfrom
hosted-idle

Conversation

@jmalloc

@jmalloc jmalloc commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Keep main.go focused on running the bank's Dogma application and serving its web UI. It constructs the application, engine, and UI handler normally, then adds the public demo's idle shutdown through one optional idletimeout.Inject() call. There is one errgroup and one HTTP server.

When DOGMA_EXAMPLE_IDLE_TIMEOUT is set to a positive duration such as 1m, Inject() replaces the engine runner, UI command executor, and outer HTTP handler before they start. The lifetime manager uses the already-created engine first, then calls the shared newEngine() factory after subsequent idle shutdowns. Commands and their options are forwarded to the current engine. Concurrent visitors share one engine, and a replacement waits for the previous engine to stop.

The projection and engine database pools remain fixed for the process lifetime. Inject() configures their unused connections to expire naturally. With the variable unset, the UI submits commands directly to the engine and the engine runs continuously.

The HTTP server, request deadlines, connection timeouts, logging, signal handling, and graceful shutdown remain visible in main.go. DOGMA_EXAMPLE_LISTEN_ADDRESS defaults to :8080. The Dockerfile and command are unchanged.

The public demo uses a JSON logger on stdout so its log collector can parse levels and attributes. Local runs use text logs on stdout. main() selects the format from the existing DOGMA_EXAMPLE_IDLE_TIMEOUT setting before creating the one shared logger for the engine, HTTP server, and idle manager.

run() owns signal handling and all deferred cleanup. It treats cancellation as a successful shutdown on every return path, including during startup. main() creates the logger and reports failures after run() returns; it has no deferred cleanup.

HTTP health checks and other probes count as activity. Scheduled work waits until the engine wakes. Platform sleeping must be enabled separately.

Validation:

  • go test ./..., go test -race ./internal/idletimeout, go vet ./..., and go fix -diff ./... passed.
  • Injection tests cover using the initial engine, stopping it while idle, waking a replacement for a request that reads projections, dispatching commands to that replacement, cancellation, and invalid timeout configuration.
  • Built the linux/amd64 scratch image and exercised it with testcontainers PostgreSQL 18. Verified idle connection cleanup, concurrent wake-up, persistent accounts, overdue transfers, port 9090 in idle mode, port 8080 in normal mode, command processing, and clean SIGTERM in both modes.
  • Logging checks verified text on stdout when the idle setting is unset or empty, and JSON on stdout when it is set.
  • Process-level checks verified successful SIGINT and SIGTERM during database initialization, while an actual startup failure logs an error and exits with status 1.
  • An earlier hosted test of commit 1ac8274 separately verified the platform's actual sleep/wake behavior and a fresh process after waking.

@jmalloc
jmalloc merged commit ebd9869 into main Nov 17, 2026
11 checks passed
@jmalloc
jmalloc deleted the hosted-idle branch November 17, 2026 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant