Skip to content

Commit

Permalink
implement ConnectionLifeTime. fixes mysql-net#212
Browse files Browse the repository at this point in the history
  • Loading branch information
caleblloyd committed Apr 6, 2017
1 parent b1ad9ac commit 024ab90
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
17 changes: 16 additions & 1 deletion src/MySqlConnector/MySqlClient/ConnectionPool.cs
Expand Up @@ -53,11 +53,26 @@ public async Task<MySqlSession> GetSessionAsync(IOBehavior ioBehavior, Cancellat
}
}

private bool SessionIsHealthy(MySqlSession session)
{
if (!session.IsConnected)
return false;
if (session.PoolGeneration != m_generation)
return false;
if (session.DatabaseOverride != null)
return false;
if (m_connectionSettings.ConnectionLifeTime > 0
&& (DateTime.UtcNow - session.CreatedUtc).TotalSeconds >= m_connectionSettings.ConnectionLifeTime)
return false;

return true;
}

public void Return(MySqlSession session)
{
try
{
if (session.IsConnected && session.PoolGeneration == m_generation && session.DatabaseOverride == null)
if (SessionIsHealthy(session))
m_sessions.Enqueue(session);
else
session.DisposeAsync(IOBehavior.Synchronous, CancellationToken.None).ConfigureAwait(false);
Expand Down
2 changes: 2 additions & 0 deletions src/MySqlConnector/Serialization/MySqlSession.cs
Expand Up @@ -23,13 +23,15 @@ public MySqlSession()

public MySqlSession(ConnectionPool pool, int poolGeneration)
{
CreatedUtc = DateTime.UtcNow;
Pool = pool;
PoolGeneration = poolGeneration;
}

public ServerVersion ServerVersion { get; set; }
public int ConnectionId { get; set; }
public byte[] AuthPluginData { get; set; }
public DateTime CreatedUtc { get; }
public ConnectionPool Pool { get; }
public int PoolGeneration { get; }
public string DatabaseOverride { get; set; }
Expand Down
29 changes: 29 additions & 0 deletions tests/SideBySide/ConnectionPool.cs
Expand Up @@ -159,6 +159,35 @@ public async Task WaitTimeout()
}
}

[Theory]
[InlineData(2u, 3u, true)]
[InlineData(180u, 3u, false)]
public async Task ConnectionLifeTime(uint lifeTime, uint delaySeconds, bool shouldTimeout)
{
var csb = AppConfig.CreateConnectionStringBuilder();
csb.Pooling = true;
csb.MinimumPoolSize = 0;
csb.MaximumPoolSize = 1;
csb.ConnectionLifeTime = lifeTime;
int serverThread;

using (var connection = new MySqlConnection(csb.ConnectionString))
{
await connection.OpenAsync();
serverThread = connection.ServerThread;
await Task.Delay(TimeSpan.FromSeconds(delaySeconds));
}

using (var connection = new MySqlConnection(csb.ConnectionString))
{
await connection.OpenAsync();
if (shouldTimeout)
Assert.NotEqual(serverThread, connection.ServerThread);
else
Assert.Equal(serverThread, connection.ServerThread);
}
}

[Fact]
public async Task CharacterSet()
{
Expand Down
20 changes: 7 additions & 13 deletions tests/SideBySide/SideBySide.csproj
@@ -1,25 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Condition=" '$(Configuration)' != 'Baseline' ">
<PropertyGroup Condition=" '$(Configuration)' != 'Baseline' ">
<TargetFrameworks>netcoreapp1.1.1;net462</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Baseline' ">
<PropertyGroup Condition=" '$(Configuration)' == 'Baseline' ">
<TargetFrameworks>net462</TargetFrameworks>
<DefineConstants>BASELINE</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<PropertyGroup>
<VersionPrefix>0.1.0</VersionPrefix>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>SideBySide</AssemblyName>
<PackageId>SideBySide</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<ThreadPoolMinThreads>64</ThreadPoolMinThreads>
</PropertyGroup>

<ItemGroup>
<ItemGroup>
<!--testing packages-->
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-*" />
Expand All @@ -31,18 +29,14 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
</ItemGroup>

<ItemGroup Condition=" '$(Configuration)' != 'Baseline' ">
<ProjectReference Include="..\..\src\MySqlConnector\MySqlConnector.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(Configuration)' == 'Baseline' ">
<PackageReference Include="MySql.Data" Version="6.9.8" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

</Project>
</Project>
6 changes: 0 additions & 6 deletions tests/SideBySide/runtimeconfig.template.json

This file was deleted.

0 comments on commit 024ab90

Please sign in to comment.