add positive response caching to grpc auth service to lessen load
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using MareSynchronosShared.Protos;
|
using MareSynchronosShared.Protos;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
@@ -13,9 +12,16 @@ public class GrpcAuthenticationService : GrpcBaseService
|
|||||||
public long Id { get; set; }
|
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 AuthService.AuthServiceClient _authClient;
|
||||||
private readonly ConcurrentQueue<AuthRequestInternal> _requestQueue = new();
|
private readonly ConcurrentQueue<AuthRequestInternal> _requestQueue = new();
|
||||||
private readonly ConcurrentDictionary<long, AuthReply> _authReplies = new();
|
private readonly ConcurrentDictionary<long, AuthReply> _authReplies = new();
|
||||||
|
private readonly ConcurrentDictionary<string, AuthResponseCache> _cachedPositiveResponses = new(StringComparer.Ordinal);
|
||||||
private long _requestId = 0;
|
private long _requestId = 0;
|
||||||
|
|
||||||
public GrpcAuthenticationService(ILogger<GrpcAuthenticationService> logger, AuthService.AuthServiceClient authClient) : base(logger)
|
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)
|
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);
|
var id = Interlocked.Increment(ref _requestId);
|
||||||
_requestQueue.Enqueue(new AuthRequestInternal()
|
_requestQueue.Enqueue(new AuthRequestInternal()
|
||||||
{
|
{
|
||||||
@@ -45,6 +56,15 @@ public class GrpcAuthenticationService : GrpcBaseService
|
|||||||
await Task.Delay(10, cts.Token).ConfigureAwait(false);
|
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
|
return response ?? new AuthReply
|
||||||
{
|
{
|
||||||
Success = false,
|
Success = false,
|
||||||
|
|||||||
Reference in New Issue
Block a user