Skip to content

Commit

Permalink
Optimise reset connection.
Browse files Browse the repository at this point in the history
Send the reset connection packet and the follow-up 'SET NAMES' query at once, reducing the number of server round trips.
  • Loading branch information
bgrainger committed Apr 17, 2018
1 parent 715c3b5 commit be19393
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/MySqlConnector/Core/ServerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,31 @@ public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavio
{
m_logArguments[1] = ServerVersion.OriginalString;
Log.Debug("Session{0} ServerVersion={1} supports reset connection; sending reset connection request", m_logArguments);
await SendAsync(ResetConnectionPayload.Instance, ioBehavior, cancellationToken).ConfigureAwait(false);
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
OkPayload.Create(payload);
if (m_payloadHandler is StandardPayloadHandler standardPayloadHandler)
{
// send both packets at once
await standardPayloadHandler.ByteHandler.WriteBytesAsync(s_resetConnectionPackets, ioBehavior).ConfigureAwait(false);

// the "reset connection" packet also resets the connection charset, so we need to change that back to our default
await SendAsync(s_setNamesUtf8mb4Payload, ioBehavior, cancellationToken).ConfigureAwait(false);
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
OkPayload.Create(payload);
// read two OK replies
standardPayloadHandler.SetNextSequenceNumber(1);
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
OkPayload.Create(payload);

standardPayloadHandler.SetNextSequenceNumber(1);
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
OkPayload.Create(payload);
}
else
{
await SendAsync(ResetConnectionPayload.Instance, ioBehavior, cancellationToken).ConfigureAwait(false);
var payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
OkPayload.Create(payload);

// the "reset connection" packet also resets the connection charset, so we need to change that back to our default
await SendAsync(s_setNamesUtf8mb4Payload, ioBehavior, cancellationToken).ConfigureAwait(false);
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
OkPayload.Create(payload);
}
}
else
{
Expand Down Expand Up @@ -1202,6 +1219,11 @@ private enum State
static byte[] s_connectionAttributes;
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(ServerSession));
static readonly PayloadData s_setNamesUtf8mb4Payload = QueryPayload.Create("SET NAMES utf8mb4 COLLATE utf8mb4_bin;");
static readonly ArraySegment<byte> s_resetConnectionPackets = new ArraySegment<byte>(new byte[]
{
1, 0, 0, 0, (byte) CommandKind.ResetConnection,
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;
});

readonly object m_lock;
readonly object[] m_logArguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public void StartNewConversation()
m_sequenceNumber = 0;
}

public void SetNextSequenceNumber(int sequenceNumber) => m_sequenceNumber = sequenceNumber;

public IByteHandler ByteHandler
{
get => m_byteHandler;
Expand Down

0 comments on commit be19393

Please sign in to comment.