Skip to content

Implement exponential backoff with jitter for retry logic in TeamsUtility methods #4896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions src/Commands/Utilities/TeamsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ public static Team NewTeam(ApiRequestHelper requestHelper, string groupId, strin
{
Group group = null;
Team returnTeam = null;

Random random = new();
// Maximum number of retries
const int maxRetries = 12;
// Create the Group
if (string.IsNullOrEmpty(groupId))
{
group = CreateGroup(requestHelper, displayName, description, classification, mailNickname, visibility, owners, sensitivityLabels, templateType, resourceBehaviorOptions);
bool wait = true;
int iterations = 0;
while (wait)

// Initial backoff time in seconds
const int initialBackoffSeconds = 5;

while (wait && iterations < maxRetries)
Copy link
Preview

Copilot AI May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The two retry loops use different maxRetries values (12 in the first block, 10 in the second). Verify whether this difference is intended or if a consistent retry limit should be applied.

Copilot uses AI. Check for mistakes.

{
iterations++;

Expand All @@ -170,14 +176,16 @@ public static Team NewTeam(ApiRequestHelper requestHelper, string groupId, strin
}
catch (Exception)
{
// In case of exception wait for 5 secs
Thread.Sleep(TimeSpan.FromSeconds(5));
}
// Calculate exponential backoff with a minimum of initialBackoffSeconds
int backoffSeconds = initialBackoffSeconds * (int)Math.Pow(2, iterations - 1);
// Cap at a maximum backoff (e.g., 30 seconds)
backoffSeconds = Math.Min(backoffSeconds, 30);

// Don't wait more than 1 minute
if (iterations > 12)
{
wait = false;
// Add random jitter between 0-1 second to avoid thundering herd
int jitterMs = random.Next(0, 1000);

// Sleep for the calculated time
Thread.Sleep(TimeSpan.FromSeconds(backoffSeconds) + TimeSpan.FromMilliseconds(jitterMs));
}
}
}
Expand All @@ -194,38 +202,45 @@ public static Team NewTeam(ApiRequestHelper requestHelper, string groupId, strin
if (group != null)
{
Team team = teamCI.ToTeam(group.Visibility.Value);
var retry = true;
var iteration = 0;
while (retry)

const int initialBackoffMs = 1000;
var retryCount = 0;
bool success = false;

while (!success && retryCount < maxRetries)
{
try
{
var teamSettings = requestHelper.Put($"v1.0/groups/{group.Id}/team", team);
if (teamSettings != null)
{
returnTeam = GetTeam(requestHelper, group.Id);
success = true;
}
retry = false;
}
catch (GraphException ge) when (ge.HttpResponse.StatusCode == System.Net.HttpStatusCode.Conflict)
{
// Handle conflict exceptions as if it succeeded, as it means a previous request succeeded enabling teams
returnTeam = GetTeam(requestHelper, group.Id);
retry = false;
success = true;
}
catch (Exception)
catch
{
Comment on lines +227 to 228
Copy link
Preview

Copilot AI May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider explicitly catching Exception (e.g., 'catch (Exception ex)') to enhance clarity and facilitate debugging or logging in the future.

Suggested change
catch
{
catch (Exception ex)
{
// Log the exception details for debugging
Console.WriteLine($"Exception occurred: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");

Copilot uses AI. Check for mistakes.

Thread.Sleep(5000);
iteration++;
if (iteration > 10)
retryCount++;

if (retryCount >= maxRetries)
{
// If we've reached max retries, rethrow the exception
throw;
}
}

if (iteration > 10) // don't try more than 10 times
{
retry = false;
// Exponential backoff with jitter to avoid thundering herd problem
int backoffMs = initialBackoffMs * (int)Math.Pow(2, retryCount - 1);
// Add up to 1 second of random jitter
int jitterMs = random.Next(0, 1000);
// Cap at 30 seconds max
int delayMs = Math.Min(backoffMs + jitterMs, 30000);
Thread.Sleep(delayMs);
}
}

Expand Down
Loading