-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve status and list commands (#8102)
- Loading branch information
Showing
5 changed files
with
117 additions
and
69 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
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
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
2 changes: 1 addition & 1 deletion
2
tools/secret-management/Azure.Sdk.Tools.SecretRotation.Core/RotationException.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
51 changes: 51 additions & 0 deletions
51
tools/secret-management/Azure.Sdk.Tools.SecretRotation.Core/TaskExtensions.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,51 @@ | ||
namespace Azure.Sdk.Tools.SecretRotation.Core; | ||
|
||
public static class TaskExtensions | ||
{ | ||
public static async Task<T[]> LimitConcurrencyAsync<T>(this IEnumerable<Task<T>> tasks, int concurrencyLimit = 1, CancellationToken cancellationToken = default) | ||
{ | ||
if (concurrencyLimit == int.MaxValue) | ||
{ | ||
return await Task.WhenAll(tasks); | ||
} | ||
|
||
var results = new List<T>(); | ||
|
||
if (concurrencyLimit == 1) | ||
{ | ||
foreach (var task in tasks) | ||
{ | ||
results.Add(await task); | ||
} | ||
|
||
return results.ToArray(); | ||
} | ||
|
||
var pending = new List<Task<T>>(); | ||
|
||
foreach (var task in tasks) | ||
{ | ||
pending.Add(task); | ||
|
||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
break; | ||
} | ||
|
||
if (pending.Count < concurrencyLimit) continue; | ||
|
||
var completed = await Task.WhenAny(pending); | ||
pending.Remove(completed); | ||
results.Add(await completed); | ||
} | ||
|
||
results.AddRange(await Task.WhenAll(pending)); | ||
|
||
return results.ToArray(); | ||
} | ||
|
||
public static Task<TResult[]> LimitConcurrencyAsync<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Task<TResult>> taskFactory, int concurrencyLimit = 1, CancellationToken cancellationToken = default) | ||
{ | ||
return LimitConcurrencyAsync(source.Select(taskFactory), concurrencyLimit, cancellationToken); | ||
} | ||
} |