Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PollySignalR

NuGet Downloads CI License: MIT .NET 10 Ready

Polly v8 reconnect policy for ASP.NET Core SignalR.
Drop-in replacement for WithAutomaticReconnect(TimeSpan[]) with full Polly v8 exponential back-off, jitter, configurable max retries, and max delay. Also provides StartWithResilienceAsync for resilient initial connections.


Why PollySignalR?

SignalR's built-in reconnect is limited to a fixed array of delays:

Feature WithAutomaticReconnect(TimeSpan[]) PollySignalR
Exponential back-off ❌ manual ✅ automatic
Jitter (reconnect storm prevention)
Configurable max retries ❌ array length
Max delay cap ❌ last array element
Linear back-off option
Infinite retries
Polly v8 pipeline for initial connect
Single line of config

Installation

dotnet add package PollySignalR

Quick Start

var connection = new HubConnectionBuilder()
    .WithUrl("https://example.com/hub")
    .WithPollyReconnect()   // exponential back-off, infinite retries, up to 60 s
    .Build();

Configuration

var connection = new HubConnectionBuilder()
    .WithUrl("https://example.com/hub")
    .WithPollyReconnect(options =>
    {
        options.MaxRetries   = 10;                          // null = infinite (default)
        options.BaseDelay    = TimeSpan.FromSeconds(1);     // default: 1 s
        options.MaxDelay     = TimeSpan.FromSeconds(60);    // default: 60 s
        options.JitterFactor = 0.5;                         // ±50% noise (default)
        options.BackoffType  = DelayBackoffType.Exponential; // or Linear
    })
    .Build();

How delays are computed

delay = clamp(BaseDelay × 2^attempt, 0, MaxDelay)
      + random(−JitterFactor × BaseDelay, +JitterFactor × BaseDelay)
Attempt BaseDelay=1s, MaxDelay=60s
0 ~1 s
1 ~2 s
2 ~4 s
3 ~8 s
4 ~16 s
5 ~32 s
6+ ~60 s (capped)

Resilient initial connection

Use StartWithResilienceAsync to apply a full Polly v8 pipeline to the first connect attempt (before automatic reconnect takes over):

var pipeline = new ResiliencePipelineBuilder()
    .AddRetry(new RetryStrategyOptions
    {
        ShouldHandle  = new PredicateBuilder().Handle<Exception>(),
        MaxRetryAttempts = 5,
        BackoffType   = DelayBackoffType.Exponential,
        Delay         = TimeSpan.FromSeconds(1),
        UseJitter     = true,
    })
    .AddTimeout(new TimeoutStrategyOptions { Timeout = TimeSpan.FromSeconds(30) })
    .Build();

await connection.StartWithResilienceAsync(pipeline);

Blazor WebAssembly

// Program.cs
builder.Services.AddSingleton(sp =>
    new HubConnectionBuilder()
        .WithUrl(builder.HostEnvironment.BaseAddress + "chathub")
        .WithPollyReconnect(o =>
        {
            o.BaseDelay  = TimeSpan.FromSeconds(2);
            o.MaxDelay   = TimeSpan.FromSeconds(120);
            o.MaxRetries = null; // never give up
        })
        .Build());

Related Packages

Package Downloads Description
PollyHealthChecks Downloads ASP.NET Core health checks for Polly v8 circuit breakers — expose circuit-breaker state (Closed, HalfOpen, Open, Isolated) as /health endpoint responses
PollyBackoff Downloads Backoff delay strategies for Polly v8 resilience pipelines
PollyGrpc Downloads Polly v8 resilience interceptor for gRPC
PollyEFCore Downloads Polly v8 resilience pipelines for Entity Framework Core — wrap every EF Core query and SaveChanges with retry, timeout and circuit-breaker via a single AddPollyResilience() call
PollyMailKit Downloads Polly v8 resilience pipelines for MailKit — retry, timeout, and circuit-breaker for SmtpClient.SendAsync and any MailKit SMTP operation
PollyMassTransit Downloads Polly v8 resilience pipelines for MassTransit — retry, timeout, and circuit-breaker for IBus.Publish and ISendEndpointProvider.Send
PollyOpenAI Downloads Polly v8 resilience for OpenAI and Azure OpenAI API calls
PollyAzureEventHub Downloads Polly v8 resilience pipelines for Azure Event Hubs — retry, timeout, and circuit-breaker for EventHubProducerClient and EventHubConsumerClient
PollyElasticsearch Downloads Polly v8 resilience pipelines for Elastic.Clients.Elasticsearch 8+ — retry, timeout, and circuit-breaker for any Elasticsearch operation, plus a built-in ElasticTransientErrors predicate covering rate limiting (429), service unavailability (503), gateway timeouts (504), and connection failures
PollyHangfire Downloads Polly v8 resilience pipelines for Hangfire — retry, timeout, and circuit-breaker for IBackgroundJobClient.Enqueue and Schedule
PollySendGrid Downloads Polly v8 resilience pipelines for SendGrid — retry, timeout, and circuit-breaker for ISendGridClient.SendEmailAsync
PollyMediatR Downloads Polly v8 resilience pipelines for MediatR — add retry, timeout, circuit-breaker, rate-limiting, hedging, and chaos engineering to any MediatR request handler with a single line of DI registration
PollyAzureKeyVault Downloads Polly v8 resilience pipelines for Azure Key Vault — retry, timeout, and circuit-breaker for SecretClient, KeyClient, and CertificateClient
PollyAzureQueueStorage Downloads Polly v8 resilience pipelines for Azure Queue Storage — retry, timeout, and circuit-breaker for Azure.Storage.Queues QueueClient
PollyRedis Downloads Polly v8 resilience for StackExchange.Redis
PollyAzureServiceBus Downloads Polly v8 resilience for Azure Service Bus — retry, circuit breaker, and timeout for sending and receiving messages
PollyKafka Downloads Polly v8 resilience for Confluent.Kafka — retry, circuit breaker, and timeout for producers and consumers
PollyAzureTableStorage Downloads Polly v8 resilience pipelines for Azure Table Storage — retry, timeout, and circuit-breaker for Azure.Data.Tables TableClient
PollyChaos Downloads Chaos engineering and fault-injection resilience strategies for Polly v8 pipelines

💼 Need .NET consulting?

The author of this package is available for consulting on Polly v8 resilience, Azure cloud architecture, and clean .NET design.

→ solidqualitysolutions.com · LinkedIn

License

MIT

About

Polly v8 reconnect policy for SignalR — exponential back-off with jitter, max retries, max delay, drop-in for WithAutomaticReconnect

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages