Skip to content

Commit 14c1889

Browse files
committed
Use Environment.TickCount instead of DateTime.UtcNow.
This should be slightly more efficient because it avoids division when converting ticks to seconds.
1 parent 59c450c commit 14c1889

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private bool SessionIsHealthy(ServerSession session)
130130
if (session.DatabaseOverride != null)
131131
return false;
132132
if (ConnectionSettings.ConnectionLifeTime > 0
133-
&& (DateTime.UtcNow - session.CreatedUtc).TotalSeconds >= ConnectionSettings.ConnectionLifeTime)
133+
&& unchecked((uint) Environment.TickCount) - session.CreatedTicks >= ConnectionSettings.ConnectionLifeTime)
134134
return false;
135135

136136
return true;
@@ -178,7 +178,7 @@ public async Task ReapAsync(IOBehavior ioBehavior, CancellationToken cancellatio
178178
{
179179
Log.Debug("{0} reaping connection pool", m_logArguments);
180180
RecoverLeakedSessions();
181-
await CleanPoolAsync(ioBehavior, session => (DateTime.UtcNow - session.LastReturnedUtc).TotalSeconds >= ConnectionSettings.ConnectionIdleTimeout, true, cancellationToken).ConfigureAwait(false);
181+
await CleanPoolAsync(ioBehavior, session => (unchecked((uint) Environment.TickCount) - session.LastReturnedTicks) / 1000 >= ConnectionSettings.ConnectionIdleTimeout, true, cancellationToken).ConfigureAwait(false);
182182
}
183183

184184
/// <summary>

src/MySqlConnector/Core/ConnectionSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ConnectionSettings(MySqlConnectionStringBuilder csb)
4040

4141
// Connection Pooling Options
4242
Pooling = csb.Pooling;
43-
ConnectionLifeTime = (int)csb.ConnectionLifeTime;
43+
ConnectionLifeTime = Math.Min(csb.ConnectionLifeTime, uint.MaxValue / 1000) * 1000;
4444
ConnectionReset = csb.ConnectionReset;
4545
ConnectionIdleTimeout = (int)csb.ConnectionIdleTimeout;
4646
if (csb.MinimumPoolSize > csb.MaximumPoolSize)
@@ -90,7 +90,7 @@ public ConnectionSettings(MySqlConnectionStringBuilder csb)
9090

9191
// Connection Pooling Options
9292
public bool Pooling { get; }
93-
public int ConnectionLifeTime { get; }
93+
public uint ConnectionLifeTime { get; }
9494
public bool ConnectionReset { get; }
9595
public int ConnectionIdleTimeout { get; }
9696
public int MinimumPoolSize { get; }

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public ServerSession(ConnectionPool pool, int poolGeneration, int id)
3434
m_lock = new object();
3535
m_payloadCache = new ArraySegmentHolder<byte>();
3636
Id = (pool?.Id ?? 0) + "." + id;
37-
CreatedUtc = DateTime.UtcNow;
37+
CreatedTicks = unchecked((uint) Environment.TickCount);
3838
Pool = pool;
3939
PoolGeneration = poolGeneration;
4040
HostName = "";
@@ -46,10 +46,10 @@ public ServerSession(ConnectionPool pool, int poolGeneration, int id)
4646
public ServerVersion ServerVersion { get; set; }
4747
public int ConnectionId { get; set; }
4848
public byte[] AuthPluginData { get; set; }
49-
public DateTime CreatedUtc { get; }
49+
public uint CreatedTicks { get; }
5050
public ConnectionPool Pool { get; }
5151
public int PoolGeneration { get; }
52-
public DateTime LastReturnedUtc { get; private set; }
52+
public uint LastReturnedTicks { get; private set; }
5353
public string DatabaseOverride { get; set; }
5454
public string HostName { get; private set; }
5555
public IPAddress IPAddress => (m_tcpClient?.Client.RemoteEndPoint as IPEndPoint)?.Address;
@@ -63,7 +63,7 @@ public void ReturnToPool()
6363
m_logArguments[1] = Pool?.Id;
6464
Log.Debug("{0} returning to Pool{1}", m_logArguments);
6565
}
66-
LastReturnedUtc = DateTime.UtcNow;
66+
LastReturnedTicks = unchecked((uint) Environment.TickCount);
6767
Pool?.Return(this);
6868
}
6969

0 commit comments

Comments
 (0)