diff --git a/src/Machine/src/Serval.Machine.Shared/Configuration/ServiceOptions.cs b/src/Machine/src/Serval.Machine.Shared/Configuration/ServiceOptions.cs index 8011e7b7..c41aa098 100644 --- a/src/Machine/src/Serval.Machine.Shared/Configuration/ServiceOptions.cs +++ b/src/Machine/src/Serval.Machine.Shared/Configuration/ServiceOptions.cs @@ -5,4 +5,5 @@ public class ServiceOptions public const string Key = "Service"; public string ServiceId { get; set; } = "machine_api"; + public TimeSpan ReadWriteLockTimeout { get; set; } = TimeSpan.FromSeconds(55); } diff --git a/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs b/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs index 7ea8679f..9c05ab5b 100644 --- a/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs +++ b/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs @@ -1,18 +1,25 @@ namespace Serval.Machine.Shared.Services; -public class DistributedReaderWriterLock(string hostId, IRepository locks, IIdGenerator idGenerator, string id) - : IDistributedReaderWriterLock +public class DistributedReaderWriterLock( + string hostId, + IRepository locks, + IIdGenerator idGenerator, + string id, + TimeSpan defaultLifetime +) : IDistributedReaderWriterLock { private readonly string _hostId = hostId; private readonly IRepository _locks = locks; private readonly IIdGenerator _idGenerator = idGenerator; private readonly string _id = id; + private readonly TimeSpan _defaultLifetime = defaultLifetime; public async Task ReaderLockAsync( TimeSpan? lifetime = default, CancellationToken cancellationToken = default ) { + lifetime ??= _defaultLifetime; string lockId = _idGenerator.GenerateId(); if (!await TryAcquireReaderLock(lockId, lifetime, cancellationToken)) { @@ -42,6 +49,7 @@ public async Task WriterLockAsync( CancellationToken cancellationToken = default ) { + lifetime ??= _defaultLifetime; string lockId = _idGenerator.GenerateId(); if (!await TryAcquireWriterLock(lockId, lifetime, cancellationToken)) { diff --git a/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLockFactory.cs b/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLockFactory.cs index 81810fb1..acc9e38c 100644 --- a/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLockFactory.cs +++ b/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLockFactory.cs @@ -39,7 +39,13 @@ await _locks.InsertAsync( // the lock is already made - no new one needs to be made // This is done instead of checking if it exists first to prevent race conditions. } - return new DistributedReaderWriterLock(_serviceOptions.ServiceId, _locks, _idGenerator, id); + return new DistributedReaderWriterLock( + _serviceOptions.ServiceId, + _locks, + _idGenerator, + id, + _serviceOptions.ReadWriteLockTimeout + ); } public async Task DeleteAsync(string id, CancellationToken cancellationToken = default)