Skip to content

Commit 8bf8265

Browse files
Improve Redis Lock management (#75)
1 parent 4fae95e commit 8bf8265

54 files changed

Lines changed: 1350 additions & 422 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ByteSync.Client/Services/Communications/DigitalSignaturesChecker.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.Threading.Tasks;
2-
using ByteSync.Common.Business.Sessions.Cloud.Connections;
1+
using ByteSync.Common.Business.Sessions.Cloud.Connections;
32
using ByteSync.Common.Business.Trust.Connections;
43
using ByteSync.Interfaces.Controls.Applications;
54
using ByteSync.Interfaces.Controls.Communications;
65
using ByteSync.Interfaces.Controls.Communications.Http;
7-
using Serilog;
86

97
namespace ByteSync.Services.Communications;
108

@@ -15,28 +13,27 @@ public class DigitalSignaturesChecker : IDigitalSignaturesChecker
1513
private readonly IDigitalSignaturesRepository _digitalSignaturesRepository;
1614
private readonly ITrustApiClient _trustApiClient;
1715
private readonly IDigitalSignatureComputer _digitalSignatureComputer;
16+
private readonly ILogger<DigitalSignaturesChecker> _logger;
1817

19-
public DigitalSignaturesChecker(IEnvironmentService environmentService,
20-
IPublicKeysManager publicKeysManager, IDigitalSignaturesRepository digitalSignaturesRepository,
21-
ITrustApiClient trustApiClient, IDigitalSignatureComputer digitalSignatureComputer)
18+
public DigitalSignaturesChecker(IEnvironmentService environmentService, IPublicKeysManager publicKeysManager,
19+
IDigitalSignaturesRepository digitalSignaturesRepository, ITrustApiClient trustApiClient, IDigitalSignatureComputer digitalSignatureComputer,
20+
ILogger<DigitalSignaturesChecker> logger)
2221
{
2322
_environmentService = environmentService;
2423
_publicKeysManager = publicKeysManager;
2524
_digitalSignaturesRepository = digitalSignaturesRepository;
2625
_trustApiClient = trustApiClient;
2726
_digitalSignatureComputer = digitalSignatureComputer;
27+
_logger = logger;
2828
}
2929

3030
public async Task<bool> CheckExistingMembersDigitalSignatures(string dataId, ICollection<string> clientInstanceIds)
3131
{
32-
// Tout le monde est trusté, on fait un Check Auth
3332
var signatureCheckInfos = new List<DigitalSignatureCheckInfo>();
34-
35-
// On enlève, au cas où, le clientInstanceId du client actuel
33+
3634
clientInstanceIds.Remove(_environmentService.ClientInstanceId);
3735
foreach (var memberInstanceId in clientInstanceIds)
3836
{
39-
// On calcule la signature d'authentification
4037
var digitalSignatureCheckInfo = _digitalSignatureComputer
4138
.BuildDigitalSignatureCheckInfo(dataId, memberInstanceId, true);
4239

@@ -75,7 +72,7 @@ public async Task CheckDigitalSignature(DigitalSignatureCheckInfo digitalSignatu
7572

7673
if (isDataOK)
7774
{
78-
Log.Information("Digital Signature successfully checked for Client {ClientInstanceId} with Public Key {@PublicKeyInfo}",
75+
_logger.LogInformation("Digital Signature successfully checked for Client {ClientInstanceId} with Public Key {@PublicKeyInfo}",
7976
digitalSignatureCheckInfo.Issuer, otherPartyPublicKeyInfo);
8077

8178
await _digitalSignaturesRepository.SetDigitalSignatureChecked(digitalSignatureCheckInfo.DataId, digitalSignatureCheckInfo.Issuer);
@@ -108,17 +105,7 @@ public async Task CheckDigitalSignature(DigitalSignatureCheckInfo digitalSignatu
108105
}
109106
else
110107
{
111-
Log.Warning("Digital Signature check failed for Client {ClientInstanceId}", digitalSignatureCheckInfo.Issuer);
108+
_logger.LogWarning("Digital Signature check failed for Client {ClientInstanceId}", digitalSignatureCheckInfo.Issuer);
112109
}
113110
}
114-
115-
// private void LogUnknownSessionReceived(string? sessionId, [CallerMemberName] string caller = "")
116-
// {
117-
// if (caller.IsNullOrEmpty())
118-
// {
119-
// caller = "UnknownCaller";
120-
// }
121-
//
122-
// Log.Error("DigitalSignaturesChecker.{caller}: unknown sessionId received ({sessionId})", caller, sessionId);
123-
// }
124111
}

src/ByteSync.Client/Services/Sessions/Connecting/Joining/YouJoinedSessionService.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
using ByteSync.Interfaces.Controls.Communications.Http;
88
using ByteSync.Interfaces.Controls.Encryptions;
99
using ByteSync.Interfaces.Repositories;
10-
using ByteSync.Interfaces.Services.Sessions;
1110
using ByteSync.Interfaces.Services.Sessions.Connecting;
1211
using ByteSync.Interfaces.Services.Sessions.Connecting.Joining;
13-
using Serilog;
1412

15-
namespace ByteSync.Services.Sessions.Connecting;
13+
namespace ByteSync.Services.Sessions.Connecting.Joining;
1614

1715
public class YouJoinedSessionService : IYouJoinedSessionService
1816
{
@@ -23,17 +21,17 @@ public class YouJoinedSessionService : IYouJoinedSessionService
2321
private readonly IDataEncrypter _dataEncrypter;
2422
private readonly ICloudSessionApiClient _cloudSessionApiClient;
2523
private readonly IPublicKeysManager _publicKeysManager;
26-
private readonly ISessionService _sessionService;
2724
private readonly IAfterJoinSessionService _afterJoinSessionService;
25+
private readonly ICloudSessionConnectionService _cloudSessionConnectionService;
2826
private readonly ILogger<YouJoinedSessionService> _logger;
2927

3028
private const string UNKNOWN_RECEIVED_SESSION_ID = "unknown received sessionId {sessionId}";
3129
private const string PUBLIC_KEY_IS_NOT_TRUSTED = "Public key is not trusted";
3230

3331
public YouJoinedSessionService(ICloudSessionConnectionRepository cloudSessionConnectionRepository,
3432
IEnvironmentService environmentService, IPublicKeysTruster publicKeysTruster, IDigitalSignaturesChecker digitalSignaturesChecker,
35-
IDataEncrypter dataEncrypter, ICloudSessionApiClient cloudSessionApiClient, IPublicKeysManager publicKeysManager, ISessionService sessionService,
36-
IAfterJoinSessionService afterJoinSessionService, ILogger<YouJoinedSessionService> logger)
33+
IDataEncrypter dataEncrypter, ICloudSessionApiClient cloudSessionApiClient, IPublicKeysManager publicKeysManager,
34+
IAfterJoinSessionService afterJoinSessionService, ICloudSessionConnectionService cloudSessionConnectionService, ILogger<YouJoinedSessionService> logger)
3735
{
3836
_cloudSessionConnectionRepository = cloudSessionConnectionRepository;
3937
_environmentService = environmentService;
@@ -42,8 +40,8 @@ public YouJoinedSessionService(ICloudSessionConnectionRepository cloudSessionCon
4240
_dataEncrypter = dataEncrypter;
4341
_cloudSessionApiClient = cloudSessionApiClient;
4442
_publicKeysManager = publicKeysManager;
45-
_sessionService = sessionService;
4643
_afterJoinSessionService = afterJoinSessionService;
44+
_cloudSessionConnectionService = cloudSessionConnectionService;
4745
_logger = logger;
4846
}
4947

@@ -138,24 +136,26 @@ public async Task Process(CloudSessionResult cloudSessionResult, ValidateJoinClo
138136
var lobbySessionDetails = await _cloudSessionConnectionRepository
139137
.GetTempLobbySessionDetails(cloudSessionResult.CloudSession.SessionId);
140138

141-
await _afterJoinSessionService.Process(
142-
new AfterJoinSessionRequest(cloudSessionResult, lobbySessionDetails, false));
143-
139+
var afterJoinSessionRequest = new AfterJoinSessionRequest(cloudSessionResult, lobbySessionDetails, false);
140+
await _afterJoinSessionService.Process(afterJoinSessionRequest);
144141

145142
await _cloudSessionConnectionRepository.SetJoinSessionResultReceived(cloudSessionResult.CloudSession.SessionId);
146143

147144
_cloudSessionConnectionRepository.SetConnectionStatus(SessionConnectionStatus.InSession);
148-
149-
// ReSharper disable once PossibleNullReferenceException
150-
Log.Information("JoinSession: {CloudSession}", cloudSessionResult.SessionId);
145+
146+
_logger.LogInformation("JoinSession: {CloudSession}", cloudSessionResult.SessionId);
151147
}
152148
catch (Exception ex)
153149
{
154-
Log.Error(ex, "OnYouJoinedSession");
150+
_logger.LogError(ex, "OnYouJoinedSession");
155151

156-
_sessionService.ClearCloudSession();
152+
var joinSessionError = new JoinSessionError
153+
{
154+
Exception = ex,
155+
Status = JoinSessionStatus.UnexpectedError
156+
};
157157

158-
_cloudSessionConnectionRepository.SetConnectionStatus(SessionConnectionStatus.NoSession);
158+
await _cloudSessionConnectionService.OnJoinSessionError(joinSessionError);
159159
}
160160
}
161161
}

src/ByteSync.Functions/Helpers/Loaders/DependencyInjectionLoader.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ private static void RegisterServerCommonAssembly(ContainerBuilder builder)
2525
.Where(t => t.Name.EndsWith("Repository"))
2626
.InstancePerLifetimeScope()
2727
.AsImplementedInterfaces();
28+
29+
var genericRepositoryTypes = executingAssembly.GetTypes()
30+
.Where(t => t.Name.Contains("Repository`") && t.IsGenericTypeDefinition && !t.IsInterface);
31+
32+
foreach (var genericType in genericRepositoryTypes)
33+
{
34+
builder.RegisterGeneric(genericType)
35+
.AsImplementedInterfaces()
36+
.AsSelf()
37+
.InstancePerLifetimeScope();
38+
}
2839

2940
builder.RegisterAssemblyTypes(executingAssembly)
3041
.Where(t => t.Name.EndsWith("Service"))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using ByteSync.ServerCommon.Entities;
2+
3+
namespace ByteSync.ServerCommon.Business.Repositories;
4+
5+
public class CacheKey
6+
{
7+
public required EntityType EntityType { get; init; }
8+
9+
public required string EntityId { get; init; }
10+
11+
public required string Value { get; init; }
12+
}

src/ByteSync.ServerCommon/Commands/CloudSessions/CreateSessionCommandHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public class CreateSessionCommandHandler : IRequestHandler<CreateSessionRequest,
1414
private readonly ICloudSessionsRepository _cloudSessionsRepository;
1515
private readonly IClientsGroupsService _clientsGroupsService;
1616
private readonly ICloudSessionsService _cloudSessionsService;
17-
private readonly ICacheService _cacheService;
17+
private readonly IRedisInfrastructureService _redisInfrastructureService;
1818
private readonly ILogger<CreateSessionCommandHandler> _logger;
1919

2020
public CreateSessionCommandHandler(ICloudSessionsRepository cloudSessionsRepository, IClientsGroupsService clientsGroupsService,
21-
IClientsRepository clientsRepository, ICloudSessionsService cloudSessionsService, ICacheService cacheService,
21+
IClientsRepository clientsRepository, ICloudSessionsService cloudSessionsService, IRedisInfrastructureService redisInfrastructureService,
2222
ILogger<CreateSessionCommandHandler> logger)
2323
{
2424
_cloudSessionsRepository = cloudSessionsRepository;
2525
_clientsGroupsService = clientsGroupsService;
2626
_cloudSessionsService = cloudSessionsService;
27-
_cacheService = cacheService;
27+
_redisInfrastructureService = redisInfrastructureService;
2828
_logger = logger;
2929
}
3030

@@ -33,7 +33,7 @@ public async Task<CloudSessionResult> Handle(CreateSessionRequest request, Cance
3333
var createCloudSessionParameters = request.CreateCloudSessionParameters;
3434
var client = request.Client;
3535

36-
var transaction = _cacheService.OpenTransaction();
36+
var transaction = _redisInfrastructureService.OpenTransaction();
3737

3838
CloudSessionData cloudSessionData;
3939
SessionMemberData creatorData;

src/ByteSync.ServerCommon/Commands/CloudSessions/FinalizeJoinCloudSessionCommandHandler.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ByteSync.Common.Business.Sessions.Cloud.Connections;
22
using ByteSync.Common.Helpers;
33
using ByteSync.ServerCommon.Business.Sessions;
4+
using ByteSync.ServerCommon.Entities;
45
using ByteSync.ServerCommon.Helpers;
56
using ByteSync.ServerCommon.Interfaces.Mappers;
67
using ByteSync.ServerCommon.Interfaces.Repositories;
@@ -17,18 +18,18 @@ public class FinalizeJoinCloudSessionCommandHandler : IRequestHandler<FinalizeJo
1718
private readonly ISessionMemberMapper _sessionMemberMapper;
1819
private readonly IInvokeClientsService _invokeClientsService;
1920
private readonly IClientsGroupsService _clientsGroupsService;
20-
private readonly ICacheService _cacheService;
21+
private readonly IRedisInfrastructureService _redisInfrastructureService;
2122
private readonly ILogger<FinalizeJoinCloudSessionCommandHandler> _logger;
2223

2324
public FinalizeJoinCloudSessionCommandHandler(ICloudSessionsRepository cloudSessionsRepository, ISessionMemberMapper sessionMemberMapper,
2425
IInvokeClientsService invokeClientsService, IClientsGroupsService clientsGroupsService,
25-
ICacheService cacheService, ILogger<FinalizeJoinCloudSessionCommandHandler> logger)
26+
IRedisInfrastructureService redisInfrastructureService, ILogger<FinalizeJoinCloudSessionCommandHandler> logger)
2627
{
2728
_cloudSessionsRepository = cloudSessionsRepository;
2829
_invokeClientsService = invokeClientsService;
2930
_clientsGroupsService = clientsGroupsService;
3031
_sessionMemberMapper = sessionMemberMapper;
31-
_cacheService = cacheService;
32+
_redisInfrastructureService = redisInfrastructureService;
3233
_logger = logger;
3334
}
3435

@@ -40,8 +41,8 @@ public async Task<FinalizeJoinSessionResult> Handle(FinalizeJoinCloudSessionRequ
4041
FinalizeJoinSessionStatuses? finalizeJoinSessionStatus = null;
4142
SessionMemberData? joiner = null;
4243

43-
var transaction = _cacheService.OpenTransaction();
44-
44+
var transaction = _redisInfrastructureService.OpenTransaction();
45+
4546
var updateResult = await _cloudSessionsRepository.Update(parameters.SessionId, innerCloudSessionData =>
4647
{
4748
if (innerCloudSessionData.IsSessionRemoved)

src/ByteSync.ServerCommon/Commands/CloudSessions/QuitSessionCommandHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public class QuitSessionCommandHandler : IRequestHandler<QuitSessionRequest>
1414
private readonly ICloudSessionsRepository _cloudSessionsRepository;
1515
private readonly IInventoryRepository _inventoryRepository;
1616
private readonly ISynchronizationRepository _synchronizationRepository;
17-
private readonly ICacheService _cacheService;
17+
private readonly IRedisInfrastructureService _redisInfrastructureService;
1818
private readonly ISessionMemberMapper _sessionMemberMapper;
1919
private readonly IClientsGroupsService _clientsGroupsService;
2020
private readonly IInvokeClientsService _invokeClientsService;
2121

2222
public QuitSessionCommandHandler(ICloudSessionsRepository cloudSessionsRepository, IInventoryRepository inventoryRepository,
23-
ISynchronizationRepository synchronizationRepository, ICacheService cacheService, ISessionMemberMapper sessionMemberMapper,
23+
ISynchronizationRepository synchronizationRepository, IRedisInfrastructureService redisInfrastructureService, ISessionMemberMapper sessionMemberMapper,
2424
IClientsGroupsService clientsGroupsService, IInvokeClientsService invokeClientsService)
2525
{
2626
_cloudSessionsRepository = cloudSessionsRepository;
2727
_inventoryRepository = inventoryRepository;
2828
_synchronizationRepository = synchronizationRepository;
29-
_cacheService = cacheService;
29+
_redisInfrastructureService = redisInfrastructureService;
3030
_sessionMemberMapper = sessionMemberMapper;
3131
_clientsGroupsService = clientsGroupsService;
3232
_invokeClientsService = invokeClientsService;
@@ -37,7 +37,7 @@ public async Task Handle(QuitSessionRequest request, CancellationToken cancellat
3737
CloudSessionData? innerCloudSessionData = null;
3838
SessionMemberData? innerQuitter = null;
3939

40-
var transaction = _cacheService.OpenTransaction();
40+
var transaction = _redisInfrastructureService.OpenTransaction();
4141

4242
var updateSessionResult = await _cloudSessionsRepository.Update(request.SessionId, cloudSessionData =>
4343
{

src/ByteSync.ServerCommon/Commands/CloudSessions/UpdateSessionSettingsCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class UpdateSessionSettingsCommandHandler : IRequestHandler<UpdateSession
1717

1818

1919
public UpdateSessionSettingsCommandHandler(ICloudSessionsRepository cloudSessionsRepository, IInventoryRepository inventoryRepository,
20-
ISynchronizationRepository synchronizationRepository, ICacheService cacheService, ISessionMemberMapper sessionMemberMapper,
20+
ISynchronizationRepository synchronizationRepository, IRedisInfrastructureService redisInfrastructureService, ISessionMemberMapper sessionMemberMapper,
2121
IInvokeClientsService invokeClientsService, ILogger<UpdateSessionSettingsCommandHandler> logger)
2222
{
2323
_cloudSessionsRepository = cloudSessionsRepository;

src/ByteSync.ServerCommon/Commands/Inventories/StartInventoryCommandHandler.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ByteSync.Common.Business.Sessions;
33
using ByteSync.ServerCommon.Business.Repositories;
44
using ByteSync.ServerCommon.Business.Sessions;
5+
using ByteSync.ServerCommon.Entities;
56
using ByteSync.ServerCommon.Interfaces.Repositories;
67
using ByteSync.ServerCommon.Interfaces.Services;
78
using ByteSync.ServerCommon.Interfaces.Services.Clients;
@@ -18,22 +19,22 @@ public class StartInventoryCommandHandler : IRequestHandler<StartInventoryReques
1819
private readonly ICloudSessionsRepository _cloudSessionsRepository;
1920
private readonly ISharedFilesService _sharedFilesService;
2021
private readonly IInvokeClientsService _invokeClientsService;
21-
private readonly ICacheService _cacheService;
22+
private readonly IRedisInfrastructureService _redisInfrastructureService;
2223
private readonly ILogger<StartInventoryCommandHandler> _logger;
2324

2425
public StartInventoryCommandHandler(
2526
IInventoryRepository inventoryRepository,
2627
ICloudSessionsRepository cloudSessionsRepository,
2728
ISharedFilesService sharedFilesService,
2829
IInvokeClientsService invokeClientsService,
29-
ICacheService cacheService,
30+
IRedisInfrastructureService redisInfrastructureService,
3031
ILogger<StartInventoryCommandHandler> logger)
3132
{
3233
_inventoryRepository = inventoryRepository;
3334
_cloudSessionsRepository = cloudSessionsRepository;
3435
_sharedFilesService = sharedFilesService;
3536
_invokeClientsService = invokeClientsService;
36-
_cacheService = cacheService;
37+
_redisInfrastructureService = redisInfrastructureService;
3738
_logger = logger;
3839
}
3940

@@ -42,13 +43,11 @@ public async Task<StartInventoryResult> Handle(StartInventoryRequest request, Ca
4243
var sessionId = request.SessionId;
4344
var client = request.Client;
4445

45-
await using var sessionRedisLock = await _cacheService.AcquireLockAsync(_cloudSessionsRepository.ComputeCacheKey(_cloudSessionsRepository.ElementName,
46-
sessionId));
46+
await using var sessionRedisLock = await _redisInfrastructureService.AcquireLockAsync(EntityType.Session, sessionId);
4747

48-
await using var inventoryRedisLock = await _cacheService.AcquireLockAsync(_inventoryRepository.ComputeCacheKey(_inventoryRepository.ElementName,
49-
sessionId));
48+
await using var inventoryRedisLock = await _redisInfrastructureService.AcquireLockAsync(EntityType.Inventory, sessionId);
5049

51-
var transaction = _cacheService.OpenTransaction();
50+
var transaction = _redisInfrastructureService.OpenTransaction();
5251

5352
UpdateEntityResult<InventoryData>? inventoryUpdateResult = null;
5453

0 commit comments

Comments
 (0)