Skip to content

Commit 69089c0

Browse files
committed
Use ValueTask. Fixes #1233
1 parent 6a6fe35 commit 69089c0

12 files changed

+31
-59
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ private int GetSessionHealth(ServerSession session)
148148
return 0;
149149
}
150150

151-
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
152151
public async ValueTask ReturnAsync(IOBehavior ioBehavior, ServerSession session)
153-
#else
154-
public async ValueTask<int> ReturnAsync(IOBehavior ioBehavior, ServerSession session)
155-
#endif
156152
{
157153
if (Log.IsTraceEnabled())
158154
Log.Trace("Pool{0} receiving Session{1} back", m_logArguments[0], session.Id);
@@ -182,10 +178,6 @@ public async ValueTask<int> ReturnAsync(IOBehavior ioBehavior, ServerSession ses
182178
{
183179
m_sessionSemaphore.Release();
184180
}
185-
186-
#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
187-
return default;
188-
#endif
189181
}
190182

191183
public async Task ClearAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ public ServerSession(ConnectionPool? pool, int poolGeneration, int id)
6565
public bool ProcAccessDenied { get; set; }
6666
public ICollection<KeyValuePair<string, object?>> ActivityTags => m_activityTags;
6767

68-
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
6968
public ValueTask ReturnToPoolAsync(IOBehavior ioBehavior, MySqlConnection? owningConnection)
70-
#else
71-
public ValueTask<int> ReturnToPoolAsync(IOBehavior ioBehavior, MySqlConnection? owningConnection)
72-
#endif
7369
{
7470
if (Log.IsTraceEnabled())
7571
{
@@ -904,7 +900,7 @@ public async ValueTask<bool> TryPingAsync(bool logInfo, IOBehavior ioBehavior, C
904900
}
905901

906902
// Starts a new conversation with the server by sending the first packet.
907-
public ValueTask<int> SendAsync(PayloadData payload, IOBehavior ioBehavior, CancellationToken cancellationToken)
903+
public ValueTask SendAsync(PayloadData payload, IOBehavior ioBehavior, CancellationToken cancellationToken)
908904
{
909905
m_payloadHandler!.StartNewConversation();
910906
return SendReplyAsync(payload, ioBehavior, cancellationToken);
@@ -968,9 +964,9 @@ private async ValueTask<PayloadData> ReceiveReplyAsyncAwaited(ValueTask<ArraySeg
968964
}
969965

970966
// Continues a conversation with the server by sending a reply to a packet received with 'Receive' or 'ReceiveReply'.
971-
public ValueTask<int> SendReplyAsync(PayloadData payload, IOBehavior ioBehavior, CancellationToken cancellationToken)
967+
public ValueTask SendReplyAsync(PayloadData payload, IOBehavior ioBehavior, CancellationToken cancellationToken)
972968
{
973-
ValueTask<int> task;
969+
ValueTask task;
974970
try
975971
{
976972
VerifyConnected();
@@ -979,7 +975,7 @@ public ValueTask<int> SendReplyAsync(PayloadData payload, IOBehavior ioBehavior,
979975
catch (Exception ex)
980976
{
981977
Log.Debug(ex, "Session{0} failed in SendReplyAsync", m_logArguments);
982-
task = ValueTaskExtensions.FromException<int>(ex);
978+
task = ValueTaskExtensions.FromException(ex);
983979
}
984980

985981
if (task.IsCompletedSuccessfully)
@@ -988,11 +984,11 @@ public ValueTask<int> SendReplyAsync(PayloadData payload, IOBehavior ioBehavior,
988984
return SendReplyAsyncAwaited(task);
989985
}
990986

991-
private async ValueTask<int> SendReplyAsyncAwaited(ValueTask<int> task)
987+
private async ValueTask SendReplyAsyncAwaited(ValueTask task)
992988
{
993989
try
994990
{
995-
return await task.ConfigureAwait(false);
991+
await task.ConfigureAwait(false);
996992
}
997993
catch (Exception ex)
998994
{

src/MySqlConnector/MySqlConnection.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,7 @@ internal async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancella
463463
/// <remarks>This is an optional feature of the MySQL protocol and may not be supported by all servers.
464464
/// It's known to be supported by MySQL Server 5.7.3 (and later) and MariaDB 10.2.4 (and later).
465465
/// Other MySQL-compatible servers or proxies may not support this command.</remarks>
466-
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
467466
public async ValueTask ResetConnectionAsync(CancellationToken cancellationToken = default)
468-
#else
469-
public async Task ResetConnectionAsync(CancellationToken cancellationToken = default)
470-
#endif
471467
{
472468
var session = Session;
473469
Log.Debug("Session{0} resetting connection", session.Id);
@@ -1080,11 +1076,7 @@ private async Task DoCloseAsync(bool changeState, IOBehavior ioBehavior)
10801076
}
10811077
}
10821078

1083-
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
10841079
private async ValueTask CloseDatabaseAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
1085-
#else
1086-
private async Task CloseDatabaseAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
1087-
#endif
10881080
{
10891081
if (m_activeReader is not null)
10901082
await m_activeReader.DisposeAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

src/MySqlConnector/MySqlDataReader.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void ActivateResultSet(CancellationToken cancellationToken)
136136
m_hasWarnings = m_resultSet.WarningCount != 0;
137137
}
138138

139-
private ValueTask<int> ScanResultSetAsync(IOBehavior ioBehavior, ResultSet resultSet, CancellationToken cancellationToken)
139+
private ValueTask ScanResultSetAsync(IOBehavior ioBehavior, ResultSet resultSet, CancellationToken cancellationToken)
140140
{
141141
if (!m_hasMoreResults)
142142
return default;
@@ -150,18 +150,17 @@ private ValueTask<int> ScanResultSetAsync(IOBehavior ioBehavior, ResultSet resul
150150
if (resultSet.BufferState != ResultSetState.HasMoreData)
151151
throw new InvalidOperationException("Invalid state: {0}".FormatInvariant(resultSet.BufferState));
152152

153-
return new ValueTask<int>(ScanResultSetAsyncAwaited(ioBehavior, resultSet, cancellationToken));
153+
return new ValueTask(ScanResultSetAsyncAwaited(ioBehavior, resultSet, cancellationToken));
154154
}
155155

156-
private async Task<int> ScanResultSetAsyncAwaited(IOBehavior ioBehavior, ResultSet resultSet, CancellationToken cancellationToken)
156+
private async Task ScanResultSetAsyncAwaited(IOBehavior ioBehavior, ResultSet resultSet, CancellationToken cancellationToken)
157157
{
158158
using (Command!.CancellableCommand.RegisterCancel(cancellationToken))
159159
{
160160
try
161161
{
162162
await resultSet.ReadResultSetHeaderAsync(ioBehavior).ConfigureAwait(false);
163163
m_hasMoreResults = resultSet.BufferState != ResultSetState.NoMoreData;
164-
return 0;
165164
}
166165
catch (MySqlException ex) when (ex.ErrorCode == MySqlErrorCode.QueryInterrupted)
167166
{

src/MySqlConnector/Protocol/Serialization/CompressedPayloadHandler.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public ValueTask<ArraySegment<byte>> ReadPayloadAsync(ArraySegmentHolder<byte> c
3939
return ProtocolUtility.ReadPayloadAsync(m_bufferedByteReader, compressedByteHandler, static () => -1, cache, protocolErrorBehavior, ioBehavior);
4040
}
4141

42-
public async ValueTask<int> WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior ioBehavior)
42+
public async ValueTask WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior ioBehavior)
4343
{
4444
// break the payload up into (possibly more than one) uncompressed packets
4545
await ProtocolUtility.WritePayloadAsync(m_uncompressedStreamByteHandler!, GetNextUncompressedSequenceNumber, payload, ioBehavior).ConfigureAwait(false);
4646

4747
if (m_uncompressedStream!.Length == 0)
48-
return default;
48+
return;
4949

5050
if (!m_uncompressedStream.TryGetBuffer(out var uncompressedData))
5151
throw new InvalidOperationException("Couldn't get uncompressed stream buffer.");
@@ -54,7 +54,6 @@ public async ValueTask<int> WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBe
5454

5555
// reset the uncompressed stream to accept more data
5656
m_uncompressedStream.SetLength(0);
57-
return default;
5857
}
5958

6059
private async ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior protocolErrorBehavior, IOBehavior ioBehavior)
@@ -189,7 +188,7 @@ private async ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBe
189188

190189
private int GetNextUncompressedSequenceNumber() => m_uncompressedSequenceNumber++;
191190

192-
private async ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressedData, IOBehavior ioBehavior)
191+
private async ValueTask CompressAndWrite(ArraySegment<byte> remainingUncompressedData, IOBehavior ioBehavior)
193192
{
194193
var remainingUncompressedBytes = Math.Min(remainingUncompressedData.Count, ProtocolUtility.MaxPacketSize);
195194

@@ -239,7 +238,7 @@ private async ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncomp
239238

240239
remainingUncompressedData = remainingUncompressedData.Slice(remainingUncompressedBytes);
241240
await m_byteHandler!.WriteBytesAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), ioBehavior).ConfigureAwait(false);
242-
return remainingUncompressedData.Count == 0 ? default :
241+
if (remainingUncompressedData.Count != 0)
243242
await CompressAndWrite(remainingUncompressedData, ioBehavior).ConfigureAwait(false);
244243
}
245244

@@ -265,7 +264,7 @@ public int RemainingTimeout
265264
public ValueTask<int> ReadBytesAsync(Memory<byte> buffer, IOBehavior ioBehavior) =>
266265
m_compressedPayloadHandler.ReadBytesAsync(buffer, m_protocolErrorBehavior, ioBehavior);
267266

268-
public ValueTask<int> WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBehavior) => throw new NotSupportedException();
267+
public ValueTask WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBehavior) => throw new NotSupportedException();
269268

270269
private readonly CompressedPayloadHandler m_compressedPayloadHandler;
271270
private readonly ProtocolErrorBehavior m_protocolErrorBehavior;

src/MySqlConnector/Protocol/Serialization/IByteHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ internal interface IByteHandler : IDisposable
2222
/// </summary>
2323
/// <param name="data">The data to write.</param>
2424
/// <param name="ioBehavior">The <see cref="IOBehavior"/> to use when writing.</param>
25-
/// <returns>A <see cref="ValueTask{Int32}"/>. The value of this object is not defined.</returns>
26-
ValueTask<int> WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBehavior);
25+
ValueTask WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBehavior);
2726
}

src/MySqlConnector/Protocol/Serialization/IPayloadHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ internal interface IPayloadHandler : IDisposable
3838
/// </summary>
3939
/// <param name="payload">The data to write.</param>
4040
/// <param name="ioBehavior">The <see cref="IOBehavior"/> to use when writing.</param>
41-
/// <returns>A <see cref="ValueTask{Int32}"/>. The value of this object is not defined.</returns>
42-
ValueTask<int> WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior ioBehavior);
41+
ValueTask WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior ioBehavior);
4342
}

src/MySqlConnector/Protocol/Serialization/ProtocolUtility.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,23 +525,22 @@ private static bool HasReadPayload(ArraySegmentHolder<byte> previousPayloads, Pa
525525
return false;
526526
}
527527

528-
public static ValueTask<int> WritePayloadAsync(IByteHandler byteHandler, Func<int> getNextSequenceNumber, ReadOnlyMemory<byte> payload, IOBehavior ioBehavior)
528+
public static ValueTask WritePayloadAsync(IByteHandler byteHandler, Func<int> getNextSequenceNumber, ReadOnlyMemory<byte> payload, IOBehavior ioBehavior)
529529
{
530530
return payload.Length <= MaxPacketSize ? WritePacketAsync(byteHandler, getNextSequenceNumber(), payload, ioBehavior) :
531531
WritePayloadAsyncAwaited(byteHandler, getNextSequenceNumber, payload, ioBehavior);
532532

533-
static async ValueTask<int> WritePayloadAsyncAwaited(IByteHandler byteHandler, Func<int> getNextSequenceNumber, ReadOnlyMemory<byte> payload, IOBehavior ioBehavior)
533+
static async ValueTask WritePayloadAsyncAwaited(IByteHandler byteHandler, Func<int> getNextSequenceNumber, ReadOnlyMemory<byte> payload, IOBehavior ioBehavior)
534534
{
535535
for (var bytesSent = 0; bytesSent < payload.Length; bytesSent += MaxPacketSize)
536536
{
537537
var contents = payload.Slice(bytesSent, Math.Min(MaxPacketSize, payload.Length - bytesSent));
538538
await WritePacketAsync(byteHandler, getNextSequenceNumber(), contents, ioBehavior).ConfigureAwait(false);
539539
}
540-
return 0;
541540
}
542541
}
543542

544-
private static ValueTask<int> WritePacketAsync(IByteHandler byteHandler, int sequenceNumber, ReadOnlyMemory<byte> contents, IOBehavior ioBehavior)
543+
private static ValueTask WritePacketAsync(IByteHandler byteHandler, int sequenceNumber, ReadOnlyMemory<byte> contents, IOBehavior ioBehavior)
545544
{
546545
var bufferLength = contents.Length + 4;
547546
var buffer = ArrayPool<byte>.Shared.Rent(bufferLength);
@@ -556,11 +555,10 @@ private static ValueTask<int> WritePacketAsync(IByteHandler byteHandler, int seq
556555
}
557556
return WritePacketAsyncAwaited(task, buffer);
558557

559-
static async ValueTask<int> WritePacketAsyncAwaited(ValueTask<int> task, byte[] buffer)
558+
static async ValueTask WritePacketAsyncAwaited(ValueTask task, byte[] buffer)
560559
{
561560
await task.ConfigureAwait(false);
562561
ArrayPool<byte>.Shared.Return(buffer);
563-
return 0;
564562
}
565563
}
566564

src/MySqlConnector/Protocol/Serialization/SocketByteHandler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private async ValueTask<int> DoReadBytesAsync(Memory<byte> buffer)
108108
return bytesRead;
109109
}
110110

111-
public ValueTask<int> WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBehavior)
111+
public ValueTask WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBehavior)
112112
{
113113
if (ioBehavior == IOBehavior.Asynchronous)
114114
return DoWriteBytesAsync(data);
@@ -120,19 +120,18 @@ public ValueTask<int> WriteBytesAsync(ReadOnlyMemory<byte> data, IOBehavior ioBe
120120
}
121121
catch (Exception ex)
122122
{
123-
return ValueTaskExtensions.FromException<int>(ex);
123+
return ValueTaskExtensions.FromException(ex);
124124
}
125125
}
126126

127-
private async ValueTask<int> DoWriteBytesAsync(ReadOnlyMemory<byte> data)
127+
private async ValueTask DoWriteBytesAsync(ReadOnlyMemory<byte> data)
128128
{
129129
#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
130130
m_socketAwaitable.EventArgs.SetBuffer(MemoryMarshal.AsMemory(data));
131131
await m_socket.SendAsync(m_socketAwaitable);
132132
#else
133133
await m_socket.SendAsync(data, SocketFlags.None).ConfigureAwait(false);
134134
#endif
135-
return 0;
136135
}
137136

138137
private readonly Socket m_socket;

src/MySqlConnector/Protocol/Serialization/StandardPayloadHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public IByteHandler ByteHandler
4040
public ValueTask<ArraySegment<byte>> ReadPayloadAsync(ArraySegmentHolder<byte> cache, ProtocolErrorBehavior protocolErrorBehavior, IOBehavior ioBehavior) =>
4141
ProtocolUtility.ReadPayloadAsync(m_bufferedByteReader!, m_byteHandler!, m_getNextSequenceNumber, cache, protocolErrorBehavior, ioBehavior);
4242

43-
public ValueTask<int> WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior ioBehavior) =>
43+
public ValueTask WritePayloadAsync(ReadOnlyMemory<byte> payload, IOBehavior ioBehavior) =>
4444
ProtocolUtility.WritePayloadAsync(m_byteHandler!, m_getNextSequenceNumber, payload, ioBehavior);
4545

4646
private readonly Func<int> m_getNextSequenceNumber;

0 commit comments

Comments
 (0)