mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 16:27:23 +01:00
add positive response caching to grpc auth service to lessen load
This commit is contained in:
parent
a554d751b4
commit
5bd9c7d09d
1 changed files with 22 additions and 2 deletions
|
|
@ -1,5 +1,4 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Security.Cryptography;
|
||||
using MareSynchronosShared.Protos;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
|
@ -13,9 +12,16 @@ public class GrpcAuthenticationService : GrpcBaseService
|
|||
public long Id { get; set; }
|
||||
}
|
||||
|
||||
private record AuthResponseCache
|
||||
{
|
||||
public AuthReply Response { get; set; }
|
||||
public DateTime WrittenTo { get; set; }
|
||||
}
|
||||
|
||||
private readonly AuthService.AuthServiceClient _authClient;
|
||||
private readonly ConcurrentQueue<AuthRequestInternal> _requestQueue = new();
|
||||
private readonly ConcurrentDictionary<long, AuthReply> _authReplies = new();
|
||||
private readonly ConcurrentDictionary<string, AuthResponseCache> _cachedPositiveResponses = new(StringComparer.Ordinal);
|
||||
private long _requestId = 0;
|
||||
|
||||
public GrpcAuthenticationService(ILogger<GrpcAuthenticationService> logger, AuthService.AuthServiceClient authClient) : base(logger)
|
||||
|
|
@ -25,7 +31,12 @@ public class GrpcAuthenticationService : GrpcBaseService
|
|||
|
||||
public async Task<AuthReply> AuthorizeAsync(string ip, string secretKey)
|
||||
{
|
||||
using var sha1 = SHA1.Create();
|
||||
if (_cachedPositiveResponses.TryGetValue(secretKey, out var cachedPositiveResponse))
|
||||
{
|
||||
if (cachedPositiveResponse.WrittenTo.AddMinutes(5) < DateTime.UtcNow) return cachedPositiveResponse.Response;
|
||||
_cachedPositiveResponses.Remove(secretKey, out _);
|
||||
}
|
||||
|
||||
var id = Interlocked.Increment(ref _requestId);
|
||||
_requestQueue.Enqueue(new AuthRequestInternal()
|
||||
{
|
||||
|
|
@ -45,6 +56,15 @@ public class GrpcAuthenticationService : GrpcBaseService
|
|||
await Task.Delay(10, cts.Token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (response?.Success ?? false)
|
||||
{
|
||||
_cachedPositiveResponses[secretKey] = new AuthResponseCache
|
||||
{
|
||||
Response = response,
|
||||
WrittenTo = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
return response ?? new AuthReply
|
||||
{
|
||||
Success = false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue