Skip to content
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

Adding timeout and retries #25

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/GitHubActions.Teams.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Aliencube.GitHubActions.Teams.ConsoleApp
/// </summary>
public static class Program
{
static Program() {
HttpClient = new HttpClient(new RetryHandler(new HttpClientHandler()))
{
Timeout = TimeSpan.FromSeconds(100) // Default one, just to be easier to customize
};
}

/// <summary>
/// Gets or sets the <see cref="IMessageHandler"/> instance.
/// </summary>
Expand All @@ -23,7 +30,7 @@ public static class Program
/// <summary>
/// Gets or sets the <see cref="HttpClient"/> instance.
/// </summary>
public static HttpClient HttpClient { get; set; } = new HttpClient();
public static HttpClient HttpClient { get; set; }

private static JsonSerializerSettings settings { get; } =
new JsonSerializerSettings()
Expand Down
42 changes: 42 additions & 0 deletions src/GitHubActions.Teams.ConsoleApp/RetryHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Aliencube.GitHubActions.Teams.ConsoleApp
{
public class RetryHandler : DelegatingHandler
{
// Strongly consider limiting the number of retries - "retry forever" is
// probably not the most user friendly way you could respond to "the
// network cable got pulled out."
private const int MaxRetries = 3;

public RetryHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{ }

protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
for (int i = 0; i < MaxRetries; i++)
{
response = await base.SendAsync(request, cancellationToken);
if (response.IsSuccessStatusCode)
{
return response;
}
}

return response;
}
}
}