v1.31.0
conocimientoapi-integration-patternsexamples/

polly-advanced.md

Referencia que api-integration-patterns carga bajo demanda desde su SKILL.md.

api-integration-patternsskill
examplescarpeta
3501 Btamaño
2026-09-21último cambio del pack

Patrón: Retry + Circuit Breaker + Timeout + Fallback Versión: Polly v8+ Uso: Máxima resiliencia para APIs críticas


Pipeline Completo

using Polly;
using Polly.Retry;
using Polly.CircuitBreaker;
using Polly.Timeout;

builder.Services.AddResiliencePipeline("critical-api", pipelineBuilder =>
{
    // 1. TIMEOUT (más interno - se ejecuta primero)
    pipelineBuilder.AddTimeout(TimeSpan.FromSeconds(10));

    // 2. RETRY con backoff exponencial + jitter
    pipelineBuilder.AddRetry(new RetryStrategyOptions<HttpResponseMessage>
    {
        MaxRetryAttempts = 3,
        BackoffType = DelayBackoffType.Exponential,
        Delay = TimeSpan.FromSeconds(1),
        UseJitter = true,
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<HttpRequestException>()
            .Handle<TimeoutException>()
            .HandleResult(r =>
                r.StatusCode == System.Net.HttpStatusCode.TooManyRequests ||
                r.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable),
        OnRetry = args =>
        {
            Console.WriteLine($"Retry {args.AttemptNumber} tras {args.RetryDelay}");
            return default;
        }
    });

    // 3. CIRCUIT BREAKER
    pipelineBuilder.AddCircuitBreaker(new CircuitBreakerStrategyOptions<HttpResponseMessage>
    {
        FailureRatio = 0.5,
        SamplingDuration = TimeSpan.FromSeconds(10),
        MinimumThroughput = 10,
        BreakDuration = TimeSpan.FromMinutes(1),
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<HttpRequestException>()
            .HandleResult(r => r.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable),
        OnOpened = args =>
        {
            Console.WriteLine("⚠️ Circuit ABIERTO");
            return default;
        }
    });

    // 4. FALLBACK (más externo)
    pipelineBuilder.AddFallback(new FallbackStrategyOptions<HttpResponseMessage>
    {
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<BrokenCircuitException>(),
        FallbackAction = args =>
        {
            var fallbackResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JsonSerializer.Serialize(new
                {
                    message = "Servicio temporalmente no disponible",
                    usandoFallback = true
                }))
            };

            return Outcome.FromResult(fallbackResponse);
        }
    });
});

// Uso en HttpClient
builder.Services.AddHttpClient<ICriticalApiClient, CriticalApiClient>()
    .AddResilienceHandler("critical-api");

Con Telemetría (Application Insights)

builder.Services.AddResiliencePipeline("api-with-telemetry", (pipelineBuilder, context) =>
{
    var telemetry = context.ServiceProvider.GetService<TelemetryClient>();

    pipelineBuilder.AddRetry(new RetryStrategyOptions<HttpResponseMessage>
    {
        MaxRetryAttempts = 3,
        OnRetry = args =>
        {
            telemetry?.TrackEvent("ApiRetry", new Dictionary<string, string>
            {
                ["Attempt"] = args.AttemptNumber.ToString(),
                ["Outcome"] = args.Outcome.Exception?.Message ?? args.Outcome.Result?.StatusCode.ToString() ?? "Unknown"
            });
            return default;
        }
    });
});

Example: polly-advanced - Ovillo

← Volver a la ficha de api-integration-patterns

Ovillo v1.31.0tag v1.31.0 (2026-10-06) · sin decisiones posteriores69 skills · 19 agents · 22 hooks · 3 reglas · 15 tools · 103 decisionesfuente: ovillo/.claude/skills/api-integration-patterns/examples/polly-advanced.mdCopyright 2026 Hugo Valer Rojas · Apache-2.0