Skip to content
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
merged 14 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,10 @@
<Compile Include="Microsoft\Data\SqlClientX\IO\TdsWriter.cs" />
<Compile Include="Microsoft\Data\SqlClientX\IO\TdsWriteStream.cs" />
<Compile Include="Microsoft\Data\SqlClientX\PoolingDataSource.cs" />
<Compile Include="Microsoft\Data\SqlClientX\RateLimiters\BlockingPeriodRateLimiter.cs" />
<Compile Include="Microsoft\Data\SqlClientX\RateLimiters\ConcurrencyRateLimiter.cs" />
<Compile Include="Microsoft\Data\SqlClientX\RateLimiters\IRateLimiter.cs" />
<Compile Include="Microsoft\Data\SqlClientX\RateLimiters\PassthroughRateLimiter.cs" />
<Compile Include="Microsoft\Data\SqlClientX\SqlConnectionX.cs" />
<Compile Include="Microsoft\Data\SqlClientX\SqlConnector.cs" />
<Compile Include="Microsoft\Data\SqlClientX\SqlDataSource.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading.Tasks;
using Microsoft.Data.ProviderBase;
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClientX.RateLimiters;

namespace Microsoft.Data.SqlClientX
{
Expand All @@ -22,6 +23,7 @@ namespace Microsoft.Data.SqlClientX
internal sealed class PoolingDataSource : SqlDataSource
{
private DbConnectionPoolGroupOptions _connectionPoolGroupOptions;
private IRateLimiter _rateLimiter;

internal int MinPoolSize => _connectionPoolGroupOptions.MinPoolSize;
internal int MaxPoolSize => _connectionPoolGroupOptions.MaxPoolSize;
Expand All @@ -30,13 +32,13 @@ internal sealed class PoolingDataSource : SqlDataSource
/// Initializes a new PoolingDataSource.
/// </summary>
//TODO: support auth contexts and provider info
PoolingDataSource(SqlConnectionStringBuilder connectionStringBuilder,
internal PoolingDataSource(SqlConnectionStringBuilder connectionStringBuilder,
SqlCredential credential,
RemoteCertificateValidationCallback userCertificateValidationCallback,
Action<X509CertificateCollection> clientCertificatesCallback,
DbConnectionPoolGroupOptions options) : base(connectionStringBuilder, credential, userCertificateValidationCallback, clientCertificatesCallback)
DbConnectionPoolGroupOptions options,
IRateLimiter rateLimiter) : base(connectionStringBuilder, credential)
{
_connectionPoolGroupOptions = options;
_rateLimiter = rateLimiter;
//TODO: other construction
}

Expand All @@ -49,7 +51,12 @@ internal override ValueTask<SqlConnector> GetInternalConnection(SqlConnectionX o
/// <inheritdoc/>
internal override ValueTask<SqlConnector> OpenNewInternalConnection(SqlConnectionX owningConnection, TimeSpan timeout, bool async, CancellationToken cancellationToken)
{
throw new NotImplementedException();
return _rateLimiter.Execute<SqlConnector>(() => RateLimitedOpen(owningConnection, timeout, async, cancellationToken), async, cancellationToken);
mdaigle marked this conversation as resolved.
Show resolved Hide resolved

ValueTask<SqlConnector> RateLimitedOpen(SqlConnectionX owningConnection, TimeSpan timeout, bool async, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 class BlockingPeriodRateLimiter : IRateLimiter
mdaigle marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Executes the provided callback in the context of the blocking period rate limit logic.
/// </summary>
/// <typeparam name="TResult">The type of the result returned by the callback.</typeparam>
/// <param name="callback">The callback function to execute.</param>
/// <param name="async">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>
/// <exception cref="NotImplementedException"></exception>
internal override ValueTask<TResult> Execute<TResult>(Func<ValueTask<TResult>> callback, bool async, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 : IRateLimiter
{
private SemaphoreSlim _concurrencyLimitSemaphore;
mdaigle marked this conversation as resolved.
Show resolved Hide resolved

/// <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);
}

/// <summary>
/// Executes the provided callback in the context of the blocking period rate limit logic.
/// </summary>
/// <typeparam name="TResult">The type of the result returned by the callback.</typeparam>
/// <param name="callback">The callback function to execute.</param>
/// <param name="async">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 override async ValueTask<TResult> Execute<TResult>(Func<ValueTask<TResult>> callback, bool async, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

//TODO: in the future, we can enforce order
if (async)
{
await _concurrencyLimitSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
}
else
{
_concurrencyLimitSemaphore.Wait(cancellationToken);
}

cancellationToken.ThrowIfCancellationRequested();
mdaigle marked this conversation as resolved.
Show resolved Hide resolved

TResult result;

try
{
result = await callback().ConfigureAwait(false);
}
finally
{
_concurrencyLimitSemaphore.Release();
}

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 IRateLimiter
mdaigle marked this conversation as resolved.
Show resolved Hide resolved
{
private IRateLimiter _next;

/// <summary>
/// The next rate limiter that should be executed within the context of this rate limiter.
/// </summary>
internal virtual IRateLimiter Next
{
get => _next;
set => _next = value;
}

/// <summary>
/// Should be implemented to execute the provided callback within the context of the rate limit, or pass the responsibility along to the next rate limiter.
/// </summary>
/// <typeparam name="TResult">The type of the result returned by the callback.</typeparam>
/// <param name="callback">The callback function to execute.</param>
/// <param name="async">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<TResult>(Func<ValueTask<TResult>> callback, bool async, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 no-op rate limiter that simply executes the callback or passes through to the next rate limiter.
/// </summary>
internal class PassthroughRateLimiter : IRateLimiter
{
//TODO: no state, add static instance

/// <summary>
/// Executes the provided callback or passes through to the next rate limiter.
/// </summary>
/// <typeparam name="TResult">The type of the result returned by the callback.</typeparam>
/// <param name="callback">The callback function to execute.</param>
/// <param name="async">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 override ValueTask<TResult> Execute<TResult>(Func<ValueTask<TResult>> callback, bool async, CancellationToken cancellationToken = default)
{
if (Next != null)
{
return Next.Execute<TResult>(callback, async, cancellationToken);
}
else
{
return callback();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ internal abstract class SqlDataSource : DbDataSource
{
private readonly SqlConnectionStringBuilder _connectionStringBuilder;

private readonly RemoteCertificateValidationCallback _userCertificateValidationCallback;

private readonly Action<X509CertificateCollection> _clientCertificatesCallback;

internal SqlCredential Credential { get; }

//TODO: return SqlConnection after it is updated to wrap SqlConnectionX
Expand All @@ -40,14 +36,10 @@ protected override SqlConnectionX CreateDbConnection()

internal SqlDataSource(
SqlConnectionStringBuilder connectionStringBuilder,
SqlCredential credential,
RemoteCertificateValidationCallback userCertificateValidationCallback,
Copy link
Contributor Author

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

Action<X509CertificateCollection> clientCertificatesCallback)
SqlCredential credential)
{
_connectionStringBuilder = connectionStringBuilder;
Credential = credential;
_userCertificateValidationCallback = userCertificateValidationCallback;
_clientCertificatesCallback = clientCertificatesCallback;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
#if NET8_0_OR_GREATER

using System;
using System.Diagnostics;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Data.Common;
using Microsoft.Data.ProviderBase;
using Microsoft.Data.SqlClient;
using Microsoft.Data.SqlClientX.RateLimiters;

namespace Microsoft.Data.SqlClientX
{
Expand Down Expand Up @@ -42,11 +46,67 @@ public SqlDataSourceBuilder(string connectionString = null, SqlCredential creden
/// </summary>
public SqlDataSource Build()
{
return new UnpooledDataSource(
ConnectionStringBuilder,
Credential,
UserCertificateValidationCallback,
ClientCertificatesCallback);
if (ConnectionStringBuilder.Pooling)
{
//TODO: pool group layer

DbConnectionPoolGroupOptions poolGroupOptions = new DbConnectionPoolGroupOptions(
ConnectionStringBuilder.IntegratedSecurity,
ConnectionStringBuilder.MinPoolSize,
ConnectionStringBuilder.MaxPoolSize,
//TODO: carry over connect timeout conversion logic from SqlConnectionFactory? if not, don't need an extra allocation for this object, just use connection string builder
ConnectionStringBuilder.ConnectTimeout,
ConnectionStringBuilder.LoadBalanceTimeout,
ConnectionStringBuilder.Enlist);

//TODO: evaluate app context switch for concurrency limit
mdaigle marked this conversation as resolved.
Show resolved Hide resolved
IRateLimiter rateLimiter = IsBlockingPeriodEnabled() ? new BlockingPeriodRateLimiter() : new PassthroughRateLimiter();
mdaigle marked this conversation as resolved.
Show resolved Hide resolved

return new PoolingDataSource(ConnectionStringBuilder,
Credential,
poolGroupOptions,
rateLimiter);
}
else
{
return new UnpooledDataSource(
ConnectionStringBuilder,
Credential);
}
}

private bool IsBlockingPeriodEnabled()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

carried over verbatim from DbConnectionPool

{
var policy = ConnectionStringBuilder.PoolBlockingPeriod;

switch (policy)
{
case PoolBlockingPeriod.Auto:
{
if (ADP.IsAzureSqlServerEndpoint(ConnectionStringBuilder.DataSource))
{
return false; // in Azure it will be Disabled
}
else
{
return true; // in Non Azure, it will be Enabled
}
}
case PoolBlockingPeriod.AlwaysBlock:
{
return true; //Enabled
}
case PoolBlockingPeriod.NeverBlock:
{
return false; //Disabled
}
default:
{
//we should never get into this path.
Debug.Fail("Unknown PoolBlockingPeriod. Please specify explicit results in above switch case statement.");
return true;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,8 @@ internal sealed class UnpooledDataSource : SqlDataSource
/// </summary>
/// <param name="connectionStringBuilder"></param>
/// <param name="credential"></param>
/// <param name="userCertificateValidationCallback"></param>
/// <param name="clientCertificatesCallback"></param>
internal UnpooledDataSource(
SqlConnectionStringBuilder connectionStringBuilder,
SqlCredential credential,
RemoteCertificateValidationCallback userCertificateValidationCallback,
Action<X509CertificateCollection> clientCertificatesCallback) :
base(
connectionStringBuilder,
credential,
userCertificateValidationCallback,
clientCertificatesCallback)
internal UnpooledDataSource(SqlConnectionStringBuilder connectionStringBuilder, SqlCredential credential) :
base(connectionStringBuilder, credential)
{
}

Expand Down