Skip to content

Commit 6f8512f

Browse files
committed
Increased circuit breaker threshold
Fix lifescope issue with IHttpClient Adapt circuit breaker to trigger its counter for no success http response
1 parent feafb4b commit 6f8512f

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
namespace WebMVC.Services.Utilities
1010
{
11+
/// <summary>
12+
/// HttpClient wrapper that integrates Retry and Circuit
13+
/// breaker policies when calling to Api services.
14+
/// Currently is ONLY implemented for the ASP MVC
15+
/// and Xamarin App
16+
/// </summary>
1117
public class HttpApiClientWrapper : IHttpClient
1218
{
1319
private HttpClient _client;
@@ -49,11 +55,11 @@ private Policy CreateRetryPolicy() =>
4955
Policy.Handle<HttpRequestException>()
5056
.WaitAndRetryAsync(
5157
// number of retries
52-
3,
58+
3,
5359
// exponential backofff
5460
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
5561
// on retry
56-
(exception, timeSpan, retryCount, context) =>
62+
(exception, timeSpan, retryCount, context) =>
5763
{
5864
_logger.LogTrace($"Retry {retryCount} " +
5965
$"of {context.PolicyKey} " +
@@ -62,27 +68,21 @@ private Policy CreateRetryPolicy() =>
6268
}
6369
);
6470

65-
// Notice that these (and other methods below) are Task
66-
// returning asynchronous methods. But, they do not
67-
// have the 'async' modifier, and do not contain
68-
// any 'await statements. In each of these methods,
69-
// the only asynchronous call is the last (or only)
70-
// statement of the method. In those instances,
71-
// a Task returning method that does not use the
72-
// async modifier is preferred. The compiler generates
73-
// synchronous code for this method, but returns the
74-
// task from the underlying asynchronous method. The
75-
// generated code does not contain the state machine
76-
// generated for asynchronous methods.
7771
public Task<string> GetStringAsync(string uri) =>
78-
HttpInvoker(() => _client.GetStringAsync(uri));
72+
HttpInvoker(() =>
73+
_client.GetStringAsync(uri));
7974

8075
public Task<HttpResponseMessage> PostAsync<T>(string uri, T item) =>
8176
// a new StringContent must be created for each retry
8277
// as it is disposed after each call
83-
HttpInvoker(() =>_client.PostAsync(uri,
84-
new StringContent(JsonConvert.SerializeObject(item),
85-
System.Text.Encoding.UTF8, "application/json")));
78+
HttpInvoker(() =>
79+
{
80+
var response = _client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json"));
81+
// raise exception if not success response
82+
// needed for circuit breaker to track fails
83+
response.Result.EnsureSuccessStatusCode();
84+
return response;
85+
});
8686

8787
public Task<HttpResponseMessage> DeleteAsync(string uri) =>
8888
HttpInvoker(() => _client.DeleteAsync(uri));

src/Web/WebMVC/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public void ConfigureServices(IServiceCollection services)
5454

5555
if(Configuration.GetValue<string>("ActivateCircuitBreaker") == bool.TrueString)
5656
{
57-
services.AddSingleton<IHttpClient, HttpApiClientWrapper>();
57+
services.AddTransient<IHttpClient, HttpApiClientWrapper>();
5858
}
5959
else
6060
{
61-
services.AddSingleton<IHttpClient, HttpApiClient>();
61+
services.AddTransient<IHttpClient, HttpApiClient>();
6262
}
6363
}
6464

src/Web/WebSPA/Client/modules/shared/services/data.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import 'rxjs/add/operator/catch';
1111
import { SecurityService } from './security.service';
1212
import { Guid } from '../../../guid';
1313

14+
// Implementing a Retry-Circuit breaker policy
15+
// is pending to do for the SPA app
1416
@Injectable()
1517
export class DataService {
1618
constructor(private http: Http, private securityService: SecurityService) { }

0 commit comments

Comments
 (0)