Home DEVELOPER Heise Online Logo

Heise Online Logo

0


The previous installment of this blog series on .NET 8.0 is AddStandardResilienceHandler() Introduced. This post is about customizing the default settings.

Advertisement





Dr. Holger Schwittenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies through consulting and training as well as software development with 53 renowned experts. Through his appearances at numerous national and international conferences as well as over 90 expert books and over 1,500 expert articles, Holger Schwittenberg is one of the best-known experts for .NET and Web technologies in Germany.

iPhone Plane Trick | Are you going to travel by plane? Use the iPhone trick everyone is talking about this summer

alternative to AddStandardResilienceHandler() You can do it through AddResilienceHandler() Define your own Polly pipeline. Figure 1 shows the standard pipeline.



The standard handler completes five tasks in a so-called pipeline (Figure 1).

(Picture: Microsoft,

The following self-defined pipeline uses only orders 2 through 5 from the above table with the following settings:

  • Total request timeout: 10 seconds
  • Repeat: The call is repeated up to five times. The interval between trials is a constant 1 second. A screen is output when the trial is completed.
  • Circuit breaker: abort if 50% of requests fail in 10 seconds, but as soon as possible after 5 requests
  • Attempt timeout: 2 seconds
services.AddHttpClient(
  // Timeout insgesamt: Hier 10 Sekunden
  client => { client.Timeout = new TimeSpan(0,0,10); })
  .AddResilienceHandler(
   "CustomPipeline",
   static builder =>
   {
   // Wiederholungen bei Fehlern
   // Hier: Der Aufruf wird bis zu fünf Mal wiederholt. Der Abstand zwischen den Versuchen ist konstant 1 Sekunde. Bei dem Versuch gibt es eine Bildschirmausgabe.
   // https://www.pollydocs.org/strategies/retry.html
   builder.AddRetry(new HttpRetryStrategyOptions
    {
     // Customize and configure the retry logic.
     BackoffType = DelayBackoffType.Constant, // oder Linear oder Exponential
     Delay = TimeSpan.FromSeconds(1),
     MaxRetryAttempts = 10,
 
     OnRetry = static (args) =>
     {
      CUI.Cyan($"{DateTime.Now.ToLongTimeString()} Versuch #{args.AttemptNumber} Zeit seit letztem Versuch:{(int)(DateTime.Now - lastTry).TotalMilliseconds} ms Dauer:{args.Duration} Grund:{args.Outcome.Result} \n");
      lastTry = DateTime.Now;
      return ValueTask.CompletedTask;
     },
    });
 
    // Trennschalter, wenn zu viele Fehler passieren
    // Hier: Abbruch wenn in 10 Sekunden 50% der Anfragen fehlschlagen, frühestens aber nach 5 Anfragen
    // https://www.pollydocs.org/strategies/circuit-breaker.html
    builder.AddCircuitBreaker(new HttpCircuitBreakerStrategyOptions
    {
     // Customize and configure the circuit breaker logic.
     SamplingDuration = TimeSpan.FromSeconds(10),
     FailureRatio = 0.5,
     MinimumThroughput = 5,
     ShouldHandle = static args =>
     {
      return ValueTask.FromResult(args is
      {
       Outcome.Result.StatusCode: HttpStatusCode.RequestTimeout or HttpStatusCode.TooManyRequests or HttpStatusCode.InternalServerError
      });
     }
    });
 
    // Timeout für einzelne Versuche
    // hier: 2 Sekunden
    // https://www.pollydocs.org/strategies/timeout.html
    builder.AddTimeout(TimeSpan.FromSeconds(2));
   });


(RME)

One million dollars for the independent web browser: Ladybird launches

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version