Skip to content

Commit 4b455e3

Browse files
committed
Allow 2 concurrent synchronisations at a time in order to avoid reading in data faster than the writer is able to write out.
1 parent 60bb933 commit 4b455e3

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

Program.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ internal static class Global
5050

5151
internal static readonly AsyncLockQueueDictionary<string> FileOperationLocks = new AsyncLockQueueDictionary<string>();
5252
//internal static readonly AsyncLock FileOperationAsyncLock = new AsyncLock();
53+
internal static readonly AsyncSemaphore FileOperationSemaphore = new AsyncSemaphore(2); //allow 2 concurrent file synchronisations: while one is finishing the write, the next one can start the read
5354
}
5455
#pragma warning restore S2223
5556

@@ -607,35 +608,38 @@ public static async Task FileUpdated(string fullName, Context context)
607608
var otherFullName = GetOtherFullName(fullName);
608609
using (await Global.FileOperationLocks.LockAsync(fullName, otherFullName, context.Token))
609610
{
610-
var fullNameInvariant = fullName.ToUpperInvariantOnWindows(Global.CaseSensitiveFilenames);
611-
612-
if (
613-
Global.WatchedCodeExtension.Any(x => fullNameInvariant.EndsWith("." + x))
614-
|| Global.WatchedCodeExtension.Contains("*")
615-
)
611+
using (await Global.FileOperationSemaphore.LockAsync())
616612
{
617-
if (fullNameInvariant.StartsWith(Extensions.GetLongPath(Global.AsyncPath)))
618-
{
619-
await AsyncToSyncConverter.AsyncFileUpdated(fullName, context);
620-
}
621-
else if (IsSyncPath(fullNameInvariant)) //NB!
622-
{
623-
await SyncToAsyncConverter.SyncFileUpdated(fullName, context);
624-
}
625-
else
613+
var fullNameInvariant = fullName.ToUpperInvariantOnWindows(Global.CaseSensitiveFilenames);
614+
615+
if (
616+
Global.WatchedCodeExtension.Any(x => fullNameInvariant.EndsWith("." + x))
617+
|| Global.WatchedCodeExtension.Contains("*")
618+
)
626619
{
627-
throw new ArgumentException("fullName");
620+
if (fullNameInvariant.StartsWith(Extensions.GetLongPath(Global.AsyncPath)))
621+
{
622+
await AsyncToSyncConverter.AsyncFileUpdated(fullName, context);
623+
}
624+
else if (IsSyncPath(fullNameInvariant)) //NB!
625+
{
626+
await SyncToAsyncConverter.SyncFileUpdated(fullName, context);
627+
}
628+
else
629+
{
630+
throw new ArgumentException("fullName");
631+
}
628632
}
629-
}
630-
else //Assume ResX file
631-
{
632-
var fileData = await FileExtensions.ReadAllBytesAsync(Extensions.GetLongPath(fullName), context.Token);
633-
var originalData = fileData;
633+
else //Assume ResX file
634+
{
635+
var fileData = await FileExtensions.ReadAllBytesAsync(Extensions.GetLongPath(fullName), context.Token);
636+
var originalData = fileData;
634637

635-
//save without transformations
636-
await ConsoleWatch.SaveFileModifications(fullName, fileData, originalData, context);
637-
}
638-
}
638+
//save without transformations
639+
await ConsoleWatch.SaveFileModifications(fullName, fileData, originalData, context);
640+
}
641+
} //using (await Global.FileOperationSemaphore.LockAsync())
642+
} //using (await Global.FileOperationLocks.LockAsync(fullName, otherFullName, context.Token))
639643
}
640644
} //public static async Task FileUpdated(string fullName, Context context)
641645

0 commit comments

Comments
 (0)