forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Asset-Sync] Control Parallel Git Invocation (Azure#4112)
* add a new class TaskQueue that can be used to control the parallellism of git commands within a single git directory
- Loading branch information
Showing
2 changed files
with
147 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/TaskQueue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System; | ||
|
||
namespace Azure.Sdk.Tools.TestProxy.Store | ||
{ | ||
/// <summary> | ||
/// This class is used to control access to a directory. Within the GitProcessHandler, a queue is used per targeted git directory. This ensures | ||
/// that multiple Async requests hitting the asset store functionality will NEVER be able to stomp on each other. | ||
/// </summary> | ||
public class TaskQueue | ||
{ | ||
private SemaphoreSlim semaphore; | ||
|
||
public TaskQueue() | ||
{ | ||
semaphore = new SemaphoreSlim(1); | ||
} | ||
|
||
/// <summary> | ||
/// Used to await the running of a single block of code. Returns a value of type T. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="incomingTask"></param> | ||
/// <returns></returns> | ||
public async Task<T> EnqueueAsync<T>(Func<Task<T>> incomingTask) | ||
{ | ||
await semaphore.WaitAsync(); | ||
try | ||
{ | ||
return await incomingTask(); | ||
} | ||
finally | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Used to await the running of a single block of code. No incoming arguments, no return types. | ||
/// </summary> | ||
/// <param name="incomingTask"></param> | ||
/// <returns></returns> | ||
public async Task EnqueueAsync(Func<Task> incomingTask) | ||
{ | ||
await semaphore.WaitAsync(); | ||
|
||
try | ||
{ | ||
await incomingTask(); | ||
} | ||
finally | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Used to invoke a block of code. No incoming arguments, no output arguments. | ||
/// </summary> | ||
/// <param name="incomingTask"></param> | ||
public void Enqueue(Action incomingTask) | ||
{ | ||
semaphore.Wait(); | ||
|
||
try | ||
{ | ||
incomingTask(); | ||
} | ||
finally | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
} | ||
} |