Skip to content

Conversation

Copy link

Copilot AI commented Jun 20, 2025

Summary

This PR implements a new IPolicyProvider interface that provides pre-configured Polly resilience pipelines based on enum identifiers, specifically designed for HTTP client scenarios with timeout and retry capabilities.

Features Added

Core Components

  • IPolicyProvider: Interface for retrieving policies by enum identifier
  • PolicyType: Enum defining available policy types (currently HttpClient)
  • PolicyProviderOptions: Configuration class with validation attributes
  • DefaultPolicyProvider: Main implementation with automatic configuration reload capability

HTTP Client Policy

The HTTP client policy combines timeout and retry strategies:

  • Pessimistic cancellation timeout (configurable, default 15 seconds)
  • Exponential backoff with jitter retry (configurable attempts, default 3)
  • Exception handling for TimeoutRejectedException, TaskCanceledException, OperationCanceledException
  • Base delay of 500ms for jitter calculation

Configuration Management

  • Integrated with IOptionsMonitor for automatic configuration reloading
  • Validates retry attempts (1-10) and timeout duration (1s-5m)
  • Policies are automatically recreated when configuration changes

Dependency Injection

// Register with default settings
services.AddPolicyProvider();

// Register with custom configuration
services.AddPolicyProvider(options =>
{
    options.HttpClientRetryAttempts = 3;
    options.HttpClientTimeout = TimeSpan.FromSeconds(15);
});

Usage Example

// Get policy provider
var policyProvider = serviceProvider.GetRequiredService<IPolicyProvider>();

// Get HTTP client policy
var httpPolicy = policyProvider.GetPolicy(PolicyType.HttpClient);

// Execute with policy
var result = await httpPolicy.ExecuteAsync(async cancellationToken =>
{
    return await httpClient.GetAsync("https://api.example.com", cancellationToken);
});

Key Benefits

  1. Enum-based identification: Type-safe policy selection
  2. Configuration-driven: Load settings from appsettings with automatic reload
  3. Performance optimized: Policies created once and cached for reuse
  4. Resilience: Combines timeout and retry with jitter to prevent thundering herd
  5. Extensible: Easy to add new policy types

Implementation Details

  • Uses Polly.Core v8+ ResiliencePipeline architecture
  • Follows existing Polly.Extensions patterns and conventions
  • Implements proper disposal and resource management
  • Includes comprehensive documentation and validation
  • Maintains backward compatibility

Files Added

  • src/Polly.Extensions/PolicyProvider/IPolicyProvider.cs
  • src/Polly.Extensions/PolicyProvider/PolicyType.cs
  • src/Polly.Extensions/PolicyProvider/PolicyProviderOptions.cs
  • src/Polly.Extensions/PolicyProvider/DefaultPolicyProvider.cs
  • src/Polly.Extensions/PolicyProvider/PolicyProviderServiceCollectionExtensions.cs
  • src/Polly.Extensions/PolicyProvider/README.md
  • test/Polly.Extensions.Tests/PolicyProvider/PolicyProviderTests.cs

This implementation addresses the need for a simplified, enum-based approach to policy management while maintaining the full power and flexibility of Polly's resilience patterns.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 20, 2025 02:12
Co-authored-by: nejaco-MSFT <125219867+nejaco-MSFT@users.noreply.github.com>
Co-authored-by: nejaco-MSFT <125219867+nejaco-MSFT@users.noreply.github.com>
Copilot AI changed the title [WIP] Create IPolicyProvider for resilient HttpClient calls Add IPolicyProvider interface for enum-based policy management with HTTP client support Jun 20, 2025
@martincostello
Copy link
Member

I don't understand where this has come from or why, so I'm closing it as there has been zero discussion in advance.

It looks to be replicating functionality that already exists downstream from dotnet/extensions.

@martincostello martincostello deleted the copilot/fix-40949ea8-77e9-4bf3-af1b-36dfc6674226 branch June 20, 2025 05:41
Copilot AI requested a review from martincostello June 20, 2025 05:41
@joelhulen
Copy link
Member

I don't understand where this has come from or why, so I'm closing it as there has been zero discussion in advance.

It looks to be replicating functionality that already exists downstream from dotnet/extensions.

I was wondering the same thing. It isn't clear who initiated the Copilot request, either. Agreed that this should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants