Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Servers/HttpSys/src/HttpSysListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
{
_serverSession = new ServerSession();
_requestQueue = new RequestQueue(options.RequestQueueName, options.RequestQueueMode, Logger);
_urlGroup = new UrlGroup(_serverSession, _requestQueue, Logger);
_urlGroup = new UrlGroup(_serverSession, _requestQueue, Logger);

_disconnectListener = new DisconnectListener(_requestQueue, Logger);
if (options.TlsClientHelloBytesCallback is not null)
Expand Down
50 changes: 18 additions & 32 deletions src/Servers/HttpSys/src/RequestProcessing/TlsListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ internal sealed partial class TlsListener : IDisposable
{
private readonly ConcurrentDictionary<ulong, DateTime> _connectionTimestamps = new();
private readonly Action<IFeatureCollection, ReadOnlySpan<byte>> _tlsClientHelloBytesCallback;

private readonly ILogger _logger;
private readonly CancellationTokenSource _cts = new();

private readonly PeriodicTimer _cleanupTimer;
private readonly Task _cleanupTask;

private static readonly TimeSpan ConnectionIdleTime = TimeSpan.FromMinutes(10);
private static readonly TimeSpan CleanupInterval = TimeSpan.FromSeconds(30);
private static readonly TimeSpan ConnectionIdleTime = TimeSpan.FromMinutes(5);

internal TlsListener(ILogger logger, Action<IFeatureCollection, ReadOnlySpan<byte>> tlsClientHelloBytesCallback)
{
_logger = logger;
_tlsClientHelloBytesCallback = tlsClientHelloBytesCallback;

_cleanupTask = Task.Run(() => CleanupLoopAsync(_cts.Token));
_cleanupTimer = new PeriodicTimer(TimeSpan.FromSeconds(30));
_cleanupTask = Task.Run(CleanupLoopAsync);
}

internal void InvokeTlsClientHelloCallback(IFeatureCollection features, Request request)
Expand All @@ -36,7 +36,6 @@ internal void InvokeTlsClientHelloCallback(IFeatureCollection features, Request

if (_connectionTimestamps.ContainsKey(request.RawConnectionId))
{
// update the TTL
_connectionTimestamps[request.RawConnectionId] = DateTime.UtcNow;
return;
}
Expand All @@ -48,11 +47,11 @@ internal void InvokeTlsClientHelloCallback(IFeatureCollection features, Request
}
}

private async Task CleanupLoopAsync(CancellationToken cancellationToken)
private async Task CleanupLoopAsync()
{
while (!cancellationToken.IsCancellationRequested)
try
{
try
while (await _cleanupTimer.WaitForNextTickAsync())
{
var now = DateTime.UtcNow;
foreach (var kvp in _connectionTimestamps)
Expand All @@ -63,33 +62,20 @@ private async Task CleanupLoopAsync(CancellationToken cancellationToken)
}
}
}
catch (Exception ex)
{
Log.CleanupClosedConnectionError(_logger, ex);
}

try
{
await Task.Delay(CleanupInterval, cancellationToken);
}
catch (TaskCanceledException)
{
break;
}
}
}

public void Dispose()
{
_cts.Cancel();
try
catch (OperationCanceledException)
{
_cleanupTask.Wait();
// expected on shutdown
}
catch
catch (Exception ex)
{
// ignore
Log.CleanupClosedConnectionError(_logger, ex);
}
_cts.Dispose();
}

public void Dispose()
{
try { _cleanupTask.Wait(); } catch { }
_cleanupTimer.Dispose();
}
}
Loading