mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 22:17:22 +01:00
try throttling incoming connections
This commit is contained in:
parent
1877bf9243
commit
68200d6d7d
1 changed files with 14 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MareSynchronosServer.Throttling;
|
||||
|
|
@ -14,6 +15,7 @@ public class SignalRLimitFilter : IHubFilter
|
|||
private readonly IRateLimitProcessor _processor;
|
||||
private readonly IHttpContextAccessor accessor;
|
||||
private readonly ILogger<SignalRLimitFilter> logger;
|
||||
private static SemaphoreSlim ConnectionLimiterSemaphore = new SemaphoreSlim(20);
|
||||
|
||||
public SignalRLimitFilter(
|
||||
IOptions<IpRateLimitOptions> options, IProcessingStrategy processing, IIpPolicyStore policyStore, IHttpContextAccessor accessor, ILogger<SignalRLimitFilter> logger)
|
||||
|
|
@ -52,6 +54,7 @@ public class SignalRLimitFilter : IHubFilter
|
|||
// Optional method
|
||||
public async Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next)
|
||||
{
|
||||
await ConnectionLimiterSemaphore.WaitAsync();
|
||||
var ip = accessor.GetIpAddress();
|
||||
var client = new ClientRequestIdentity
|
||||
{
|
||||
|
|
@ -66,11 +69,21 @@ public class SignalRLimitFilter : IHubFilter
|
|||
{
|
||||
var retry = counter.Timestamp.RetryAfterFrom(rule);
|
||||
logger.LogWarning($"Connection rate limit triggered from {ip}");
|
||||
ConnectionLimiterSemaphore.Release();
|
||||
throw new HubException($"Connection rate limit {retry}");
|
||||
}
|
||||
}
|
||||
|
||||
await next(context);
|
||||
try
|
||||
{
|
||||
await Task.Delay(100);
|
||||
await next(context);
|
||||
}
|
||||
catch { }
|
||||
finally
|
||||
{
|
||||
ConnectionLimiterSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
// Optional method
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue