Skip to content

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

Draft
apoorvdeshmukh wants to merge 2 commits into
mainfrom
dev/ad/conn-prune
Draft

Add automatic pool size reduction (pruning) to ChannelDbConnectionPool#4304
apoorvdeshmukh wants to merge 2 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.

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

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

2 participants