8000 Optimise reset connection. · mysql-net/MySqlConnector@be19393 · GitHub
[go: up one dir, main page]

Skip to content

Commit be19393

Browse files
committed
Optimise reset connection.
Send the reset connection packet and the follow-up 'SET NAMES' query at once, reducing the number of server round trips.
1 parent 715c3b5 commit be19393

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,31 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
362362
{
363363
m_logArguments[1] = ServerVersion.OriginalString;
364364
Log.Debug("Session{0} ServerVersion={1} supports reset connection; sending reset connection request", m_logArguments);
365-
await SendAsync(ResetConnectionPayload.Instance, ioBehavior, cancellationToken).ConfigureAwait(false);
366-
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
367-
OkPayload.Create(payload);
365+
if (m_payloadHandler is StandardPayloadHandler standardPayloadHandler)
366+
{
367+
// send both packets at once
368+
await standardPayloadHandler.ByteHandler.WriteBytesAsync(s_resetConnectionPackets, ioBehavior).ConfigureAwait(false);
368369

369-
// the "reset connection" packet also resets the connection charset, so we need to change that back to our default
370-
await SendAsync(s_setNamesUtf8mb4Payload, ioBehavior, cancellationToken).ConfigureAwait(false);
371-
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
372-
OkPayload.Create(payload);
370+
// read two OK replies
371+
standardPayloadHandler.SetNextSequenceNumber(1);
372+
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
373+
OkPayload.Create(payload);
374+
375+
standardPayloadHandler.SetNextSequenceNumber(1);
376+
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
377+
OkPayload.Create(payload);
378+
}
379+
else
380+
{
381+
await SendAsync(ResetConnectionPayload.Instance, ioBehavior, cancellationToken).ConfigureAwait(false);
382+
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
383+
OkPayload.Create(payload);
384+
385+
// the "reset connection" packet also resets the connection charset, so we need to change that back to our default
386+
await SendAsync(s_setNamesUtf8mb4Payload, ioBehavior, cancellationToken).ConfigureAwait(false);
387+
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
388+
OkPayload.Create(payload);
389+
}
373390
}
374391
else
375392
{
@@ -1202,6 +1219,11 @@ private enum State
12021219
static byte[] s_connectionAttributes;
12031220
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(ServerSession));
12041221
static readonly PayloadData s_setNamesUtf8mb4Payload = QueryPayload.Create("SET NAMES utf8mb4 COLLATE utf8mb4_bin;");
1222+
static readonly ArraySegment<byte> s_resetConnectionPackets = new ArraySegment<byte>(new byte[]
1223+
{
1224+
1, 0, 0, 0, (byte) CommandKind.ResetConnection,
1225+
39, 0, 0, 0, (byte) CommandKind.Query, 83, 69, 84, 32, 78, 65, 77, 69, 83, 32, 117, 116, 102, 56, 109, 98, 52, 32, 67, 79, 76, 76, 65, 84, 69, 32, 117, 116, 102, 56, 109, 98, 52, 95, 98, 105, 110, 59 // SET NAMES utf8mb4 COLLATE utf8mb4_bin;
1226+
});
12051227

12061228
readonly object m_lock;
12071229
readonly object[] m_logArguments;

src/MySqlConnector/Protocol/Serialization/StandardPayloadHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public void StartNewConversation()
2323
m_sequenceNumber = 0;
2424
}
2525

26+
public void SetNextSequenceNumber(int sequenceNumber) => m_sequenceNumber = sequenceNumber;
27+
2628
public IByteHandler ByteHandler
2729
{
2830
get => m_byteHandler;

0 commit comments

Comments
 (0)
0