Skip to content

Commit 308d1d2

Browse files
committed
implement ConnectionLifeTime. fixes #212
1 parent b1ad9ac commit 308d1d2

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

src/MySqlConnector/MySqlClient/ConnectionPool.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,26 @@ public async Task<MySqlSession> GetSessionAsync(IOBehavior ioBehavior, Cancellat
5353
}
5454
}
5555

56+
private bool SessionIsHealthy(MySqlSession session)
57+
{
58+
if (!session.IsConnected)
59+
return false;
60+
if (session.PoolGeneration != m_generation)
61+
return false;
62+
if (session.DatabaseOverride != null)
63+
return false;
64+
if (m_connectionSettings.ConnectionLifeTime > 0
65+
&& (DateTime.UtcNow - session.CreatedUtc).TotalSeconds >= m_connectionSettings.ConnectionLifeTime)
66+
return false;
67+
68+
return true;
69+
}
70+
5671
public void Return(MySqlSession session)
5772
{
5873
try
5974
{
60-
if (session.IsConnected && session.PoolGeneration == m_generation && session.DatabaseOverride == null)
75+
if (SessionIsHealthy(session))
6176
m_sessions.Enqueue(session);
6277
else
6378
session.DisposeAsync(IOBehavior.Synchronous, CancellationToken.None).ConfigureAwait(false);

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ public MySqlSession()
2323

2424
public MySqlSession(ConnectionPool pool, int poolGeneration)
2525
{
26+
CreatedUtc = DateTime.UtcNow;
2627
Pool = pool;
2728
PoolGeneration = poolGeneration;
2829
}
2930

3031
public ServerVersion ServerVersion { get; set; }
3132
public int ConnectionId { get; set; }
3233
public byte[] AuthPluginData { get; set; }
34+
public DateTime CreatedUtc { get; }
3335
public ConnectionPool Pool { get; }
3436
public int PoolGeneration { get; }
3537
public string DatabaseOverride { get; set; }

tests/SideBySide/ConnectionPool.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ public async Task WaitTimeout()
159159
}
160160
}
161161

162+
[Theory]
163+
[InlineData(2u, 3u, true)]
164+
[InlineData(180u, 3u, false)]
165+
public async Task ConnectionLifeTime(uint lifeTime, uint delaySeconds, bool shouldTimeout)
166+
{
167+
var csb = AppConfig.CreateConnectionStringBuilder();
168+
csb.Pooling = true;
169+
csb.MinimumPoolSize = 0;
170+
csb.MaximumPoolSize = 1;
171+
csb.ConnectionLifeTime = lifeTime;
172+
int serverThread;
173+
174+
using (var connection = new MySqlConnection(csb.ConnectionString))
175+
{
176+
await connection.OpenAsync();
177+
serverThread = connection.ServerThread;
178+
await Task.Delay(TimeSpan.FromSeconds(delaySeconds));
179+
}
180+
181+
using (var connection = new MySqlConnection(csb.ConnectionString))
182+
{
183+
await connection.OpenAsync();
184+
if (shouldTimeout)
185+
Assert.NotEqual(serverThread, connection.ServerThread);
186+
else
187+
Assert.Equal(serverThread, connection.ServerThread);
188+
}
189+
}
190+
162191
[Fact]
163192
public async Task CharacterSet()
164193
{

tests/SideBySide/SideBySide.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
<PreserveCompilationContext>true</PreserveCompilationContext>
1616
<AssemblyName>SideBySide</AssemblyName>
1717
<PackageId>SideBySide</PackageId>
18-
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
1918
<ServerGarbageCollection>true</ServerGarbageCollection>
19+
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
20+
<ThreadPoolMinThreads>64</ThreadPoolMinThreads>
2021
</PropertyGroup>
2122

2223
<ItemGroup>

tests/SideBySide/runtimeconfig.template.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)