-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the configuration so its easier to add new settings, clearer what the default values are and which environment variables impact the running of container. This includes compatibility with slog for future use. Bump version of golangci-lint so to prevent invalid failures
- Loading branch information
Showing
7 changed files
with
137 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/caarlos0/env/v11" | ||
) | ||
|
||
// config represents the configuration for the reaper. | ||
type config struct { | ||
// ConnectionTimeout is the duration without receiving any connections which will trigger a shutdown. | ||
ConnectionTimeout time.Duration `env:"RYUK_CONNECTION_TIMEOUT" envDefault:"60s"` | ||
|
||
// ReconnectionTimeout is the duration after the last connection closes which will trigger | ||
// resource clean up and shutdown. | ||
ReconnectionTimeout time.Duration `env:"RYUK_RECONNECTION_TIMEOUT" envDefault:"10s"` | ||
|
||
// ShutdownTimeout is the maximum amount of time the reaper will wait | ||
// for once signalled to shutdown before it terminates even if connections | ||
// are still established. | ||
ShutdownTimeout time.Duration `env:"RYUK_SHUTDOWN_TIMEOUT" envDefault:"10m"` | ||
|
||
// Port is the port to listen on for connections. | ||
Port uint16 `env:"RYUK_PORT" envDefault:"8080"` | ||
|
||
// Verbose is whether to enable verbose aka debug logging. | ||
Verbose bool `env:"RYUK_VERBOSE" envDefault:"false"` | ||
} | ||
|
||
// LogAttrs returns the configuration as a slice of attributes. | ||
func (c config) LogAttrs() []slog.Attr { | ||
return []slog.Attr{ | ||
slog.Duration("connection_timeout", c.ConnectionTimeout), | ||
slog.Duration("reconnection_timeout", c.ReconnectionTimeout), | ||
slog.Duration("shutdown_timeout", c.ShutdownTimeout), | ||
slog.Int("port", int(c.Port)), | ||
slog.Bool("verbose", c.Verbose), | ||
} | ||
} | ||
|
||
// loadConfig loads the configuration from the environment | ||
// applying defaults where necessary. | ||
func loadConfig() (*config, error) { | ||
var cfg config | ||
if err := env.Parse(&cfg); err != nil { | ||
return nil, fmt.Errorf("parse env: %w", err) | ||
} | ||
|
||
return &cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// clearConfigEnv clears the environment variables for the config fields. | ||
func clearConfigEnv(t *testing.T) { | ||
t.Helper() | ||
|
||
var cfg config | ||
typ := reflect.TypeOf(cfg) | ||
for i := range typ.NumField() { | ||
field := typ.Field(i) | ||
if name := field.Tag.Get("env"); name != "" { | ||
if os.Getenv(name) != "" { | ||
t.Setenv(name, "") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Test_loadConfig(t *testing.T) { | ||
clearConfigEnv(t) | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
expected := config{ | ||
Port: 8080, | ||
ConnectionTimeout: time.Minute, | ||
ReconnectionTimeout: time.Second * 10, | ||
ShutdownTimeout: time.Minute * 10, | ||
} | ||
|
||
cfg, err := loadConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expected, *cfg) | ||
}) | ||
|
||
t.Run("custom", func(t *testing.T) { | ||
t.Setenv("RYUK_PORT", "1234") | ||
t.Setenv("RYUK_CONNECTION_TIMEOUT", "2s") | ||
t.Setenv("RYUK_RECONNECTION_TIMEOUT", "3s") | ||
t.Setenv("RYUK_SHUTDOWN_TIMEOUT", "7s") | ||
t.Setenv("RYUK_VERBOSE", "true") | ||
|
||
expected := config{ | ||
Port: 1234, | ||
ConnectionTimeout: time.Second * 2, | ||
ReconnectionTimeout: time.Second * 3, | ||
ShutdownTimeout: time.Second * 7, | ||
Verbose: true, | ||
} | ||
|
||
cfg, err := loadConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expected, *cfg) | ||
}) | ||
|
||
for _, name := range []string{ | ||
"RYUK_PORT", | ||
"RYUK_CONNECTION_TIMEOUT", | ||
"RYUK_RECONNECTION_TIMEOUT", | ||
"RYUK_SHUTDOWN_TIMEOUT", | ||
"RYUK_VERBOSE", | ||
} { | ||
t.Run("invalid-"+name, func(t *testing.T) { | ||
t.Setenv(name, "invalid") | ||
_, err := loadConfig() | ||
require.Error(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters