Skip to content

Commit

Permalink
Reduce thread usage by removing a possible unneeded Task.Run (#52)
Browse files Browse the repository at this point in the history
* Reduce thread usage by removing a possible unneeded Task.Run

* fmt
  • Loading branch information
adamhathcock authored Aug 5, 2024
1 parent 9da1d79 commit f8e4682
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
4 changes: 1 addition & 3 deletions src/Speckle.Core/Transports/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,8 @@ public override string ToString()
/// Awaits untill write completion (ie, the current queue is fully consumed).
/// </summary>
/// <returns></returns>
public async Task WriteComplete()
{
public async Task WriteComplete() =>
await Utilities.WaitUntil(() => WriteCompletionStatus, 500).ConfigureAwait(false);
}

/// <summary>
/// Returns true if the current write queue is empty and comitted.
Expand Down
15 changes: 3 additions & 12 deletions src/Speckle.Core/Transports/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@ public static class Utilities
/// </summary>
/// <param name="condition"></param>
/// <param name="frequency"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public static async Task WaitUntil(Func<bool> condition, int frequency = 25, int timeout = -1)
public static async Task WaitUntil(Func<bool> condition, int frequency = 25)
{
var waitTask = Task.Run(async () =>
while (!condition())
{
while (!condition())
{
await Task.Delay(frequency).ConfigureAwait(false);
}
});

if (waitTask != await Task.WhenAny(waitTask, Task.Delay(timeout)).ConfigureAwait(false))
{
throw new SpeckleException("Process timed out", new TimeoutException());
await Task.Delay(frequency).ConfigureAwait(false);
}
}
}
13 changes: 1 addition & 12 deletions src/Speckle.Transports.MongoDB/MongoDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,7 @@ internal void DeleteObject(string hash)
/// Awaits until write completion (ie, the current queue is fully consumed).
/// </summary>
/// <returns></returns>
public async Task WriteComplete()
{
await Utilities
.WaitUntil(
() =>
{
return GetWriteCompletionStatus();
},
500
)
.ConfigureAwait(false);
}
public async Task WriteComplete() => await Utilities.WaitUntil(GetWriteCompletionStatus, 500).ConfigureAwait(false);

/// <summary>
/// Returns true if the current write queue is empty and committed.
Expand Down

0 comments on commit f8e4682

Please sign in to comment.