Path: redb-route/redb.Route.File/ReadLockStrategies.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.File/ReadLockStrategies.cs
----
namespace redb.Route.File;
///
/// Abstraction for read lock strategies used by the file consumer.
/// Each strategy determines how to acquire and release locks on files being polled,
/// preventing the consumer from reading a file that is still being written.
///
public interface IReadLockStrategy
{
///
/// Attempts to acquire a read lock on the file.
///
/// The file to lock.
/// Endpoint options with lock configuration.
/// Cancellation token.
/// True if the lock was acquired; false if the file should be skipped.
Task AcquireLock(FileInfo file, FileEndpointOptions options, CancellationToken ct);
///
/// Releases the read lock on the file after processing.
///
/// The file to unlock.
/// Endpoint options with lock configuration.
void ReleaseLock(FileInfo file, FileEndpointOptions options);
///
/// Path the file occupies while this lock is held. Strategies that relocate the file
/// (see ) report the new location here so the
/// consumer reads and post-processes the right path.
///
/// Path the file had before the lock was acquired.
/// The effective path. Default: unchanged.
string GetWorkPath(string originalPath) => originalPath;
///
/// Read stream already held by this lock, if any. Strategies that keep an exclusive
/// handle (see ) expose it here: reopening such a
/// file would fail against the lock this very consumer holds.
///
/// Path the file had before the lock was acquired.
/// The open stream, or null when the consumer should open the file itself.
Stream? GetLockedStream(string originalPath) => null;
}
///
/// No read lock — file is picked up immediately.
///
internal sealed class NoReadLock : IReadLockStrategy
{
public static readonly NoReadLock Instance = new();
public Task AcquireLock(FileInfo file, FileEndpointOptions options, CancellationToken ct)
=> Task.FromResult(true);
public void ReleaseLock(FileInfo file, FileEndpointOptions options) { }
}
///
/// Marker file lock strategy. Creates a *.redbLock file next to the target.
/// Other consumers skip files with an existing lock marker.
///
internal sealed class MarkerFileReadLock : IReadLockStrategy
{
public static readonly MarkerFileReadLock Instance = new();
public Task AcquireLock(FileInfo file, FileEndpointOptions options, CancellationToken ct)
{
var markerPath = file.FullName + options.ReadLockMarkerFileExtension;
if (System.IO.File.Exists(markerPath))
return Task.FromResult(false); // Another consumer holds the lock
try
{
// Create marker file atomically — if it already exists, FileMode.CreateNew throws
using var fs = new FileStream(markerPath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
return Task.FromResult(true);
}
catch (IOException)
{
// Race condition: another process created the marker first
return Task.FromResult(false);
}
}
public void ReleaseLock(FileInfo file, FileEndpointOptions options)
{
var markerPath = file.FullName + options.ReadLockMarkerFileExtension;
try
{
System.IO.File.Delete(markerPath);
}
catch (IOException)
{
// Best effort
}
}
}
///
/// Changed-size read lock. Waits until the file size stops changing for
/// milliseconds.
/// Detects files still being written by monitoring size stability.
///
internal sealed class ChangedReadLock : IReadLockStrategy
{
public static readonly ChangedReadLock Instance = new();
public async Task AcquireLock(FileInfo file, FileEndpointOptions options, CancellationToken ct)
{
var timeout = options.ReadLockTimeout;
var checkInterval = options.ReadLockCheckInterval;
var minAge = options.ReadLockMinAge;
var sw = System.Diagnostics.Stopwatch.StartNew();
var lastSize = file.Length;
var lastModified = file.LastWriteTimeUtc;
var stableStart = sw.ElapsedMilliseconds;
while (sw.ElapsedMilliseconds < timeout)
{
ct.ThrowIfCancellationRequested();
await Task.Delay(checkInterval, ct).ConfigureAwait(false);
file.Refresh();
if (!file.Exists)
return false; // File was deleted while waiting
if (file.Length != lastSize || file.LastWriteTimeUtc != lastModified)
{
// File changed — reset stability timer
lastSize = file.Length;
lastModified = file.LastWriteTimeUtc;
stableStart = sw.ElapsedMilliseconds;
}
else if (sw.ElapsedMilliseconds - stableStart >= minAge)
{
// File has been stable for minAge
return true;
}
}
return false; // Timeout
}
public void ReleaseLock(FileInfo file, FileEndpointOptions options) { }
}
///
/// OS-level file lock using FileShare.None. If the file cannot be opened exclusively,
/// it means another process has it locked.
///
internal sealed class FileLockReadLock : IReadLockStrategy
{
// Holds active lock streams keyed by absolute path
private readonly System.Collections.Concurrent.ConcurrentDictionary _locks = new();
public Task AcquireLock(FileInfo file, FileEndpointOptions options, CancellationToken ct)
{
try
{
// FileShare.Delete and nothing else: other processes can neither read nor write the
// file while we hold it, but this process can still delete or move it during
// post-processing (Windows requires FILE_SHARE_DELETE on the open handle for that).
var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete);
_locks[file.FullName] = fs;
return Task.FromResult(true);
}
catch (IOException)
{
return Task.FromResult(false); // File is locked by another process
}
catch (UnauthorizedAccessException)
{
return Task.FromResult(false);
}
}
public void ReleaseLock(FileInfo file, FileEndpointOptions options)
{
if (_locks.TryRemove(file.FullName, out var fs))
{
fs.Dispose();
}
}
///
/// The exclusive handle acquired by this strategy. The consumer must read through it:
/// opening the file a second time would be denied by our own lock.
///
public Stream? GetLockedStream(string originalPath)
=> _locks.TryGetValue(originalPath, out var fs) ? fs : null;
}
///
/// Rename lock strategy. Tries to rename the file to a temp name; if rename fails,
/// the file is locked by another process. Renames back after processing.
///
internal sealed class RenameReadLock : IReadLockStrategy
{
private readonly System.Collections.Concurrent.ConcurrentDictionary _renames = new();
///
/// While the lock is held the file lives under the temporary name, so that is where the
/// consumer must read from and post-process.
///
public string GetWorkPath(string originalPath)
=> _renames.TryGetValue(originalPath, out var temp) ? temp : originalPath;
public Task AcquireLock(FileInfo file, FileEndpointOptions options, CancellationToken ct)
{
var tempName = file.FullName + ".redbRename";
try
{
System.IO.File.Move(file.FullName, tempName);
_renames[file.FullName] = tempName;
return Task.FromResult(true);
}
catch (IOException)
{
return Task.FromResult(false); // File in use
}
}
public void ReleaseLock(FileInfo file, FileEndpointOptions options)
{
if (_renames.TryRemove(file.FullName, out var tempName))
{
try
{
if (System.IO.File.Exists(tempName))
{
System.IO.File.Move(tempName, file.FullName);
}
}
catch (IOException)
{
// Best effort — file may have been moved/deleted during processing
}
}
}
}
///
/// Factory for creating read lock strategy instances based on configuration.
///
internal static class ReadLockFactory
{
/// Creates the appropriate read lock strategy for the given configuration.
public static IReadLockStrategy Create(ReadLockStrategy strategy) => strategy switch
{
ReadLockStrategy.None => NoReadLock.Instance,
ReadLockStrategy.MarkerFile => MarkerFileReadLock.Instance,
ReadLockStrategy.Changed => ChangedReadLock.Instance,
ReadLockStrategy.FileLock => new FileLockReadLock(),
ReadLockStrategy.Rename => new RenameReadLock(),
_ => throw new ArgumentOutOfRangeException(nameof(strategy), strategy, "Unknown read lock strategy.")
};
}