CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
The owner renamed the product and moved the repository to the self-hosted Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control, and asked for Actions and a first automatic release with the server package and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md). - Chronos everywhere: a byte-exact rename of contents and paths, then a per-language review of the forms the old name's ending had dictated. - Gitea instead of GitHub: the update check, licence, issue and support links read ChronosReleasesUrl; the update check takes the highest stable version, offers a pre-release build its final release and reports a repository it cannot see as a failed check. EULA, security policy and Gitea issue forms live at the repository root. - Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release, nightly image and performance gates in the .NET SDK container with git checkout and the system SQLite. The release cross-compiles the Windows programs, checks their icon, version and DPI manifest, packs installers and zips, publishes the Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER). - New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the docker-exec script, which Gitea's service containers do not allow; the throw-away server runs without durability) and pe-check. - Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR, 3 on the shared runner; the weekly performance gates keep theirs). BREAKING CHANGE: programs, settings, data folders, file types, environment variables and the Docker image use the new name; nothing is migrated from Titano, which never had a release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
139 lines
5.2 KiB
C#
139 lines
5.2 KiB
C#
using System.Net.Sockets;
|
|
using Chronos.Core.Diagnostics;
|
|
|
|
namespace Chronos.Data.Postgres;
|
|
|
|
/// <summary>
|
|
/// Connection pool: at most <c>Maximum Pool Size</c> connections, reused most-recent-first; a connection idle
|
|
/// longer than <c>Health Check Interval</c> is pinged with <c>SELECT 1</c> before reuse; broken connections and
|
|
/// those left inside a transaction are discarded; idle ones beyond <c>Minimum Pool Size</c> are closed after
|
|
/// <c>Connection Idle Lifetime</c>.
|
|
/// </summary>
|
|
internal sealed class PgPool : IAsyncDisposable
|
|
{
|
|
private const string LogCategory = "Data.Postgres";
|
|
private static readonly TimeSpan PruneInterval = TimeSpan.FromSeconds(10);
|
|
|
|
private readonly PostgresSettings _settings;
|
|
private readonly SemaphoreSlim _slots;
|
|
private readonly LinkedList<PgConnector> _idle = new();
|
|
private readonly Lock _gate = new();
|
|
private readonly Timer _pruner;
|
|
private bool _disposed;
|
|
|
|
public PgPool(PostgresSettings settings)
|
|
{
|
|
_settings = settings;
|
|
_slots = new SemaphoreSlim(settings.MaxPoolSize, settings.MaxPoolSize);
|
|
var interval = settings.IdleLifetime < PruneInterval ? settings.IdleLifetime : PruneInterval;
|
|
_pruner = new Timer(static state => ((PgPool)state!).Prune(), this, interval, interval);
|
|
}
|
|
|
|
public async Task<PgConnector> RentAsync(CancellationToken cancellationToken)
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
if (!await _slots.WaitAsync(_settings.ConnectTimeout, cancellationToken).ConfigureAwait(false))
|
|
throw new DatabaseConnectionException($"No pooled connection became free within {_settings.ConnectTimeout.TotalSeconds} s (Maximum Pool Size={_settings.MaxPoolSize}).");
|
|
try
|
|
{
|
|
while (TakeIdle() is { } connector)
|
|
{
|
|
bool healthy;
|
|
try
|
|
{
|
|
healthy = !connector.IsBroken
|
|
&& (connector.IdleTime < _settings.HealthCheckInterval || await connector.PingAsync(cancellationToken).ConfigureAwait(false));
|
|
}
|
|
catch
|
|
{
|
|
// The caller gave up during the health check; the connection's state is unknown.
|
|
await connector.DisposeAsync().ConfigureAwait(false);
|
|
throw;
|
|
}
|
|
if (healthy) return connector;
|
|
await connector.DisposeAsync().ConfigureAwait(false);
|
|
}
|
|
return await OpenAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch
|
|
{
|
|
_slots.Release();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>Takes a connection back; a broken or busy one, or one inside a transaction, is closed instead.</summary>
|
|
public void Return(PgConnector connector)
|
|
{
|
|
PgConnector? discard = connector;
|
|
if (!connector.IsBroken && !connector.IsBusy && connector.TransactionStatus == 'I')
|
|
{
|
|
connector.MarkIdle();
|
|
lock (_gate)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
_idle.AddFirst(connector);
|
|
discard = null;
|
|
}
|
|
}
|
|
}
|
|
if (discard is not null) _ = discard.DisposeAsync().AsTask();
|
|
_slots.Release();
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
List<PgConnector> idle;
|
|
lock (_gate)
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
idle = [.. _idle];
|
|
_idle.Clear();
|
|
}
|
|
await _pruner.DisposeAsync().ConfigureAwait(false);
|
|
foreach (var connector in idle) await connector.DisposeAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>Opens a connection, retrying once after a second when the server is starting up or refused it.</summary>
|
|
private async Task<PgConnector> OpenAsync(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
return await PgConnector.OpenAsync(_settings, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex) when (ex is PostgresException { IsConnectionFailure: true } or DatabaseConnectionException { InnerException: SocketException })
|
|
{
|
|
Log.Warning(LogCategory, $"Connecting to {_settings.Host}:{_settings.Port} failed ({ex.Message}); retrying once.");
|
|
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
|
|
return await PgConnector.OpenAsync(_settings, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
private PgConnector? TakeIdle()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (_idle.First is not { } first) return null;
|
|
_idle.RemoveFirst();
|
|
return first.Value;
|
|
}
|
|
}
|
|
|
|
private void Prune()
|
|
{
|
|
List<PgConnector>? expired = null;
|
|
lock (_gate)
|
|
{
|
|
while (_idle.Count > _settings.MinPoolSize && _idle.Last is { } last && last.Value.IdleTime >= _settings.IdleLifetime)
|
|
{
|
|
_idle.RemoveLast();
|
|
(expired ??= []).Add(last.Value);
|
|
}
|
|
}
|
|
if (expired is null) return;
|
|
foreach (var connector in expired) _ = connector.DisposeAsync().AsTask();
|
|
}
|
|
}
|