Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304
Draft
apoorvdeshmukh wants to merge 2 commits into
Draft
Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304apoorvdeshmukh wants to merge 2 commits into
apoorvdeshmukh wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an automatic “pruning” mechanism to ChannelDbConnectionPool (pool V2) to gradually reduce excess idle connections during low demand by sampling idle counts over time and pruning based on the median sample.
Changes:
- Introduces pruning state, sampling logic, and a one-shot timer callback to periodically evaluate and close excess idle connections.
- Wires pruning timer enable/disable behavior into connection add/remove paths and disposes the timer during pool shutdown.
- Adds a dedicated unit test suite validating pruning configuration, sampling/median behavior, floors (MinPoolSize), and shutdown behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/ChannelDbConnectionPool.cs | Implements the pruning timer, sampling window/median computation, and integrates timer lifecycle with pool growth/shrink/shutdown. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolPruningTest.cs | Adds unit tests covering pruning timer creation/configuration, sampling/median pruning behavior, and shutdown semantics. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Summary
Implements automatic pool size reduction for
ChannelDbConnectionPool(connection pool V2). When demand drops, excess idle connections are gradually closed based on observed usage patterns rather than reacting to instantaneous idle counts.Design
LoadBalanceTimeout / PruningIntervalsamples (defaults to 300s / 10s = 30 samples whenConnection Lifetimeis 0).MinPoolSize.MinPoolSize >= MaxPoolSize, no timer is created.Changes
ChannelDbConnectionPool.cs: Added pruning fields, timer creation in constructor,UpdatePruningTimer(),PruneIdleConnections()static callback,DivideRoundingUp()helper, and timer disposal inShutdown().Thread Safety
lock(_pruningTimer)to read/write shared sample state.Shutdown()acquires the same lock before disposing the timer.UpdatePruningTimer()checksIsRunningat entry to avoid post-shutdown races.pool.IsRunningon each iteration.Issues
AB#44848
Testing
Tests
ChannelDbConnectionPoolPruningTest.cs: 27 unit tests covering:UpdatePruningTimerenable/disable logicPruneIdleConnectionssampling, buffer reset, MinPoolSize floor, in-use connection safetyDivideRoundingUpcorrectnessGuidelines
Please review the contribution guidelines before submitting a pull request: