Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace Orbit.Infrastructure.Persistence;
/// migrations, the design-time factory, and the Hangfire durable queue use
/// <c>ConnectionStrings:SessionConnection</c> (the session pooler), falling back to
/// <c>DefaultConnection</c> when it is unset. The configured <see cref="DatabaseConnectionSettings"/>
/// pool caps override any pool size present in the raw connection string.
/// pool caps override any pool size present in the raw connection string. Idle session-pool connections
/// are pruned after 60 seconds, and open inactive session connections receive keepalive queries after
/// 30 seconds to detect connections closed by the pooler or network.
/// </summary>
public static class OrbitConnectionStringFactory
{
Expand All @@ -28,7 +30,15 @@ public static string ForSession(IConfiguration configuration)
if (string.IsNullOrWhiteSpace(sessionConnectionString))
sessionConnectionString = configuration.GetConnectionString("DefaultConnection");

return ApplyPoolCap(sessionConnectionString, settings.SessionMaxPoolSize);
var cappedConnectionString = ApplyPoolCap(sessionConnectionString, settings.SessionMaxPoolSize);
if (string.IsNullOrEmpty(cappedConnectionString))
return cappedConnectionString;

return new NpgsqlConnectionStringBuilder(cappedConnectionString)
{
ConnectionIdleLifetime = 60,
KeepAlive = 30
}.ConnectionString;
}

private static string ApplyPoolCap(string? connectionString, int maxPoolSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public void ForSession_PrefersSessionConnection_OverDefault()
result.Host.Should().Be("session");
result.Port.Should().Be(5432);
result.MaxPoolSize.Should().Be(5);
result.MinPoolSize.Should().Be(0);
result.ConnectionIdleLifetime.Should().Be(60);
result.KeepAlive.Should().Be(30);
}

[Theory]
Expand Down
Loading