Runtime
lakta.NewRuntime() is the entry point for every Lakta service. It wires modules together and manages their full lifecycle.
Creating a runtime
Section titled “Creating a runtime”if err := lakta.NewRuntime(module1, module2, module3, ...).Run(); err != nil { os.Exit(1)}Run installs signal handlers for SIGTERM/SIGINT, then delegates to RunContext. Pass your own context to RunContext directly if you need custom cancellation.
What the runtime does
Section titled “What the runtime does”- Dependency sort — topologically sorts all modules from their
Provider/Dependentdeclarations. Returns an error immediately if a required dependency has no provider or a cycle is detected. - Init — calls
LoadConfigthenIniton each module sequentially in resolved order. - Logger injection — retrieves
*slog.Loggerfrom DI and injects it into context. Wires hot-reload callbacks forHotReloadablemodules. - StartAsync — all
AsyncModuleimplementations start concurrently and must return quickly. - Start — all
SyncModuleimplementations start concurrently and block until shutdown. - Shutdown — on signal or any start error, calls
Shutdownon all initialized modules in reverse init order with a 30-second deadline.
Module ordering
Section titled “Module ordering”You do not need to pass modules in dependency order. The runtime resolves order automatically from Provider and Dependent declarations:
// These can be in any order — the runtime sorts themlakta.NewRuntime( myapp.NewModule(), // declares dependency on *pgxpool.Pool, *slog.Logger pgx.NewModule(...), // declares it provides *pgxpool.Pool slog.NewModule(), // declares it provides *slog.Logger config.NewModule(...), // declares it provides *koanf.Koanf tint.NewModule(), // no deps declared — sorted by original position)Modules that implement neither Provider nor Dependent are queued in their original relative order.
Error handling
Section titled “Error handling”| Situation | Behaviour |
|---|---|
| Required dep has no provider | Error before any Init fires |
| Dependency cycle detected | Error before any Init fires |
Init returns error | Remaining modules not started; already-initialized modules shut down in reverse order |
StartAsync returns error | Shutdown triggered for all initialized modules |
Start returns error | Shutdown triggered for all initialized modules |
Shutdown returns error | Logged; shutdown continues; first error returned to caller |
Signal handling
Section titled “Signal handling”Run handles SIGTERM and SIGINT. A second signal forces immediate exit without waiting for graceful shutdown.