Skip to content

Commit 0912f24

Browse files
committed
Skip ping when resetting connection.
This improves connection opening speed (#258) and reformats a recent contribution in 79bd53b.
1 parent 0851817 commit 0912f24

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/MySqlConnector/MySqlClient/ConnectionPool.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,47 @@ public async Task<MySqlSession> GetSessionAsync(MySqlConnection connection, IOBe
4040
}
4141
if (session != null)
4242
{
43-
if (session.PoolGeneration != m_generation || !await session.TryPingAsync(ioBehavior, cancellationToken).ConfigureAwait(false))
43+
bool reuseSession;
44+
45+
if (session.PoolGeneration != m_generation)
4446
{
45-
// session is either old or cannot communicate with the server
46-
await session.DisposeAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
47+
reuseSession = false;
4748
}
4849
else
4950
{
50-
bool resetFailed = false;
51-
52-
// session is valid, reset if supported
5351
if (m_connectionSettings.ConnectionReset)
5452
{
5553
try
5654
{
5755
// Depending on the server (Aurora?), this can randomly fail when re-authenticating to the server.
5856
// If it does, we can just pretend it didn't happen and continue with creating a new session below.
5957
await session.ResetConnectionAsync(m_connectionSettings, ioBehavior, cancellationToken).ConfigureAwait(false);
58+
reuseSession = true;
6059
}
61-
catch
60+
catch (Exception)
6261
{
63-
resetFailed = true;
64-
65-
await session.DisposeAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
62+
reuseSession = false;
6663
}
6764
}
68-
69-
if (!resetFailed)
65+
else
7066
{
71-
// pooled session is ready to be used; return it
72-
session.OwningConnection = new WeakReference<MySqlConnection>(connection);
73-
lock (m_leasedSessions)
74-
m_leasedSessions.Add(session.Id, session);
75-
return session;
67+
reuseSession = await session.TryPingAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
7668
}
7769
}
70+
71+
if (!reuseSession)
72+
{
73+
// session is either old or cannot communicate with the server
74+
await session.DisposeAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
75+
}
76+
else
77+
{
78+
// pooled session is ready to be used; return it
79+
session.OwningConnection = new WeakReference<MySqlConnection>(connection);
80+
lock (m_leasedSessions)
81+
m_leasedSessions.Add(session.Id, session);
82+
return session;
83+
}
7884
}
7985

8086
// create a new session

0 commit comments

Comments
 (0)