Skip to content

Commit

Permalink
fix: common db pool config wasn't reloadable (#5210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidddddarth authored Oct 18, 2024
1 parent 9683021 commit ace1447
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions utils/misc/dbutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ func NewDatabaseConnectionPool(
maxConnLifetime := maxConnLifetimeVar.Load()
db.SetConnMaxLifetime(maxConnLifetime)

ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
rruntime.Go(func() {
ticker := time.NewTicker(
conf.GetDurationVar(
5,
time.Second,
"db."+componentName+".pool.configUpdateInterval",
"db.pool.configUpdateInterval",
),
)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
Expand Down
29 changes: 29 additions & 0 deletions utils/misc/dbutils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package misc_test

import (
"context"
"database/sql"
"fmt"
"testing"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/rudderlabs/rudder-go-kit/config"
"github.com/rudderlabs/rudder-go-kit/stats"
"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource/postgres"
"github.com/rudderlabs/rudder-server/utils/misc"
)
Expand Down Expand Up @@ -128,3 +130,30 @@ func TestIdleTxTimeout(t *testing.T) {
require.NoError(t, tx.Commit())
})
}

func TestCommonPool(t *testing.T) {
pool, err := dockertest.NewPool("")
require.NoError(t, err)
postgresContainer, err := postgres.Setup(pool, t)
require.NoError(t, err)

conf := config.New()
conf.Set("DB.host", postgresContainer.Host)
conf.Set("DB.user", postgresContainer.User)
conf.Set("DB.name", postgresContainer.Database)
conf.Set("DB.port", postgresContainer.Port)
conf.Set("DB.password", postgresContainer.Password)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf.Set("db.test.pool.configUpdateInterval", 10*time.Millisecond)
db, err := misc.NewDatabaseConnectionPool(ctx, conf, stats.NOP, "test")
require.NoError(t, err)
require.NoError(t, db.Ping())
defer db.Close()
require.Equal(t, 40, db.Stats().MaxOpenConnections)

conf.Set("db.test.pool.maxOpenConnections", 5)
time.Sleep(100 * time.Millisecond)
require.Equal(t, 5, db.Stats().MaxOpenConnections)
}

0 comments on commit ace1447

Please sign in to comment.