guibranco / Sankhya-SDK-dotnet

📊 ⚙️ Sankhya platform .NET SDK
https://guibranco.github.io/Sankhya-SDK-dotnet/
MIT License
4 stars 3 forks source link

[FEATURE] Implement `Polly` library for retry handling in `SankhyaWrapper` #118

Open guibranco opened 1 year ago

guibranco commented 1 year ago

Is your feature request related to a problem? Please describe.
Currently, the SankhyaWrapper does not handle transient faults effectively, which can lead to failures during API calls. To enhance the resilience of the application, it's essential to implement a retry mechanism, timeout, and circuit breaker patterns.

Describe the solution you'd like
Integrate the Polly library into the SankhyaWrapper to implement the following resilience strategies:

  1. Retry Policy: Automatically retry failed requests a configurable number of times.
  2. Timeout Policy: Set a timeout for requests to prevent hanging indefinitely.
  3. Circuit Breaker Policy: Stop sending requests when failures reach a certain threshold, allowing the system to recover before retrying.

Implementation References:

Example Implementation:
Below is a sample implementation of how to configure Polly in the SankhyaWrapper:

using Polly;
using System;
using System.Net.Http;
using System.Threading.Tasks;

public class SankhyaWrapper
{
    private readonly HttpClient _httpClient;

    public SankhyaWrapper(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task<HttpResponseMessage> MakeRequestAsync()
    {
        var retryPolicy = Policy
            .Handle<HttpRequestException>()
            .RetryAsync(3); // Number of retry attempts

        var timeoutPolicy = Policy
            .TimeoutAsync(TimeSpan.FromSeconds(30)); // Timeout duration

        var circuitBreakerPolicy = Policy
            .Handle<HttpRequestException>()
            .CircuitBreakerAsync(3, TimeSpan.FromMinutes(1)); // Break for 1 minute after 3 failures

        return await Policy.WrapAsync(retryPolicy, timeoutPolicy, circuitBreakerPolicy)
            .ExecuteAsync(async () =>
            {
                return await _httpClient.GetAsync("your-api-endpoint");
            });
    }
}

Additional Context
Implementing these policies will significantly improve the reliability of the SankhyaWrapper when interacting with external services, leading to a better user experience and reduced downtime.

gitauto-ai[bot] commented 2 months ago

@guibranco Pull request completed! Check it out here https://github.com/guibranco/Sankhya-SDK-dotnet/pull/284 🚀

Note: I automatically create a pull request for an unassigned and open issue in order from oldest to newest once a day at 00:00 UTC, as long as you have remaining automation usage. Should you have any questions or wish to change settings or limits, please feel free to contact info@gitauto.ai or invite us to Slack Connect.