Skip to content

Commit

Permalink
config: tidy up config units
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall committed May 6, 2024
1 parent 4dba324 commit 005cda6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
30 changes: 15 additions & 15 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

type ServerConfig struct {
// URL is the server URL.
URL string `json:"url" yaml:"url"`
HeartbeatIntervalSeconds int `json:"heartbeat_interval_seconds" yaml:"heartbeat_interval_seconds"`
HeartbeatTimeoutSeconds int `json:"heartbeat_timeout_seconds" yaml:"heartbeat_timeout_seconds"`
URL string `json:"url" yaml:"url"`
HeartbeatInterval int `json:"heartbeat_interval" yaml:"heartbeat_interval"`
HeartbeatTimeout int `json:"heartbeat_timeout" yaml:"heartbeat_timeout"`
}

func (c *ServerConfig) Validate() error {
Expand All @@ -23,10 +23,10 @@ func (c *ServerConfig) Validate() error {
if _, err := url.Parse(c.URL); err != nil {
return fmt.Errorf("invalid url: %w", err)
}
if c.HeartbeatIntervalSeconds == 0 {
if c.HeartbeatInterval == 0 {
return fmt.Errorf("missing heartbeat interval")
}
if c.HeartbeatTimeoutSeconds == 0 {
if c.HeartbeatTimeout == 0 {
return fmt.Errorf("missing heartbeat timeout")
}
return nil
Expand All @@ -35,7 +35,7 @@ func (c *ServerConfig) Validate() error {
// ForwarderConfig contains the configuration for how to forward requests
// from Pico.
type ForwarderConfig struct {
TimeoutSeconds int `json:"timeout_seconds" yaml:"timeout_seconds"`
Timeout int `json:"timeout" yaml:"timeout"`
}

type AdminConfig struct {
Expand Down Expand Up @@ -119,30 +119,30 @@ Note Pico connects to the server with WebSockets, so will replace http/https
with ws/wss (you can configure either).`,
)
fs.IntVar(
&c.Server.HeartbeatIntervalSeconds,
"server.heartbeat-interval-seconds",
&c.Server.HeartbeatInterval,
"server.heartbeat-interval",
10,
`
Heartbeat interval in seconds.
To verify the connection to the server is ok, the listener sends a
heartbeat to the upstream at the '--server.heartbeat-interval-seconds'
interval, with a timeout of '--server.heartbeat-timeout-seconds'.`,
heartbeat to the upstream at the '--server.heartbeat-interval'
interval, with a timeout of '--server.heartbeat-timeout'.`,
)
fs.IntVar(
&c.Server.HeartbeatTimeoutSeconds,
"server.heartbeat-timeout-seconds",
&c.Server.HeartbeatTimeout,
"server.heartbeat-timeout",
10,
`
Heartbeat timeout in seconds.,
To verify the connection to the server is ok, the listener sends a
heartbeat to the upstream at the '--server.heartbeat-interval-seconds'
interval, with a timeout of '--server.heartbeat-timeout-seconds'.`,
heartbeat to the upstream at the '--server.heartbeat-interval'
interval, with a timeout of '--server.heartbeat-timeout'.`,
)

fs.IntVar(
&c.Forwarder.TimeoutSeconds,
&c.Forwarder.Timeout,
"forwarder.timeout",
10,
`
Expand Down
6 changes: 3 additions & 3 deletions agent/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newEndpoint(
forwarder: newForwarder(
endpointID,
forwardAddr,
time.Duration(conf.Forwarder.TimeoutSeconds)*time.Second,
time.Duration(conf.Forwarder.Timeout)*time.Second,
metrics,
logger,
),
Expand Down Expand Up @@ -74,8 +74,8 @@ func (e *endpoint) Run(ctx context.Context) error {

if err := stream.Monitor(
ctx,
time.Duration(e.conf.Server.HeartbeatIntervalSeconds)*time.Second,
time.Duration(e.conf.Server.HeartbeatTimeoutSeconds)*time.Second,
time.Duration(e.conf.Server.HeartbeatInterval)*time.Second,
time.Duration(e.conf.Server.HeartbeatTimeout)*time.Second,
); err != nil {
if ctx.Err() != nil {
// Shutdown.
Expand Down
15 changes: 8 additions & 7 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ proxy:
# The timeout when sending proxied requests to upstream listeners for forwarding
# to other nodes in the cluster.
#
# If the upstream does not respond within the given timeout a
# '504 Gateway Timeout' is returned to the client.
gateway_timeout: 15
Expand All @@ -51,7 +52,7 @@ upstream:
# The host/port to listen for connections from upstream listeners.
#
# If the host is unspecified it defaults to all listeners, such as
# '--proxy.bind-addr :8001' will listen on '0.0.0.0:8001'.
# '--upstream.bind-addr :8001' will listen on '0.0.0.0:8001'.
bind_addr: :8001
# Upstream listen address to advertise to other nodes in the cluster.
Expand Down Expand Up @@ -190,15 +191,15 @@ server:
# Heartbeat interval in seconds.
#
# To verify the connection to the server is ok, the listener sends a
# heartbeat to the upstream at the '--server.heartbeat-interval-seconds'
# interval, with a timeout of '--server.heartbeat-timeout-seconds'.`,
heartbeat_interval_seconds: 10
# heartbeat to the upstream at the '--server.heartbeat-interval'
# interval, with a timeout of '--server.heartbeat-timeout'.`,
heartbeat_interval: 10
# Heartbeat timeout in seconds.,
#
# To verify the connection to the server is ok, the listener sends a
# heartbeat to the upstream at the '--server.heartbeat-interval-seconds'
heartbeat_timeout_seconds: 10
# heartbeat to the upstream at the '--server.heartbeat-interval'
heartbeat_timeout: 10
forwarder:
# Forwarder timeout in seconds.
Expand All @@ -208,7 +209,7 @@ forwarder:
#
# If the upstream does not respond within the given timeout a
# '504 Gateway Timeout' is returned to the client.
timeout_seconds: 10
timeout: 10
admin:
# The host/port to listen for incoming admin connections.
Expand Down
13 changes: 12 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ type ClusterConfig struct {
Join []string `json:"join" yaml:"join"`
}

func (c *ClusterConfig) Validate() error {
if c.NodeID != "" && c.NodeIDPrefix != "" {
return fmt.Errorf("cannot specify both node ID and node ID prefix")
}
return nil
}

type ServerConfig struct {
// GracefulShutdownTimeout is the timeout to allow for graceful shutdown
// of the server in seconds.
Expand Down Expand Up @@ -108,6 +115,9 @@ func (c *Config) Validate() error {
if err := c.Gossip.Validate(); err != nil {
return fmt.Errorf("gossip: %w", err)
}
if err := c.Cluster.Validate(); err != nil {
return fmt.Errorf("cluster: %w", err)
}
if err := c.Server.Validate(); err != nil {
return fmt.Errorf("server: %w", err)
}
Expand Down Expand Up @@ -151,6 +161,7 @@ advertise address of '10.26.104.14:8000'.`,
`
The timeout when sending proxied requests to upstream listeners for forwarding
to other nodes in the cluster.
If the upstream does not respond within the given timeout a
'504 Gateway Timeout' is returned to the client.`,
)
Expand All @@ -163,7 +174,7 @@ If the upstream does not respond within the given timeout a
The host/port to listen for connections from upstream listeners.
If the host is unspecified it defaults to all listeners, such as
'--proxy.bind-addr :8001' will listen on '0.0.0.0:8001'`,
'--upstream.bind-addr :8001' will listen on '0.0.0.0:8001'`,
)
fs.StringVar(
&c.Upstream.AdvertiseAddr,
Expand Down
6 changes: 3 additions & 3 deletions tests/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ func defaultAgentConfig(serverAddr string) *agentconfig.Config {
return &agentconfig.Config{
Server: agentconfig.ServerConfig{
URL: "http://" + serverAddr,
HeartbeatIntervalSeconds: 1,
HeartbeatTimeoutSeconds: 1,
HeartbeatInterval: 1,
HeartbeatTimeout: 1,
},
Forwarder: agentconfig.ForwarderConfig{
TimeoutSeconds: 1,
Timeout: 1,
},
Admin: agentconfig.AdminConfig{
BindAddr: "127.0.0.1:0",
Expand Down

0 comments on commit 005cda6

Please sign in to comment.