Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304
Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304apoorvdeshmukh wants to merge 8 commits into
Conversation
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. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4304 +/- ##
==========================================
- Coverage 66.44% 65.02% -1.43%
==========================================
Files 284 279 -5
Lines 43301 66167 +22866
==========================================
+ Hits 28773 43025 +14252
- Misses 14528 23142 +8614
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
mdaigle
left a comment
There was a problem hiding this comment.
Would like to see some improvements to the tests, otherwise looks good!
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: