Dev/mdaigle/pool rate limit#4376
Draft
mdaigle wants to merge 16 commits into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds infrastructure for connection-pool rate limiting and aligns both pool implementations on a shared “blocking period” error-state mechanism (fast-fail after connection-creation failures with exponential backoff), with accompanying unit tests and a draft spec.
Changes:
- Introduces
BlockingPeriodErrorStateand integrates it into bothChannelDbConnectionPoolandWaitHandleDbConnectionPool. - Adds initial plumbing for connection-creation rate limiting in
ChannelDbConnectionPool(plusNoOpAcquiredLease). - Adds unit tests and a feature spec/diagram documenting intended behavior.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolTest.cs | Adds unit tests for blocking period behavior and rate-limit lease disposal scenarios. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs | Refactors blocking-period logic to use the shared BlockingPeriodErrorState and pool-group policy helper. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/NoOpAcquiredLease.cs | Adds a singleton no-op RateLimitLease used when no limiter is configured. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/DbConnectionPoolGroup.cs | Centralizes blocking-period enablement policy in IsBlockingPeriodEnabled(). |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/ChannelDbConnectionPool.cs | Adds blocking-period error state support and rate-limit acquisition logic on connection creation. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/BlockingPeriodErrorState.cs | New shared implementation for cached error + exponential backoff + timer-based recovery. |
| src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj | Adds System.Threading.RateLimiting package reference. |
| specs/006-pool-rate-limiting/spec.md | Draft feature specification for rate limiting + blocking period semantics. |
| specs/006-pool-rate-limiting/diagrams.md | Mermaid diagrams comparing old vs new pool throttling approach. |
| Directory.Packages.props | Adds central package version entry for System.Threading.RateLimiting. |
Comment on lines
+446
to
+449
| try | ||
| { | ||
| using RateLimitLease lease = _connectionCreationRateLimiter?.AttemptAcquire(1) ?? NoOpAcquiredLease.Instance; | ||
|
|
Comment on lines
+481
to
+492
| finally | ||
| { | ||
| // Disposing the lease releases the permit back to the limiter (no-op | ||
| // for the default lease). After releasing, signal a waiter on the | ||
| // idle channel that they may now retry an open. We only do this if | ||
| // the pool can still grow; if we're at MaxPoolSize, only a connection | ||
| // return can satisfy a waiter. FR-004. | ||
| if (lease.IsAcquired && _connectionSlots.ReservationCount < MaxPoolSize) | ||
| { | ||
| _idleChannel.TryWrite(null); | ||
| } | ||
| } |
Comment on lines
+509
to
+513
| // A successful creation clears any prior error state and resets backoff. FR-009. | ||
| if (connection is not null && _errorState.HasError) | ||
| { | ||
| _errorState.Clear(); | ||
| } |
Comment on lines
+517
to
+524
| catch (Exception ex) when (ADP.IsCatchableExceptionType(ex)) | ||
| { | ||
| // Enter the blocking period error state on creation failure if configured. | ||
| // FR-006, FR-007. | ||
| if (PoolGroup.IsBlockingPeriodEnabled()) | ||
| { | ||
| _errorState.Enter(ex); | ||
| } |
Comment on lines
+97
to
+103
| /// <summary> | ||
| /// Optional rate limiter that throttles the number of concurrent physical connection | ||
| /// creation attempts. When null, no rate limiting is applied. A non-null limiter is | ||
| /// supplied at pool construction time; there is no default. Callers fast-fail against | ||
| /// the limiter and fall back to the idle-channel wait when no permit is available. | ||
| /// </summary> | ||
| private readonly RateLimiter? _connectionCreationRateLimiter; |
Comment on lines
+5
to
+7
| using System; | ||
| using System.Threading; | ||
|
|
Comment on lines
+96
to
+101
| /// <summary> | ||
| /// Determines whether the blocking period is enabled for this pool group based on the | ||
| /// configured <see cref="PoolBlockingPeriod"/> and the target data source. Returns true | ||
| /// when no connection options are available so callers fail safe into the blocking | ||
| /// behavior. | ||
| /// </summary> |
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
Provide a summary of the changes being introduced. Important topics to cover
include:
High quality descriptions will lead to a smoother review experience.
Issues
Link to any relevant issues, bugs, or discussions (e.g.,
Closes #123,Fixes issue #456).Testing
Describe the automated tests (unit, integration) you created or modified.
Provide justification for any gap in automated testing. List any manual testing
steps that were performed to ensure the changes work.
Guidelines
Please review the contribution guidelines before submitting a pull request: