-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ValueTask stream overloads on SNI streams
- Loading branch information
Showing
8 changed files
with
307 additions
and
104 deletions.
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
32 changes: 32 additions & 0 deletions
32
...SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/ConcurrentQueueSemaphore.NetCoreApp.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,32 @@ | ||
// 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.SqlClient.SNI | ||
{ | ||
internal sealed partial class ConcurrentQueueSemaphore | ||
{ | ||
public ValueTask WaitAsync(CancellationToken cancellationToken) | ||
{ | ||
// try sync wait with 0 which will not block to see if we need to do an async wait | ||
if (_semaphore.Wait(0, cancellationToken)) | ||
{ | ||
return new ValueTask(); | ||
} | ||
else | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
_queue.Enqueue(tcs); | ||
_semaphore.WaitAsync().ContinueWith( | ||
continuationAction: s_continuePop, | ||
state: _queue, | ||
cancellationToken: cancellationToken | ||
); | ||
return new ValueTask(tcs.Task); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...qlClient/netcore/src/Microsoft/Data/SqlClient/SNI/ConcurrentQueueSemaphore.NetStandard.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,24 @@ | ||
// 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.SqlClient.SNI | ||
{ | ||
internal sealed partial class ConcurrentQueueSemaphore | ||
{ | ||
public Task WaitAsync(CancellationToken cancellationToken) | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
_queue.Enqueue(tcs); | ||
_semaphore.WaitAsync().ContinueWith( | ||
continuationAction: s_continuePop, | ||
state: _queue, | ||
cancellationToken: cancellationToken | ||
); | ||
return tcs.Task; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...osoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/ConcurrentQueueSemaphore.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,44 @@ | ||
// 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.Collections.Concurrent; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Data.SqlClient.SNI | ||
{ | ||
/// <summary> | ||
/// This class implements a FIFO Queue with SemaphoreSlim for ordered execution of parallel tasks. | ||
/// Currently used in Managed SNI (SNISslStream) to override SslStream's WriteAsync implementation. | ||
/// </summary> | ||
internal sealed partial class ConcurrentQueueSemaphore | ||
{ | ||
private static readonly Action<Task, object> s_continuePop = ContinuePop; | ||
|
||
private readonly SemaphoreSlim _semaphore; | ||
private readonly ConcurrentQueue<TaskCompletionSource<bool>> _queue = | ||
new ConcurrentQueue<TaskCompletionSource<bool>>(); | ||
|
||
public ConcurrentQueueSemaphore(int initialCount) | ||
{ | ||
_semaphore = new SemaphoreSlim(initialCount); | ||
} | ||
|
||
public void Release() | ||
{ | ||
_semaphore.Release(); | ||
} | ||
|
||
private static void ContinuePop(Task task, object state) | ||
{ | ||
ConcurrentQueue<TaskCompletionSource<bool>> queue = (ConcurrentQueue<TaskCompletionSource<bool>>)state; | ||
if (queue.TryDequeue(out TaskCompletionSource<bool> popped)) | ||
{ | ||
popped.SetResult(true); | ||
} | ||
} | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
...icrosoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIStreams.NetCoreApp.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,126 @@ | ||
// 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.Net.Security; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Net.Sockets; | ||
using System; | ||
|
||
namespace Microsoft.Data.SqlClient.SNI | ||
{ | ||
|
||
internal sealed partial class SNISslStream | ||
{ | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
ValueTask<int> valueTask = ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken); | ||
if (valueTask.IsCompletedSuccessfully) | ||
{ | ||
return Task.FromResult(valueTask.Result); | ||
} | ||
else | ||
{ | ||
return valueTask.AsTask(); | ||
} | ||
} | ||
|
||
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
await _readAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
return await base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_readAsyncSemaphore.Release(); | ||
} | ||
} | ||
|
||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
ValueTask valueTask = WriteAsync(new Memory<byte>(buffer, offset, count), cancellationToken); | ||
if (valueTask.IsCompletedSuccessfully) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
else | ||
{ | ||
return valueTask.AsTask(); | ||
} | ||
} | ||
|
||
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
await _writeAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
await base.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_writeAsyncSemaphore.Release(); | ||
} | ||
} | ||
} | ||
|
||
|
||
internal sealed partial class SNINetworkStream | ||
{ | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
ValueTask<int> valueTask = ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken); | ||
if (valueTask.IsCompletedSuccessfully) | ||
{ | ||
return Task.FromResult(valueTask.Result); | ||
} | ||
else | ||
{ | ||
return valueTask.AsTask(); | ||
} | ||
} | ||
|
||
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
await _readAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
return await base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_readAsyncSemaphore.Release(); | ||
} | ||
} | ||
|
||
// Prevent the WriteAsync collisions by running the task in a Semaphore Slim | ||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
ValueTask valueTask = WriteAsync(new Memory<byte>(buffer, offset, count), cancellationToken); | ||
if (valueTask.IsCompletedSuccessfully) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
else | ||
{ | ||
return valueTask.AsTask(); | ||
} | ||
} | ||
|
||
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
await _writeAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
await base.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_writeAsyncSemaphore.Release(); | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...crosoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIStreams.NetStandard.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,72 @@ | ||
// 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.Net.Security; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Net.Sockets; | ||
|
||
namespace Microsoft.Data.SqlClient.SNI | ||
{ | ||
internal sealed partial class SNISslStream | ||
{ | ||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
await _readAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
return await base.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_readAsyncSemaphore.Release(); | ||
} | ||
} | ||
|
||
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
await _writeAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
await base.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_writeAsyncSemaphore.Release(); | ||
} | ||
} | ||
} | ||
|
||
internal sealed partial class SNINetworkStream | ||
{ | ||
|
||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
await _readAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
return await base.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_readAsyncSemaphore.Release(); | ||
} | ||
} | ||
|
||
// Prevent the WriteAsync collisions by running the task in a Semaphore Slim | ||
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
await _writeAsyncSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
try | ||
{ | ||
await base.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
_writeAsyncSemaphore.Release(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.