mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-15 06:44:14 +01:00
adjust signalR limit filter to include connection handling
This commit is contained in:
parent
74efb5eb6a
commit
2170eb127e
1 changed files with 8 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using AspNetCoreRateLimit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
|
@ -10,12 +11,14 @@ public class SignalRLimitFilter : IHubFilter
|
|||
{
|
||||
private readonly IRateLimitProcessor _processor;
|
||||
private readonly IHttpContextAccessor accessor;
|
||||
private readonly ILogger<SignalRLimitFilter> logger;
|
||||
|
||||
public SignalRLimitFilter(
|
||||
IOptions<IpRateLimitOptions> options, IProcessingStrategy processing, IIpPolicyStore policyStore, IHttpContextAccessor accessor)
|
||||
IOptions<IpRateLimitOptions> options, IProcessingStrategy processing, IIpPolicyStore policyStore, IHttpContextAccessor accessor, ILogger<SignalRLimitFilter> logger)
|
||||
{
|
||||
_processor = new IpRateLimitProcessor(options?.Value, policyStore, processing);
|
||||
this.accessor = accessor;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async ValueTask<object> InvokeMethodAsync(
|
||||
|
|
@ -32,15 +35,15 @@ public class SignalRLimitFilter : IHubFilter
|
|||
foreach (var rule in await _processor.GetMatchingRulesAsync(client))
|
||||
{
|
||||
var counter = await _processor.ProcessRequestAsync(client, rule);
|
||||
Console.WriteLine("time: {0}, count: {1}", counter.Timestamp, counter.Count);
|
||||
if (counter.Count > rule.Limit)
|
||||
{
|
||||
var retry = counter.Timestamp.RetryAfterFrom(rule);
|
||||
logger.LogWarning($"Method rate limit triggered from {ip}: {invocationContext.HubMethodName}");
|
||||
throw new HubException($"call limit {retry}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Calling hub method '{invocationContext.HubMethodName}'");
|
||||
//Console.WriteLine($"Calling hub method '{invocationContext.HubMethodName}'");
|
||||
return await next(invocationContext);
|
||||
}
|
||||
|
||||
|
|
@ -57,11 +60,11 @@ public class SignalRLimitFilter : IHubFilter
|
|||
foreach (var rule in await _processor.GetMatchingRulesAsync(client))
|
||||
{
|
||||
var counter = await _processor.ProcessRequestAsync(client, rule);
|
||||
Console.WriteLine("time: {0}, count: {1}", counter.Timestamp, counter.Count);
|
||||
if (counter.Count > rule.Limit)
|
||||
{
|
||||
var retry = counter.Timestamp.RetryAfterFrom(rule);
|
||||
throw new HubException($"rate limit {retry}");
|
||||
logger.LogWarning($"Connection rate limit triggered from {ip}");
|
||||
throw new HubException($"Connection rate limit {retry}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue