-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scaffolding for pool throttling #2667
Merged
mdaigle
merged 14 commits into
dotnet:feat/sqlclientx
from
mdaigle:throttling-scaffolding
Jul 15, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e55336a
Add basic rate limiters
mdaigle 786572a
Add license headers. Clean up usings.
mdaigle 1a1f96a
Add docs.
mdaigle 6cb8878
Instantiate pooling data source via builder. Set rate limiter based o…
mdaigle 310a598
Compilation fixes. Remove auth params for now.
mdaigle 61cd913
Merge branch 'feat/sqlclientx' into throttling-scaffolding
mdaigle 394516b
Use delegate to better capture callback signature.
mdaigle b381129
Merge branch 'throttling-scaffolding' of github.com:mdaigle/SqlClient…
mdaigle a9ae6eb
Use state type to remove closed params. Clean up docs.
mdaigle 4bbc3f2
Implement review suggestions.
mdaigle c99bdd9
Implement dispose.
mdaigle a7f3406
Dispose next rate limiter.
mdaigle 02a74e5
Dispose rate limiter on pool shutdown.
mdaigle 6ba5e88
Address review nits.
mdaigle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
20 changes: 20 additions & 0 deletions
20
...rosoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/RateLimiters/AsyncFlagFunc.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Threading; | ||
|
||
namespace Microsoft.Data.SqlClientX.RateLimiters | ||
{ | ||
/// <summary> | ||
/// A function that operates asynchronously based on a flag. If isAsync is true, the function operates asynchronously. | ||
/// If isAsync is false, the function operates synchronously. | ||
/// </summary> | ||
/// <typeparam name="TState">The type accepted by the callback as input.</typeparam> | ||
/// <typeparam name="TResult">The type returned by the callback.</typeparam> | ||
/// <param name="state">An instance of State to be passed to the callback.</param> | ||
/// <param name="isAsync">Indicates whether the function should operate asynchronously.</param> | ||
/// <param name="cancellationToken">Allows cancellation of the operation.</param> | ||
/// <returns>Returns the result of the callback.</returns> | ||
internal delegate TResult AsyncFlagFunc<in TState, out TResult>(TState state, bool isAsync, CancellationToken cancellationToken); | ||
} |
34 changes: 34 additions & 0 deletions
34
...SqlClient/netcore/src/Microsoft/Data/SqlClientX/RateLimiters/BlockingPeriodRateLimiter.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Data.SqlClientX.RateLimiters | ||
{ | ||
/// <summary> | ||
/// A rate limiter that enforces a backoff (blocking) period upon error. | ||
/// Each subsequent error increases the blocking duration, up to a maximum, until a success occurs. | ||
/// </summary> | ||
internal sealed class BlockingPeriodRateLimiter : RateLimiterBase | ||
{ | ||
/// <inheritdoc/> | ||
internal override ValueTask<TResult> Execute<State, TResult>( | ||
AsyncFlagFunc<State, ValueTask<TResult>> callback, | ||
State state, | ||
bool isAsync, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ta.SqlClient/netcore/src/Microsoft/Data/SqlClientX/RateLimiters/ConcurrencyRateLimiter.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Data.SqlClientX.RateLimiters | ||
{ | ||
/// <summary> | ||
/// A rate limiter that enforces a concurrency limit. | ||
/// When the limit is reached, new requests must wait until a spot is freed upon completion of an existing request. | ||
/// </summary> | ||
internal class ConcurrencyRateLimiter : RateLimiterBase | ||
{ | ||
private readonly SemaphoreSlim _concurrencyLimitSemaphore; | ||
|
||
/// <summary> | ||
/// Initializes a new ConcurrencyRateLimiter with the specified concurrency limit. | ||
/// </summary> | ||
/// <param name="concurrencyLimit">The maximum number of concurrent requests.</param> | ||
internal ConcurrencyRateLimiter(int concurrencyLimit) | ||
{ | ||
_concurrencyLimitSemaphore = new SemaphoreSlim(concurrencyLimit); | ||
} | ||
|
||
/// <inheritdoc/> | ||
internal sealed override async ValueTask<TResult> Execute<State, TResult>(AsyncFlagFunc<State, ValueTask<TResult>> callback, State state, bool isAsync, CancellationToken cancellationToken = default) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
//TODO: in the future, we can enforce order | ||
if (isAsync) | ||
{ | ||
await _concurrencyLimitSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
_concurrencyLimitSemaphore.Wait(cancellationToken); | ||
} | ||
|
||
try | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
if (Next != null) | ||
{ | ||
return await Next.Execute(callback, state, isAsync, cancellationToken).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
return await callback(state, isAsync, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
finally | ||
{ | ||
_concurrencyLimitSemaphore.Release(); | ||
} | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
_concurrencyLimitSemaphore.Dispose(); | ||
Next.Dispose(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ta.SqlClient/netcore/src/Microsoft/Data/SqlClientX/RateLimiters/PassthroughRateLimiter.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Data.SqlClientX.RateLimiters | ||
{ | ||
/// <summary> | ||
/// A no-op rate limiter that simply executes the callback or passes through to the next rate limiter. | ||
/// </summary> | ||
internal sealed class PassthroughRateLimiter : RateLimiterBase | ||
{ | ||
//TODO: no state, add static instance | ||
|
||
/// <inheritdoc/> | ||
internal override ValueTask<TResult> Execute<State, TResult>( | ||
AsyncFlagFunc<State, ValueTask<TResult>> callback, | ||
State state, | ||
bool isAsync, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (Next != null) | ||
{ | ||
return Next.Execute(callback, state, isAsync, cancellationToken); | ||
} | ||
else | ||
{ | ||
return callback(state, isAsync, cancellationToken); | ||
} | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
Next.Dispose(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...soft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClientX/RateLimiters/RateLimiterBase.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Data.SqlClientX.RateLimiters | ||
{ | ||
/// <summary> | ||
/// An interface for rate limiters that execute arbitraty code. Intended to be small and self contained and chained together to achieve more complex behavior. | ||
/// </summary> | ||
internal abstract class RateLimiterBase : IDisposable | ||
{ | ||
|
||
/// <summary> | ||
/// The next rate limiter that should be executed within the context of this rate limiter. | ||
/// </summary> | ||
protected readonly RateLimiterBase Next; | ||
|
||
/// <summary> | ||
/// Execute the provided callback within the context of the rate limit, or pass the responsibility along to the next rate limiter. | ||
/// </summary> | ||
/// <typeparam name="State">The type accepted by the callback as input.</typeparam> | ||
/// <typeparam name="TResult">The type of the result returned by the callback.</typeparam> | ||
/// <param name="callback">The callback function to execute.</param> | ||
/// <param name="state">An instance of State to be passed to the callback.</param> | ||
/// <param name="isAsync">Whether this method should run asynchronously.</param> | ||
/// <param name="cancellationToken">Cancels outstanding requests.</param> | ||
/// <returns>Returns the result of the callback or the next rate limiter.</returns> | ||
internal abstract ValueTask<TResult> Execute<State, TResult>( | ||
AsyncFlagFunc<State, ValueTask<TResult>> callback, | ||
State state, | ||
bool isAsync, | ||
CancellationToken cancellationToken = default); | ||
|
||
public abstract void Dispose(); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removing these for now until we fully implement additional auth pathways