Skip to content

Commit 91653f8

Browse files
committed
Inline ResetConnectionAsync.
This will reduce the number of compiler-generated async state machines.
1 parent 6e0efd0 commit 91653f8

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -257,42 +257,39 @@ public async Task ConnectAsync(ConnectionSettings cs, IOBehavior ioBehavior, Can
257257
m_payloadHandler = new CompressedPayloadHandler(m_payloadHandler.ByteHandler);
258258
}
259259

260-
private async Task ResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
260+
public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
261261
{
262262
VerifyState(State.Connected);
263-
if (ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0)
264-
{
265-
await SendAsync(ResetConnectionPayload.Create(), ioBehavior, cancellationToken).ConfigureAwait(false);
266-
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
267-
OkPayload.Create(payload);
268263

269-
// the "reset connection" packet also resets the connection charset, so we need to change that back to our default
270-
payload = new PayloadData(new ArraySegment<byte>(PayloadUtilities.CreateEofStringPayload(CommandKind.Query, "SET NAMES utf8mb4 COLLATE utf8mb4_bin;")));
271-
await SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
272-
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
273-
OkPayload.Create(payload);
274-
}
275-
else
264+
try
276265
{
277-
// optimistically hash the password with the challenge from the initial handshake (supported by MariaDB; doesn't appear to be supported by MySQL)
278-
var hashedPassword = AuthenticationUtility.CreateAuthenticationResponse(AuthPluginData, 0, cs.Password);
279-
var payload = ChangeUserPayload.Create(cs.UserID, hashedPassword, cs.Database, m_supportsConnectionAttributes ? s_connectionAttributes : null);
280-
await SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
281-
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
282-
if (payload.HeaderByte == AuthenticationMethodSwitchRequestPayload.Signature)
266+
if (ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0)
283267
{
284-
await SwitchAuthenticationAsync(cs, payload, ioBehavior, cancellationToken).ConfigureAwait(false);
268+
await SendAsync(ResetConnectionPayload.Create(), ioBehavior, cancellationToken).ConfigureAwait(false);
269+
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
270+
OkPayload.Create(payload);
271+
272+
// the "reset connection" packet also resets the connection charset, so we need to change that back to our default
273+
payload = new PayloadData(new ArraySegment<byte>(PayloadUtilities.CreateEofStringPayload(CommandKind.Query, "SET NAMES utf8mb4 COLLATE utf8mb4_bin;")));
274+
await SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
285275
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
276+
OkPayload.Create(payload);
277+
}
278+
else
279+
{
280+
// optimistically hash the password with the challenge from the initial handshake (supported by MariaDB; doesn't appear to be supported by MySQL)
281+
var hashedPassword = AuthenticationUtility.CreateAuthenticationResponse(AuthPluginData, 0, cs.Password);
282+
var payload = ChangeUserPayload.Create(cs.UserID, hashedPassword, cs.Database, m_supportsConnectionAttributes ? s_connectionAttributes : null);
283+
await SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
284+
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
285+
if (payload.HeaderByte == AuthenticationMethodSwitchRequestPayload.Signature)
286+
{
287+
await SwitchAuthenticationAsync(cs, payload, ioBehavior, cancellationToken).ConfigureAwait(false);
288+
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
289+
}
290+
OkPayload.Create(payload);
286291
}
287-
OkPayload.Create(payload);
288-
}
289-
}
290292

291-
public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
292-
{
293-
try
294-
{
295-
await ResetConnectionAsync(cs, ioBehavior, cancellationToken).ConfigureAwait(false);
296293
return true;
297294
}
298295
catch (EndOfStreamException)

0 commit comments

Comments
 (0)