Skip to content

Commit

Permalink
Parametrize redis pool size configuration (#487)
Browse files Browse the repository at this point in the history
Parametrize redis pool size configuration
  • Loading branch information
guilhermocc authored Jul 5, 2022
1 parent 2d8fce3 commit 3a753de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internalApi:
enabled: true

adapters:
redis:
poolSize: 500
schedulerStorage:
postgres:
url: "postgres://maestro:maestro@localhost:5432/maestro?sslmode=disable"
Expand Down
23 changes: 13 additions & 10 deletions internal/service/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ const (
// Redis instance storage
instanceStorageRedisUrlPath = "adapters.instanceStorage.redis.url"
instanceStorageRedisScanSizePath = "adapters.instanceStorage.redis.scanSize"
// Redis operation flow
operationFlowRedisUrlPath = "adapters.operationFlow.redis.url"
// Redis configs
redisPoolSize = "adapters.redis.poolSize"
// Random port allocator
portAllocatorRandomRangePath = "adapters.portAllocator.random.range"
// Postgres scheduler storage
schedulerStoragePostgresUrlPath = "adapters.schedulerStorage.postgres.url"
// Redis operation flow
operationFlowRedisUrlPath = "adapters.operationFlow.redis.url"

// Health Controller operation TTL
healthControllerOperationTTL = "workers.redis.operationsTtl"
)
Expand Down Expand Up @@ -121,7 +124,7 @@ func NewRuntimeKubernetes(c config.Config) (ports.Runtime, error) {
}

func NewOperationStorageRedis(clock ports.Clock, c config.Config) (ports.OperationStorage, error) {
client, err := createRedisClient(c.GetString(operationStorageRedisUrlPath))
client, err := createRedisClient(c, c.GetString(operationStorageRedisUrlPath))
if err != nil {
return nil, fmt.Errorf("failed to initialize Redis operation storage: %w", err)
}
Expand All @@ -134,7 +137,7 @@ func NewOperationStorageRedis(clock ports.Clock, c config.Config) (ports.Operati
}

func NewOperationLeaseStorageRedis(clock ports.Clock, c config.Config) (ports.OperationLeaseStorage, error) {
client, err := createRedisClient(c.GetString(operationLeaseStorageRedisUrlPath))
client, err := createRedisClient(c, c.GetString(operationLeaseStorageRedisUrlPath))
if err != nil {
return nil, fmt.Errorf("failed to initialize Redis operation lease storage: %w", err)
}
Expand All @@ -143,7 +146,7 @@ func NewOperationLeaseStorageRedis(clock ports.Clock, c config.Config) (ports.Op
}

func NewRoomStorageRedis(c config.Config) (ports.RoomStorage, error) {
client, err := createRedisClient(c.GetString(roomStorageRedisUrlPath))
client, err := createRedisClient(c, c.GetString(roomStorageRedisUrlPath))
if err != nil {
return nil, fmt.Errorf("failed to initialize Redis room storage: %w", err)
}
Expand All @@ -152,7 +155,7 @@ func NewRoomStorageRedis(c config.Config) (ports.RoomStorage, error) {
}

func NewGameRoomInstanceStorageRedis(c config.Config) (ports.GameRoomInstanceStorage, error) {
client, err := createRedisClient(c.GetString(instanceStorageRedisUrlPath))
client, err := createRedisClient(c, c.GetString(instanceStorageRedisUrlPath))
if err != nil {
return nil, fmt.Errorf("failed to initialize Redis instance storage: %w", err)
}
Expand All @@ -161,7 +164,7 @@ func NewGameRoomInstanceStorageRedis(c config.Config) (ports.GameRoomInstanceSto
}

func NewSchedulerCacheRedis(c config.Config) (ports.SchedulerCache, error) {
client, err := createRedisClient(c.GetString(schedulerCacheRedisUrlPath))
client, err := createRedisClient(c, c.GetString(schedulerCacheRedisUrlPath))
if err != nil {
return nil, fmt.Errorf("failed to initialize Redis scheduler cache: %w", err)
}
Expand Down Expand Up @@ -195,12 +198,12 @@ func NewSchedulerStoragePg(c config.Config) (ports.SchedulerStorage, error) {
return scheduleradapters.NewSchedulerStorage(opts), nil
}

func createRedisClient(url string) (*redis.Client, error) {
func createRedisClient(c config.Config, url string) (*redis.Client, error) {
opts, err := redis.ParseURL(url)
if err != nil {
return nil, fmt.Errorf("invalid redis URL: %w", err)
}
opts.PoolSize = 500
opts.PoolSize = c.GetInt(redisPoolSize)
return redis.NewClient(opts), nil
}

Expand All @@ -215,7 +218,7 @@ func NewAutoscaler(policies autoscaler.PolicyMap) autoscalerports.Autoscaler {
}

func NewOperationFlowRedis(c config.Config) (ports.OperationFlow, error) {
client, err := createRedisClient(c.GetString(operationFlowRedisUrlPath))
client, err := createRedisClient(c, c.GetString(operationFlowRedisUrlPath))
if err != nil {
return nil, fmt.Errorf("failed to initialize Redis operation storage: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/service/adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestOperationStorageRedis(t *testing.T) {
config := configmock.NewMockConfig(mockCtrl)

config.EXPECT().GetString(operationStorageRedisUrlPath).Return(getRedisUrl(t))
config.EXPECT().GetInt(redisPoolSize).Return(500)
config.EXPECT().GetDuration(healthControllerOperationTTL).Return(time.Minute)
opStorage, err := NewOperationStorageRedis(clock, config)
require.NoError(t, err)
Expand All @@ -81,6 +82,7 @@ func TestOperationStorageRedis(t *testing.T) {

config.EXPECT().GetString(operationStorageRedisUrlPath).Return("redis://somewhere-in-the-world:6379")
config.EXPECT().GetDuration(healthControllerOperationTTL).Return(time.Minute)
config.EXPECT().GetInt(redisPoolSize).Return(500)
opStorage, err := NewOperationStorageRedis(clock, config)
require.NoError(t, err)

Expand Down Expand Up @@ -110,6 +112,7 @@ func TestRoomStorageRedis(t *testing.T) {
config := configmock.NewMockConfig(mockCtrl)

config.EXPECT().GetString(roomStorageRedisUrlPath).Return(getRedisUrl(t))
config.EXPECT().GetInt(redisPoolSize).Return(500)
roomStorage, err := NewRoomStorageRedis(config)
require.NoError(t, err)

Expand All @@ -124,6 +127,7 @@ func TestRoomStorageRedis(t *testing.T) {
config := configmock.NewMockConfig(mockCtrl)

config.EXPECT().GetString(roomStorageRedisUrlPath).Return("redis://somewhere-in-the-world:6379")
config.EXPECT().GetInt(redisPoolSize).Return(500)
roomStorage, err := NewRoomStorageRedis(config)
require.NoError(t, err)

Expand Down Expand Up @@ -154,6 +158,7 @@ func TestInstanceStorageRedis(t *testing.T) {

config.EXPECT().GetString(instanceStorageRedisUrlPath).Return(getRedisUrl(t))
config.EXPECT().GetInt(instanceStorageRedisScanSizePath).Return(10)
config.EXPECT().GetInt(redisPoolSize).Return(500)
instanceStorage, err := NewGameRoomInstanceStorageRedis(config)
require.NoError(t, err)

Expand All @@ -169,6 +174,7 @@ func TestInstanceStorageRedis(t *testing.T) {

config.EXPECT().GetString(instanceStorageRedisUrlPath).Return("redis://somewhere-in-the-world:6379")
config.EXPECT().GetInt(instanceStorageRedisScanSizePath).Return(10)
config.EXPECT().GetInt(redisPoolSize).Return(500)
instanceStorage, err := NewGameRoomInstanceStorageRedis(config)
require.NoError(t, err)

Expand Down Expand Up @@ -254,6 +260,7 @@ func TestOperationFlowRedis(t *testing.T) {
config := configmock.NewMockConfig(mockCtrl)

config.EXPECT().GetString(operationFlowRedisUrlPath).Return(getRedisUrl(t))
config.EXPECT().GetInt(redisPoolSize).Return(500)
operationFlow, err := NewOperationFlowRedis(config)
require.NoError(t, err)

Expand All @@ -268,6 +275,7 @@ func TestOperationFlowRedis(t *testing.T) {
config := configmock.NewMockConfig(mockCtrl)

config.EXPECT().GetString(operationFlowRedisUrlPath).Return("redis://somewhere-in-the-world:6379")
config.EXPECT().GetInt(redisPoolSize).Return(500)
operationFlow, err := NewOperationFlowRedis(config)
require.NoError(t, err)

Expand Down

0 comments on commit 3a753de

Please sign in to comment.