Documentation
¶
Overview ¶
Package config reads the service's configuration from the environment.
Environment variables are the only source: there is no flag and no remote source, and a secret may come from a file only by a variable that names it (see Secret). They are read here and nowhere else, so that every value has one origin and one validation.
Only the variables the service actually uses are here. A variable arrives with the code that consumes it, because a required variable with no reader is a deployment that fails for no reason.
Index ¶
Constants ¶
const ( VarPort = "PORT" VarLogLevel = "API_LOG_LEVEL" VarReadHeaderTimeout = "API_HTTP_READ_HEADER_TIMEOUT" VarReadTimeout = "API_HTTP_READ_TIMEOUT" VarWriteTimeout = "API_HTTP_WRITE_TIMEOUT" VarIdleTimeout = "API_HTTP_IDLE_TIMEOUT" VarShutdownTimeout = "API_SHUTDOWN_TIMEOUT" VarService = "K_SERVICE" )
The names of the variables. The service's own carry the prefix API_; PORT and K_SERVICE are the names Cloud Run sets.
const ( DefaultPort = "8080" DefaultLogLevel = "info" DefaultReadHeaderTimeout = 5 * time.Second DefaultReadTimeout = 30 * time.Second DefaultWriteTimeout = 60 * time.Second DefaultIdleTimeout = 120 * time.Second DefaultShutdownTimeout = 9 * time.Second )
Defaults for every optional variable. They are named constants rather than literals in LoadFrom so that the documentation and the code cannot drift.
const CloudRunStopGrace = 10 * time.Second
CloudRunStopGrace is how long Cloud Run waits after it asks an instance to stop before it kills it. A deployed way out has to end before then, or the close it keeps time for never runs.
const MinShutdownTimeout = time.Second
MinShutdownTimeout is the shortest way out a service may be given: the close after the drain has a quarter of it, and a quarter of less is no time to close anything in.
Variables ¶
var ErrInvalid = errors.New("config: invalid")
ErrInvalid is returned by LoadFrom; callers branch on it with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Port is the TCP port to listen on, as plain digits.
Port string
// LogLevel is the lowest level the log writes.
LogLevel slog.Level
// ReadHeaderTimeout is how long a client may take to send its headers.
ReadHeaderTimeout time.Duration
// ReadTimeout is how long a client may take to send its whole request.
ReadTimeout time.Duration
// WriteTimeout is how long the server may take to send its answer.
WriteTimeout time.Duration
// IdleTimeout is how long a connection is kept open between requests.
IdleTimeout time.Duration
// ShutdownTimeout is the whole way out once a stop is asked for: the
// server's drain and the close after it together. The platform stops the
// process ten seconds after asking it to, so the default stops a second
// short of it.
ShutdownTimeout time.Duration
// Service is the name K_SERVICE carries. Cloud Run sets it on every
// service and a developer's machine does not, which is what makes it the
// gate for what a deployment must not be given.
Service string
}
Config is the whole configuration of the process.
func LoadFrom ¶
LoadFrom reads the configuration from a set of KEY=VALUE strings, in the shape os.Environ returns them, applies the defaults and validates the result. The source is an argument — the command hands over os.Environ — so that a test can hand over an environment instead of changing the one the process runs in.
A variable set to an empty string counts as unset: an empty value is what a deployment leaves behind when it stops setting something, and it should not silently override a default with nothing.
func (*Config) CloseTimeout ¶
CloseTimeout is the part of the way out kept for closing what the service ran on: a quarter of it.
func (*Config) DrainTimeout ¶
DrainTimeout is the part of the way out the server waits for the requests still in flight: three quarters of it. The drain and the close after it share one budget in fixed shares, so that a slow request cannot eat the time the close needs.
type Secret ¶
type Secret struct {
// contains filtered or unexported fields
}
Secret is a value that must reach only the code it is for: a key, a token, a connection string. Printing it — by fmt with any verb, by the log, in an error — gives "[redacted]"; only Reveal gives the value, and the one caller that needs it calls that.