Skip to content

HTTP Resilience

Granit.HttpResilience wraps Microsoft.Extensions.Http.Resilience (built on Polly v8) and exposes a single AddGranitHttpClient() extension that replaces bare AddHttpClient() calls across all Granit modules. Every outbound HTTP call automatically benefits from retry with exponential back-off, circuit breaking, and per-request timeouts.

StageDefault
Total timeout30 s
Retry3 attempts, exponential back-off (2 s base)
Circuit breakerOpens after 10 failures in 30 s, stays open for 1 min
Per-attempt timeout10 s

These defaults come from AddStandardResilienceHandler() and apply to every client registered via AddGranitHttpClient().

// Drop-in replacement for AddHttpClient()
services.AddGranitHttpClient("granit-webhooks", (sp, client) =>
{
client.BaseAddress = new Uri("https://webhook-receiver.example.com/");
client.Timeout = TimeSpan.FromSeconds(10);
});

The returned IHttpClientBuilder can be chained for further customization.

Override pipeline settings per named client in appsettings.json without redeploying:

{
"HttpResilience": {
"granit-webhooks": {
"Retry": { "MaxRetryAttempts": 5 },
"CircuitBreaker": {
"SamplingDuration": "00:01:00",
"MinimumThroughput": 20
}
},
"KeycloakAdmin": {
"Retry": { "MaxRetryAttempts": 1 }
}
}
}

Settings from HttpResilience:{clientName} are applied on top of the defaults at runtime via IPostConfigureOptions<HttpStandardResilienceOptions>.

All Granit modules that register outbound HttpClient instances declare [DependsOn(typeof(GranitHttpResilienceModule))] and call AddGranitHttpClient():

ModuleClient nameNotes
Granit.Webhooksgranit-webhook-deliveryWebhook delivery
Granit.Notifications.TwilioTwilioSMS + WhatsApp
Granit.Notifications.BrevoBrevoEmail + SMS + WhatsApp
Granit.Notifications.ZulipZulipChat
Granit.Notifications.Email.SendGridSendGridEmail
Granit.Notifications.Email.ScalewayScalewayEmail
Granit.Notifications.MobilePush.GoogleFcmGoogleFcmPushMobile push
Granit.Identity.KeycloakKeycloakAdminAdmin API
Granit.Identity.EntraIdMicrosoftGraphGraph API
Granit.AI.OllamaGranitAIOllamaHealthCheckHealth check
graph TD
    GHR[Granit.HttpResilience] --> MEHR[Microsoft.Extensions.Http.Resilience]
    MEHR --> POLLY[Polly v8]
    GHR --> WEBHOOKS[Granit.Webhooks]
    GHR --> NOTIF[Granit.Notifications.*]
    GHR --> IDENTITY[Granit.Identity.*]
    GHR --> AI[Granit.AI.Ollama]
  • ISO 27001 A.17.1 — service continuity: circuit breaking prevents cascade failures when a downstream dependency is unavailable
  • GDPR Article 32 — availability: resilience policies reduce data-processing downtime caused by transient network or API errors