Skip to content

Commit 000f586

Browse files
committed
Refactor null-checking code.
Eliminate 'session' local variable that was only used to access the Id, and store that in a local instead. (This mostly reverts changes from eadb98c and restores the code to how it was in 2f0e81c.) Use more pattern matching to check for null and avoid reading fields twice.
1 parent 917b776 commit 000f586

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/MySqlConnector/MySqlConnection.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,14 +746,13 @@ internal ServerSession Session
746746

747747
internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
748748
{
749-
var session = m_session;
750-
if (session is null || State != ConnectionState.Open || !session.TryStartCancel(command))
749+
if (m_session?.Id is not string sessionId || State != ConnectionState.Open || m_session?.TryStartCancel(command) is not true)
751750
{
752751
Log.Trace("Ignoring cancellation for closed connection or invalid CommandId {0}", commandId);
753752
return;
754753
}
755754

756-
Log.Debug("CommandId {0} for Session{1} has been canceled via {2}.", commandId, session.Id, isCancel ? "Cancel()" : "command timeout");
755+
Log.Debug("CommandId {0} for Session{1} has been canceled via {2}.", commandId, sessionId, isCancel ? "Cancel()" : "command timeout");
757756

758757
try
759758
{
@@ -763,8 +762,8 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
763762
AutoEnlist = false,
764763
Pooling = false,
765764
};
766-
if (session.IPAddress is not null)
767-
csb.Server = session.IPAddress.ToString();
765+
if (m_session?.IPAddress is { } ipAddress)
766+
csb.Server = ipAddress.ToString();
768767
var cancellationTimeout = GetConnectionSettings().CancellationTimeout;
769768
csb.ConnectionTimeout = cancellationTimeout < 1 ? 3u : (uint) cancellationTimeout;
770769

@@ -778,13 +777,13 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
778777
{
779778
// ignore a rare race condition where the connection is open at the beginning of the method, but closed by the time
780779
// KILL QUERY is executed: https://github.com/mysql-net/MySqlConnector/issues/1002
781-
Log.Info(ex, "Session{0} ignoring cancellation for closed connection.", session.Id);
780+
Log.Info(ex, "Session{0} ignoring cancellation for closed connection.", sessionId);
782781
m_session?.AbortCancel(command);
783782
}
784783
catch (MySqlException ex)
785784
{
786785
// cancelling the query failed; setting the state back to 'Querying' will allow another call to 'Cancel' to try again
787-
Log.Info(ex, "Session{0} cancelling CommandId {1} failed", session.Id, command.CommandId);
786+
Log.Info(ex, "Session{0} cancelling CommandId {1} failed", sessionId, command.CommandId);
788787
m_session?.AbortCancel(command);
789788
}
790789
}

0 commit comments

Comments
 (0)