This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Optimize SqlClient SNIPacket async paths #34184
Merged
Merged
Changes from 3 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
117 changes: 117 additions & 0 deletions
117
src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIPacket.NetCoreApp.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,117 @@ | ||
| // 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.Buffers; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Data.SqlClient.SNI | ||
| { | ||
| internal partial class SNIPacket | ||
| { | ||
| /// <summary> | ||
| /// Read data from a stream asynchronously | ||
| /// </summary> | ||
| /// <param name="stream">Stream to read from</param> | ||
| /// <param name="callback">Completion callback</param> | ||
| public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback) | ||
| { | ||
| // Treat local function as a static and pass all params otherwise as async will allocate | ||
| async Task ReadFromStreamAsync(SNIPacket packet, SNIAsyncCallback cb, ValueTask<int> valueTask) | ||
| { | ||
| bool error = false; | ||
| try | ||
| { | ||
| packet._length = await valueTask.ConfigureAwait(false); | ||
| if (packet._length == 0) | ||
| { | ||
| SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, string.Empty); | ||
| error = true; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, ex); | ||
| error = true; | ||
| } | ||
|
|
||
| if (error) | ||
| { | ||
| packet.Release(); | ||
| } | ||
|
|
||
| cb(packet, error ? TdsEnums.SNI_ERROR : TdsEnums.SNI_SUCCESS); | ||
| } | ||
|
|
||
| ValueTask<int> vt = stream.ReadAsync(new Memory<byte>(_data, 0, _capacity), CancellationToken.None); | ||
|
|
||
| if (vt.IsCompletedSuccessfully) | ||
| { | ||
| _length = vt.Result; | ||
| // Zero length to go via async local function as is error condition | ||
| if (_length > 0) | ||
| { | ||
| callback(this, TdsEnums.SNI_SUCCESS); | ||
|
|
||
| // Completed | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Not complete or error call the async local function to complete | ||
| _ = ReadFromStreamAsync(this, callback, vt); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Write data to a stream asynchronously | ||
| /// </summary> | ||
| /// <param name="stream">Stream to write to</param> | ||
| public void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, SNIProviders provider, bool disposeAfterWriteAsync = false) | ||
| { | ||
| // Treat local function as a static and pass all params otherwise as async will allocate | ||
| async Task WriteToStreamAsync(SNIPacket packet, SNIAsyncCallback cb, SNIProviders providers, bool disposeAfter, ValueTask valueTask) | ||
| { | ||
| uint status = TdsEnums.SNI_SUCCESS; | ||
| try | ||
| { | ||
| await valueTask.ConfigureAwait(false); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| SNILoadHandle.SingletonInstance.LastError = new SNIError(providers, SNICommon.InternalExceptionError, e); | ||
| status = TdsEnums.SNI_ERROR; | ||
| } | ||
|
|
||
| cb(packet, status); | ||
|
|
||
| if (disposeAfter) | ||
| { | ||
| packet.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| ValueTask vt = stream.WriteAsync(new Memory<byte>(_data, 0, _length), CancellationToken.None); | ||
|
|
||
| if (vt.IsCompletedSuccessfully) | ||
| { | ||
| // Read the result to register as complete for the ValueTask | ||
| vt.GetAwaiter().GetResult(); | ||
|
|
||
| callback(this, TdsEnums.SNI_SUCCESS); | ||
|
|
||
| if (disposeAfterWriteAsync) | ||
| { | ||
| Dispose(); | ||
| } | ||
|
|
||
| // Completed | ||
| return; | ||
| } | ||
|
|
||
| // Not complete or error call the async local function to complete | ||
| _ = WriteToStreamAsync(this, callback, provider, disposeAfterWriteAsync, vt); | ||
| } | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
117
src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIPacket.NetStandard.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,117 @@ | ||
| // 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.Buffers; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Data.SqlClient.SNI | ||
| { | ||
| internal partial class SNIPacket | ||
| { | ||
| /// <summary> | ||
| /// Read data from a stream asynchronously | ||
| /// </summary> | ||
| /// <param name="stream">Stream to read from</param> | ||
| /// <param name="callback">Completion callback</param> | ||
| public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback) | ||
| { | ||
| // Treat local function as a static and pass all params otherwise as async will allocate | ||
| async Task ReadFromStreamAsync(SNIPacket packet, SNIAsyncCallback cb, Task<int> task) | ||
| { | ||
| bool error = false; | ||
| try | ||
| { | ||
| packet._length = await task.ConfigureAwait(false); | ||
| if (packet._length == 0) | ||
| { | ||
| SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, string.Empty); | ||
| error = true; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, ex); | ||
| error = true; | ||
| } | ||
|
|
||
| if (error) | ||
| { | ||
| packet.Release(); | ||
| } | ||
|
|
||
| cb(packet, error ? TdsEnums.SNI_ERROR : TdsEnums.SNI_SUCCESS); | ||
| } | ||
|
|
||
| Task<int> t = stream.ReadAsync(_data, 0, _capacity, CancellationToken.None); | ||
|
|
||
| if ((t.Status & TaskStatus.RanToCompletion) != 0) | ||
| { | ||
| _length = t.Result; | ||
| // Zero length to go via async local function as is error condition | ||
| if (_length > 0) | ||
| { | ||
| callback(this, TdsEnums.SNI_SUCCESS); | ||
|
|
||
| // Completed | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Not complete or error call the async local function to complete | ||
| _ = ReadFromStreamAsync(this, callback, t); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Write data to a stream asynchronously | ||
| /// </summary> | ||
| /// <param name="stream">Stream to write to</param> | ||
| public void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, SNIProviders provider, bool disposeAfterWriteAsync = false) | ||
| { | ||
| // Treat local function as a static and pass all params otherwise as async will allocate | ||
| async Task WriteToStreamAsync(SNIPacket packet, SNIAsyncCallback cb, SNIProviders providers, bool disposeAfter, Task task) | ||
| { | ||
| uint status = TdsEnums.SNI_SUCCESS; | ||
| try | ||
| { | ||
| await task.ConfigureAwait(false); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| SNILoadHandle.SingletonInstance.LastError = new SNIError(providers, SNICommon.InternalExceptionError, e); | ||
| status = TdsEnums.SNI_ERROR; | ||
| } | ||
|
|
||
| cb(packet, status); | ||
|
|
||
| if (disposeAfter) | ||
| { | ||
| packet.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| Task t = stream.WriteAsync(_data, 0, _length, CancellationToken.None); | ||
|
|
||
| if ((t.Status & TaskStatus.RanToCompletion) != 0) | ||
| { | ||
| // Read the result to register as complete for the Task | ||
| t.GetAwaiter().GetResult(); | ||
|
|
||
| callback(this, TdsEnums.SNI_SUCCESS); | ||
|
|
||
| if (disposeAfterWriteAsync) | ||
| { | ||
| Dispose(); | ||
| } | ||
|
|
||
| // Completed | ||
| return; | ||
| } | ||
|
|
||
| // Not complete or error call the async local function to complete | ||
| _ = WriteToStreamAsync(this, callback, provider, disposeAfterWriteAsync, t); | ||
| } | ||
| } | ||
| } |
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
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.