Skip to content

Update database connections defaults #5853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 14, 2025

Conversation

jananiarunachalam
Copy link
Contributor

@jananiarunachalam jananiarunachalam commented Feb 9, 2025

Pull Request check list

  • Commit conforms to CONTRIBUTING.md?
  • Proper tests/regressions included?
  • Documentation updated?

Affected functionality

Description of change

Which issue this PR fixes
Fixes #5802

Copy link
Member

@amartinezfayo amartinezfayo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jananiarunachalam for working on this contribution!

Comment on lines 1068 to 1072
defaultConnMaxLifetime, err := time.ParseDuration("30s")
db.DB().SetConnMaxLifetime(defaultConnMaxLifetime) // default value
if err != nil {
return nil, "", false, nil, fmt.Errorf("failed to parse conn_max_lifetime %q: %w", defaultConnMaxLifetime, err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SetConnMaxLifetime function is already called later, based on the ConnMaxLifetime setting.
We don't need to make any change there. What we need to adjust is the ConnMaxIdleTime property calling SetConnMaxIdleTime, maybe with a value of 30s.
Also note that you will not need to make any parsing for that (i.e. time.ParseDuration("30s")), you can just call SetConnMaxIdleTime(time.Second * 30). We may define this as a constant in the function, and probably the MaxOpenConns and MaxIdleConns numbers too.

@sorindumitru
Copy link
Collaborator

Also, could you update the documentation in https://github.com/spiffe/spire/blob/main/doc/plugin_server_datastore_sql.md with the new defaults?

Comment on lines 14 to 15
| 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: 30s) |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual change

@jananiarunachalam jananiarunachalam force-pushed the issue-5802-fix-db-conns branch 4 times, most recently from 7fc428b to b9ec496 Compare March 12, 2025 08:55
if cfg.MaxIdleConns != nil {
db.DB().SetMaxIdleConns(*cfg.MaxIdleConns)
}
const connMaxLifetime = time.Second * 30
db.DB().SetConnMaxLifetime(connMaxLifetime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SetConnMaxLifetime function is already called later, based on the ConnMaxLifetime setting. We shouldn't need to make any change here. What we need to adjust is the ConnMaxIdleTime property calling SetConnMaxIdleTime, maybe with a value of 30s.

| max_idle_conns | The maximum number of idle connections in the pool (default: 2) |
| conn_max_lifetime | The maximum amount of time a connection may be reused (default: unlimited) |
| 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: 30s) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to make any change in the conn_max_lifetime. We need to set ConnMaxIdleTime to 30s and document it (we will not expose a setting for that).

@sorindumitru sorindumitru added this to the 1.12.0 milestone Mar 13, 2025
Signed-off-by: jananiarunachalam <[email protected]>
Signed-off-by: jananiarunachalam <[email protected]>
Signed-off-by: jananiarunachalam <[email protected]>
Signed-off-by: jananiarunachalam <[email protected]>
Signed-off-by: jananiarunachalam <[email protected]>
@jananiarunachalam jananiarunachalam force-pushed the issue-5802-fix-db-conns branch from b9ec496 to 0677af5 Compare March 14, 2025 16:32
Signed-off-by: jananiarunachalam <[email protected]>
Copy link
Member

@amartinezfayo amartinezfayo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jananiarunachalam!

@sorindumitru sorindumitru merged commit e35cb7e into spiffe:main Mar 14, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider changing defaults for database connections
3 participants