Skip to content

Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304

Open
apoorvdeshmukh wants to merge 8 commits into
mainfrom
dev/ad/conn-prune
Open

Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304
apoorvdeshmukh wants to merge 8 commits into
mainfrom
dev/ad/conn-prune

Conversation

@apoorvdeshmukh
Copy link
Copy Markdown
Contributor

@apoorvdeshmukh apoorvdeshmukh commented May 21, 2026

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

  • Sampling-based pruning: A one-shot timer fires every 10 seconds, recording the current idle connection count into a circular buffer.
  • Median-based decision: Once the buffer fills, samples are sorted and the median is used as the prune target. This smooths out brief traffic lulls and avoids over-pruning.
  • Sample window: LoadBalanceTimeout / PruningInterval samples (defaults to 300s / 10s = 30 samples when Connection Lifetime is 0).
  • MinPoolSize floor: Pruning never reduces the pool below MinPoolSize.
  • Fixed-size pools skip pruning: When 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 in Shutdown().

Thread Safety

  • Timer callback acquires lock(_pruningTimer) to read/write shared sample state.
  • Shutdown() acquires the same lock before disposing the timer.
  • UpdatePruningTimer() checks IsRunning at entry to avoid post-shutdown races.
  • Prune loop checks pool.IsRunning on each iteration.

Issues

AB#44848

Testing

Tests

  • ChannelDbConnectionPoolPruningTest.cs: 27 unit tests covering:
    • Timer creation and configuration (fixed-size pool skips, sample size calculation)
    • UpdatePruningTimer enable/disable logic
    • PruneIdleConnections sampling, buffer reset, MinPoolSize floor, in-use connection safety
    • DivideRoundingUp correctness
    • Shutdown behavior
    • Acceptance scenarios (excess idle pruned, no pruning at min, median prevents aggressive pruning)

Guidelines

Please review the contribution guidelines before submitting a pull request:

@apoorvdeshmukh apoorvdeshmukh requested a review from a team as a code owner May 21, 2026 16:17
Copilot AI review requested due to automatic review settings May 21, 2026 16:17
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board May 21, 2026
@apoorvdeshmukh apoorvdeshmukh marked this pull request as draft May 21, 2026 16:17
@apoorvdeshmukh apoorvdeshmukh added this to the 7.1.0-preview2 milestone May 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@apoorvdeshmukh apoorvdeshmukh marked this pull request as ready for review May 27, 2026 13:44
Copilot AI review requested due to automatic review settings May 27, 2026 13:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

❌ Patch coverage is 96.33028% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.02%. Comparing base (f873c1a) to head (722d33f).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...qlClient/ConnectionPool/ChannelDbConnectionPool.cs 96.33% 4 Missing ⚠️
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     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 65.02% <96.33%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@mdaigle mdaigle left a comment

Choose a reason for hiding this comment

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

Would like to see some improvements to the tests, otherwise looks good!

@github-project-automation github-project-automation Bot moved this from To triage to Waiting for customer in SqlClient Board May 27, 2026
@apoorvdeshmukh apoorvdeshmukh requested a review from mdaigle May 29, 2026 17:08
Copilot AI review requested due to automatic review settings May 30, 2026 10:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 12 comments.

Copilot AI review requested due to automatic review settings May 30, 2026 11:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Waiting for customer

Development

Successfully merging this pull request may close these issues.

4 participants