From 909d10c4bba70ce71f13d2d53d848b8fd4e0014d Mon Sep 17 00:00:00 2001 From: Michelangelo Mori Date: Wed, 18 Sep 2024 13:40:00 +0200 Subject: [PATCH] Make max number of idle connections configurable. This change allows setting the maximum number of idle connections via configuration file. This is useful since local testing showed that Minder `eventer` spends roughly ~25% of its time opening connections to Postgres. --- internal/config/common.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/config/common.go b/internal/config/common.go index efccc34aaa..3e88bdb52c 100644 --- a/internal/config/common.go +++ b/internal/config/common.go @@ -34,12 +34,13 @@ import ( // DatabaseConfig is the configuration for the database type DatabaseConfig struct { - Host string `mapstructure:"dbhost" default:"localhost"` - Port int `mapstructure:"dbport" default:"5432"` - User string `mapstructure:"dbuser" default:"postgres"` - Password string `mapstructure:"dbpass" default:"postgres"` - Name string `mapstructure:"dbname" default:"minder"` - SSLMode string `mapstructure:"sslmode" default:"disable"` + Host string `mapstructure:"dbhost" default:"localhost"` + Port int `mapstructure:"dbport" default:"5432"` + User string `mapstructure:"dbuser" default:"postgres"` + Password string `mapstructure:"dbpass" default:"postgres"` + Name string `mapstructure:"dbname" default:"minder"` + SSLMode string `mapstructure:"sslmode" default:"disable"` + IdleConnections int `mapstructure:"idle_connections" default:"0"` } // GetDBConnection returns a connection to the database @@ -54,6 +55,10 @@ func (c *DatabaseConfig) GetDBConnection(ctx context.Context) (*sql.DB, string, return nil, "", err } + if c.IdleConnections != 0 { + conn.SetMaxIdleConns(c.IdleConnections) + } + for i := 0; i < 8; i++ { zerolog.Ctx(ctx).Info().Int("try number", i).Msg("Trying to connect to DB") // we don't defer canceling the context because we want to cancel it as soon as we're done