From e35cb7eb0819c5aebf2dd67fa5a41e53e71fd3d0 Mon Sep 17 00:00:00 2001 From: jananiarunachalam Date: Fri, 14 Mar 2025 17:47:12 +0000 Subject: [PATCH] Update database connections defaults (#5853) * Update database connections defaults Signed-off-by: jananiarunachalam * Update test Signed-off-by: jananiarunachalam * PR comment: SetConnMaxIdleTime Signed-off-by: jananiarunachalam * PR comment: update doc Signed-off-by: jananiarunachalam * Update doc: 30s -> unlimited Signed-off-by: jananiarunachalam * SetConnMaxLifetime -> SetConnMaxIdleTime Signed-off-by: jananiarunachalam * Update doc w ConnMaxIdleTime Signed-off-by: jananiarunachalam --------- Signed-off-by: jananiarunachalam --- doc/plugin_server_datastore_sql.md | 4 +++- pkg/server/datastore/sqlstore/sqlstore.go | 8 +++++++- pkg/server/datastore/sqlstore/sqlstore_test.go | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/plugin_server_datastore_sql.md b/doc/plugin_server_datastore_sql.md index cb343d9cb..cfb3d1724 100644 --- a/doc/plugin_server_datastore_sql.md +++ b/doc/plugin_server_datastore_sql.md @@ -11,13 +11,15 @@ The `sql` plugin implements SQL based data storage for the SPIRE server using SQ | client_cert_path | Path to client certificate (MySQL only) | | client_key_path | Path to private key for client certificate (MySQL only) | | max_open_conns | The maximum number of open db connections (default: 100) | -| max_idle_conns | The maximum number of idle connections in the pool (default: 2) | +| max_idle_conns | The maximum number of idle connections in the pool (default: 100) | | conn_max_lifetime | The maximum amount of time a connection may be reused (default: unlimited) | | disable_migration | True to disable auto-migration functionality. Use of this flag allows finer control over when datastore migrations occur and coordination of the migration of a datastore shared with a SPIRE Server cluster. Only available for databases from SPIRE Code version 0.9.0 or later. | For more information on the `max_open_conns`, `max_idle_conns`, and `conn_max_lifetime`, refer to the documentation for the Go [`database/sql`](https://golang.org/pkg/database/sql/#DB) package. +> **Note:** The SQL plugin uses an internal default setting of 30 seconds for the maximum idle time per connection (ConnMaxIdleTime). This setting is not configurable through the plugin configuration. + ## Database configurations ### `database_type = "sqlite3"` diff --git a/pkg/server/datastore/sqlstore/sqlstore.go b/pkg/server/datastore/sqlstore/sqlstore.go index 74b14108e..b4cd86a97 100644 --- a/pkg/server/datastore/sqlstore/sqlstore.go +++ b/pkg/server/datastore/sqlstore/sqlstore.go @@ -1052,13 +1052,19 @@ func (ds *Plugin) openDB(cfg *configuration, isReadOnly bool) (*gorm.DB, string, db.SetLogger(gormLogger{ log: ds.log.WithField(telemetry.SubsystemName, "gorm"), }) - db.DB().SetMaxOpenConns(100) // default value + + const maxOpenConns = 100 + db.DB().SetMaxOpenConns(maxOpenConns) if cfg.MaxOpenConns != nil { db.DB().SetMaxOpenConns(*cfg.MaxOpenConns) } + const maxIdleConns = 100 + db.DB().SetMaxIdleConns(maxIdleConns) if cfg.MaxIdleConns != nil { db.DB().SetMaxIdleConns(*cfg.MaxIdleConns) } + const ConnMaxIdleTime = time.Second * 30 + db.DB().SetConnMaxIdleTime(ConnMaxIdleTime) if cfg.ConnMaxLifetime != nil { connMaxLifetime, err := time.ParseDuration(*cfg.ConnMaxLifetime) if err != nil { diff --git a/pkg/server/datastore/sqlstore/sqlstore_test.go b/pkg/server/datastore/sqlstore/sqlstore_test.go index e98d600bd..dd313160a 100644 --- a/pkg/server/datastore/sqlstore/sqlstore_test.go +++ b/pkg/server/datastore/sqlstore/sqlstore_test.go @@ -5252,7 +5252,7 @@ func (s *PluginSuite) TestConfigure() { desc: "defaults", expectMaxOpenConns: 100, // defined in database/sql - expectIdle: 2, + expectIdle: 100, }, { desc: "zero values",