-
Notifications
You must be signed in to change notification settings - Fork 315
Add ValueTask stream overloads on SNI streams and Waits #902
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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
60 changes: 60 additions & 0 deletions
60
...osoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/ConcurrentQueueSemaphore.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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 readonly SemaphoreSlim _semaphore; | ||
private readonly ConcurrentQueue<TaskCompletionSource<bool>> _queue; | ||
|
||
public ConcurrentQueueSemaphore(int initialCount) | ||
{ | ||
_semaphore = new SemaphoreSlim(initialCount); | ||
_queue = new ConcurrentQueue<TaskCompletionSource<bool>>(); | ||
} | ||
|
||
public Task 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 Task.CompletedTask; | ||
} | ||
else | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
_queue.Enqueue(tcs); | ||
_semaphore.WaitAsync().ContinueWith( | ||
continuationAction: static (Task task, object state) => | ||
{ | ||
ConcurrentQueue<TaskCompletionSource<bool>> queue = (ConcurrentQueue<TaskCompletionSource<bool>>)state; | ||
if (queue.TryDequeue(out TaskCompletionSource<bool> popped)) | ||
{ | ||
popped.SetResult(true); | ||
} | ||
}, | ||
state: _queue, | ||
cancellationToken: cancellationToken | ||
); | ||
return tcs.Task; | ||
} | ||
} | ||
|
||
public void Release() | ||
{ | ||
_semaphore.Release(); | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIStreams.Task.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 | ||
{ | ||
// NetCore2.1: | ||
// DO NOT OVERRIDE ValueTask versions of ReadAsync and WriteAsync because the underlying SslStream implements them | ||
// by calling the Task versions which are already overridden meaning that if a caller uses Task WriteAsync this would | ||
// call ValueTask WriteAsync which then called TaskWriteAsync introducing a lock cycle and never return | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIStreams.ValueTask.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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; | ||
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) | ||
{ | ||
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).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) | ||
{ | ||
return WriteAsync(new Memory<byte>(buffer, offset, count), cancellationToken).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) | ||
{ | ||
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).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) | ||
{ | ||
return WriteAsync(new Memory<byte>(buffer, offset, count), cancellationToken).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(); | ||
} | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.