Update database connections defaults (#5853)

* Update database connections defaults

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

* Update test

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

* PR comment: SetConnMaxIdleTime

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

* PR comment: update doc

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

* Update doc: 30s -> unlimited

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

* SetConnMaxLifetime -> SetConnMaxIdleTime

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

* Update doc w ConnMaxIdleTime

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>

---------

Signed-off-by: jananiarunachalam <jananiarunachalam17@gmail.com>
This commit is contained in:
jananiarunachalam 2025-03-14 17:47:12 +00:00 committed by GitHub
parent ecec291c9c
commit e35cb7eb08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 3 deletions

View File

@ -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"`

View File

@ -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 {

View File

@ -5252,7 +5252,7 @@ func (s *PluginSuite) TestConfigure() {
desc: "defaults",
expectMaxOpenConns: 100,
// defined in database/sql
expectIdle: 2,
expectIdle: 100,
},
{
desc: "zero values",