After reading this document page, https://docs.abp.io/en/abp/latest/Distributed-Event-Bus I am confused about shall I add distributed lock for event handler. Scenario: The event hander is within a Micro Service and Deployed with two instances. Shall I add distributed lock in event handler so that to make sure one message only processed once? Like code below:
public async Task HandleEventAsync(EntityDataSyncEto eto)
{
try
{
await using var handle = await _distributedLock.TryAcquireAsync("SyncEntityList");
if (handle != null)
{
_ = await _dataSyncAppService.ProcessEntity(eto);
}
}
catch (Exception ex)
{
_logger.LogException(ex);
}
}
Then I noticed with above lock, if there are several messages coming almost at the same time, some messages are missed to process in the event handler. Thanks, Domina
- ABP Framework version: v7.2.3
- UI Type: Angular
- Database System: EF Core/ MongoDB
- Tiered (for MVC) or Auth Server Separated (for Angular): yes , Microservice
1 Answer(s)
-
0
This is by design, if the lock is not acquired, it returns immediately
You can consider specifying a waiting time, or issuing a compensation event See: https://github.com/madelson/DistributedLock?tab=readme-ov-file#tryacquire
- https://docs.abp.io/en/abp/latest/Distributed-Locking#using-the-iabpdistributedlock-service