rails icon indicating copy to clipboard operation
rails copied to clipboard

Add minimum size to connection pool

Open catlee opened this issue 1 year ago • 0 comments

Motivation / Background

This PR implements the minimum connection pool size idea described in #50989.

The primary motivation with this is to ensure that we always have at least N connections established to the DB so that we don't need to spend time connecting to the DB during the handling of a request.

Fixes #50989

Detail

In ConnectionPool, rename :size to :max_size, and add :min_size as a configuration option. :min_size is initialized from db_config.min_size, where I've also added a new configuration option.

ConnectionPool#initialize calls a new method, ensure_minimum_connections when min_size is > 0.

In ConnectionPool#flush, we need to pay attention to the minimum pool size, and make sure that we don't close too many connections.

Finally, ConnectionPool::Reaper also calls into ensure_minimum_connections after reaping/flushing to ensure that we always have the desired number of established connections active.

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • [x] This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • [x] Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • [x] Tests are added or updated if you fix a bug or add a feature.
  • [x] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

catlee avatar Feb 06 '24 22:02 catlee