initializer

command
v14.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Service lifecycle: iris.Initializer at startup, closers at shutdown.

A service passed to Services gets a full lifecycle. Its constructor runs at registration; Build() then calls Init on every singleton that implements iris.Initializer, in registration order, and a non-nil error stops the build. Because *UserCache is passed as a value and has a Close() error method, Services registers it as a dependency AND as a shutdown closer - no extra call needed. Deferrables adds cleanup that owns no such method.

Both run when app.Shutdown is called, and Ctrl+C alone does not call it: the interrupt handler stops the HTTP server and exits. Hand app.Shutdown to iris.RegisterOnInterrupt, as main does below, and the signal runs the closers too.

Learn: warm a service with iris.Initializer during Build, and run closers and Deferrables when app.Shutdown is called. Before: ../request-scoped

Run:

go run main.go

Try:

curl http://localhost:8080/users          # 200, served from the warmed cache
curl http://localhost:8080/users/status   # 200, cached count and warmed_at

Press Ctrl+C: the log shows the cache Close and the deferrable running.

Next: ../../08-errors/central-error-map

Jump to

Keyboard shortcuts

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