Serialize connection creation per key

This commit is contained in:
Namhyeon, Go 2025-10-28 14:12:25 +09:00
parent b8362e570c
commit c7890aaadb

View File

@ -22,6 +22,8 @@ namespace WelsonJS.Launcher
{
private readonly ConcurrentDictionary<string, (TConnection Connection, TParameters Parameters)> _pool
= new ConcurrentDictionary<string, (TConnection, TParameters)>();
private readonly ConcurrentDictionary<string, SemaphoreSlim> _openLocks
= new ConcurrentDictionary<string, SemaphoreSlim>();
/// <summary>
/// Creates a unique cache key for the given connection parameters.
@ -61,12 +63,29 @@ namespace WelsonJS.Launcher
return existing.Connection;
}
var gate = _openLocks.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
await gate.WaitAsync(token).ConfigureAwait(false);
try
{
if (_pool.TryGetValue(key, out existing) && IsConnectionHealthy(existing.Connection))
{
return existing.Connection;
}
if (existing.Connection != null && !IsConnectionHealthy(existing.Connection))
{
RemoveInternal(key, existing.Connection);
}
var connection = await OpenConnectionAsync(parameters, token).ConfigureAwait(false);
_pool[key] = (connection, parameters);
return connection;
}
finally
{
gate.Release();
}
}
/// <summary>
/// Removes the connection associated with the provided parameters.