Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions config/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "time"

type (
Status uint
Policy uint
Expand Down Expand Up @@ -59,20 +61,20 @@ const (
DefaultChunkSize = 4096
DefaultReceiveDeadline = 0 // 0 means no deadline (timeout)
DefaultSendDeadline = 0
DefaultTCPKeepAlivePeriod = "30s"
DefaultTCPKeepAlivePeriod = 30 * time.Second

// Pool constants.
EmptyPoolCapacity = 0
DefaultPoolSize = 10
MinimumPoolSize = 2
DefaultHealthCheckPeriod = "60s" // This must match PostgreSQL authentication timeout.
DefaultHealthCheckPeriod = 60 * time.Second // This must match PostgreSQL authentication timeout.

// Server constants.
DefaultListenNetwork = "tcp"
DefaultListenAddress = "0.0.0.0:15432"
DefaultTickInterval = "5s"
DefaultTickInterval = 5 * time.Second
DefaultBufferSize = 1 << 24 // 16777216 bytes
DefaultTCPKeepAlive = "3s"
DefaultTCPKeepAlive = 3 * time.Second
DefaultLoadBalancer = "roundrobin"

// Utility constants.
Expand Down
3 changes: 2 additions & 1 deletion config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func LoadGlobalConfigDefaults(cfg *koanf.Koanf) {
"default": map[string]interface{}{
"receiveBufferSize": DefaultBufferSize,
"receiveChunkSize": DefaultChunkSize,
"tcpKeepAlivePeriod": DefaultTCPKeepAlivePeriod,
"tcpKeepAlivePeriod": DefaultTCPKeepAlivePeriod.String(),
},
},
"pools": map[string]interface{}{
Expand All @@ -121,6 +121,7 @@ func LoadGlobalConfigDefaults(cfg *koanf.Koanf) {
"default": map[string]interface{}{
"elastic": false,
"reuseElasticClients": false,
"healthCheckPeriod": DefaultHealthCheckPeriod.String(),
},
},
"server": map[string]interface{}{
Expand Down
47 changes: 16 additions & 31 deletions gatewayd.yaml
Original file line number Diff line number Diff line change
@@ -1,62 +1,47 @@
# Gatewayd config
# This file is used to configure the gatewayd daemon.

loggers:
default:
output: "console" # stdout, stderr, file or console
# Implementing file output
# file : ./logs/gatewayd.log
level: "debug" # panic, fatal, error, warn, info, debug, trace
output: "console" # stdout, stderr or file
level: "debug" # panic, fatal, error, warn, info (default), debug, trace
noColor: False
timeFormat: "unix"
timeFormat: "unix" # unixms, unixmicro and unixnano

clients:
default:
network: tcp
address: localhost:5432
# tcpKeepAlive: True
# tcpKeepAlivePeriod: 20s # duration
# receiveBufferSize: 16777216
# receiveChunkSize: 4096
# receiveDeadline: 0ms # duration, 0ms/0s means no deadline
# sendDeadline: 0ms # duration, 0ms/0s means no deadline
tcpKeepAlive: False
tcpKeepAlivePeriod: 30s # duration
receiveBufferSize: 16777216
receiveChunkSize: 4096
receiveDeadline: 0ms # duration, 0ms/0s means no deadline
sendDeadline: 0ms # duration, 0ms/0s means no deadline

pools:
default:
size: 10
# Database configs for the connection pool
client: clients.default

proxy:
default:
# Use the logger config passed here
# i.e. don't assume it's the same as the logger config above
logger: loggers.default
# Use the pool config passed here
# i.e. don't assume it's the same as the pool config above
pool: pool
elastic: False
reuseElasticClients: False
elasticClient: clients.default
healthCheckPeriod: 60s # duration

server:
network: tcp
address: 0.0.0.0:15432
# softLimit: 0
# hardLimit: 0

logger: loggers.default
proxy: proxy.default

softLimit: 0
hardLimit: 0
enableTicker: False
tickInterval: 5s # duration
multiCore: True
lockOSThread: False
loadBalancer: roundrobin
# readBufferCap: 16777216
# writeBufferCap: 16777216
# socketRecvBuffer: 16777216
# socketSendBuffer: 16777216
readBufferCap: 16777216
writeBufferCap: 16777216
socketRecvBuffer: 16777216
socketSendBuffer: 16777216
reuseAddress: True
reusePort: True
tcpKeepAlive: 3s # duration
Expand Down
6 changes: 1 addition & 5 deletions network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ func NewClient(clientConfig *config.Client, logger zerolog.Logger) *Client {
// Set the TCP keep alive.
client.TCPKeepAlive = clientConfig.TCPKeepAlive
if clientConfig.TCPKeepAlivePeriod <= 0 {
if keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod); err == nil {
client.TCPKeepAlivePeriod = keepAlive
} else {
logger.Error().Err(err).Msg("Failed to parse TCP keep alive period")
}
client.TCPKeepAlivePeriod = config.DefaultTCPKeepAlivePeriod
} else {
client.TCPKeepAlivePeriod = clientConfig.TCPKeepAlivePeriod
}
Expand Down
22 changes: 4 additions & 18 deletions network/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package network

import (
"testing"
"time"

embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/gatewayd-io/gatewayd/config"
"github.com/gatewayd-io/gatewayd/logging"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestNewClient tests the NewClient function.
Expand All @@ -34,9 +32,6 @@ func TestNewClient(t *testing.T) {

logger := logging.NewLogger(cfg)

keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod)
require.NoError(t, err)

client := NewClient(
&config.Client{
Network: "tcp",
Expand All @@ -46,7 +41,7 @@ func TestNewClient(t *testing.T) {
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: keepAlive,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
defer client.Close()
Expand Down Expand Up @@ -80,9 +75,6 @@ func TestSend(t *testing.T) {

logger := logging.NewLogger(cfg)

keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod)
require.NoError(t, err)

client := NewClient(
&config.Client{
Network: "tcp",
Expand All @@ -92,7 +84,7 @@ func TestSend(t *testing.T) {
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: keepAlive,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
defer client.Close()
Expand Down Expand Up @@ -126,9 +118,6 @@ func TestReceive(t *testing.T) {

logger := logging.NewLogger(cfg)

keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod)
require.NoError(t, err)

client := NewClient(
&config.Client{
Network: "tcp",
Expand All @@ -138,7 +127,7 @@ func TestReceive(t *testing.T) {
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: keepAlive,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
defer client.Close()
Expand Down Expand Up @@ -182,9 +171,6 @@ func TestClose(t *testing.T) {

logger := logging.NewLogger(cfg)

keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod)
require.NoError(t, err)

client := NewClient(
&config.Client{
Network: "tcp",
Expand All @@ -194,7 +180,7 @@ func TestClose(t *testing.T) {
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: keepAlive,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
assert.NotNil(t, client)
Expand Down
8 changes: 1 addition & 7 deletions network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ type Proxy struct {
var _ IProxy = &Proxy{}

// NewProxy creates a new proxy.
//
//nolint:funlen
func NewProxy(
connPool pool.IPool, hookConfig *hook.Config,
elastic, reuseElasticClients bool,
Expand All @@ -60,11 +58,7 @@ func NewProxy(
}

if proxy.HealthCheckPeriod == 0 {
if healthCheck, err := time.ParseDuration(config.DefaultHealthCheckPeriod); err == nil {
proxy.HealthCheckPeriod = healthCheck
} else {
logger.Error().Err(err).Msg("Failed to parse the health check period")
}
proxy.HealthCheckPeriod = config.DefaultHealthCheckPeriod
} else {
proxy.HealthCheckPeriod = healthCheckPeriod
}
Expand Down
25 changes: 6 additions & 19 deletions network/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package network

import (
"testing"
"time"

embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/gatewayd-io/gatewayd/config"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/gatewayd-io/gatewayd/pool"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestNewProxy tests the creation of a new proxy with a fixed connection pool.
Expand Down Expand Up @@ -39,9 +37,6 @@ func TestNewProxy(t *testing.T) {
// Create a connection pool
pool := pool.NewPool(config.EmptyPoolCapacity)

keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod)
require.NoError(t, err)

client := NewClient(
&config.Client{
Network: "tcp",
Expand All @@ -51,17 +46,15 @@ func TestNewProxy(t *testing.T) {
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: keepAlive,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
},
logger)
err = pool.Put(client.ID, client)
err := pool.Put(client.ID, client)
assert.Nil(t, err)

healthCheck, err := time.ParseDuration(config.DefaultHealthCheckPeriod)
require.NoError(t, err)

// Create a proxy with a fixed buffer pool
proxy := NewProxy(pool, hook.NewHookConfig(), false, false, healthCheck, nil, logger)
proxy := NewProxy(
pool, hook.NewHookConfig(), false, false, config.DefaultHealthCheckPeriod, nil, logger)

assert.NotNil(t, proxy)
assert.Equal(t, 0, proxy.busyConnections.Size(), "Proxy should have no connected clients")
Expand Down Expand Up @@ -89,14 +82,8 @@ func TestNewProxyElastic(t *testing.T) {
// Create a connection pool
pool := pool.NewPool(config.EmptyPoolCapacity)

healthCheck, err := time.ParseDuration(config.DefaultHealthCheckPeriod)
require.NoError(t, err)

keepAlive, err := time.ParseDuration(config.DefaultTCPKeepAlivePeriod)
require.NoError(t, err)

// Create a proxy with an elastic buffer pool
proxy := NewProxy(pool, hook.NewHookConfig(), true, false, healthCheck,
proxy := NewProxy(pool, hook.NewHookConfig(), true, false, config.DefaultHealthCheckPeriod,
&config.Client{
Network: "tcp",
Address: "localhost:5432",
Expand All @@ -105,7 +92,7 @@ func TestNewProxyElastic(t *testing.T) {
ReceiveDeadline: config.DefaultReceiveDeadline,
SendDeadline: config.DefaultSendDeadline,
TCPKeepAlive: false,
TCPKeepAlivePeriod: keepAlive,
TCPKeepAlivePeriod: config.DefaultTCPKeepAlivePeriod,
}, logger)

assert.NotNil(t, proxy)
Expand Down
8 changes: 2 additions & 6 deletions network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,8 @@ func NewServer(
}

if tickInterval == 0 {
if tickInterval, err := time.ParseDuration(config.DefaultTickInterval); err == nil {
server.TickInterval = tickInterval
logger.Debug().Msg("Tick interval is not set, using the default value")
} else {
logger.Error().Err(err).Msg("Failed to parse the default tick interval")
}
server.TickInterval = config.DefaultTickInterval
logger.Debug().Msg("Tick interval is not set, using the default value")
} else {
server.TickInterval = tickInterval
}
Expand Down
Loading