Configs2026年5月23日·1 分钟阅读

Polly — .NET Resilience and Transient Fault Handling

A .NET library that lets developers express retry, circuit breaker, timeout, bulkhead isolation, and fallback policies in a fluent, thread-safe API.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Polly Overview
直接安装命令
npx -y tokrepo@latest install 40a83ed4-56c4-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Polly is a .NET resilience and transient fault handling library. It provides a fluent API for defining policies such as retry, circuit breaker, timeout, rate limiter, and fallback that wrap unreliable calls to external services, databases, or any operation that can fail transiently.

What Polly Does

  • Retries failed operations with configurable backoff strategies
  • Implements circuit breaker patterns to stop cascading failures
  • Enforces timeouts on long-running operations
  • Provides bulkhead isolation to limit concurrent resource usage
  • Combines multiple strategies into resilience pipelines

Architecture Overview

Polly v8 introduces a pipeline-based architecture built on ResiliencePipelineBuilder. Each strategy (retry, circuit breaker, timeout) is a composable middleware layer. The pipeline executes strategies in order, wrapping the inner delegate. The library uses a non-allocating execution path for hot paths and integrates with Microsoft.Extensions.Resilience for dependency injection in ASP.NET Core.

Self-Hosting & Configuration

  • Install the core package: dotnet add package Polly
  • For DI integration: dotnet add package Microsoft.Extensions.Resilience
  • Register pipelines in Program.cs via builder.Services.AddResiliencePipeline()
  • Configure strategies through option objects or appsettings.json
  • Works with .NET Standard 2.0+ and .NET 6 through .NET 9

Key Features

  • Fluent builder API for composing resilience strategies
  • Built-in telemetry and metering via .NET metrics
  • Hedging strategy for speculative parallel execution
  • Rate limiter integration with System.Threading.RateLimiting
  • Thread-safe and designed for high-throughput scenarios

Comparison with Similar Tools

  • Microsoft.Extensions.Http.Resilience — built on Polly; provides opinionated HttpClient resilience defaults
  • Resilience4j (Java) — similar patterns for the JVM ecosystem; no .NET support
  • Steeltoe — cloud-native .NET framework with circuit breaker; broader scope, less focused
  • Hystrix (.NET port) — Netflix circuit breaker; no longer actively maintained
  • Custom retry loops — ad hoc try/catch patterns; no composability, harder to test

FAQ

Q: What changed between Polly v7 and v8? A: Polly v8 replaced the policy-based API with a pipeline-based architecture using ResiliencePipelineBuilder. It is simpler, more performant, and integrates natively with Microsoft.Extensions.

Q: Can I use Polly with HttpClient? A: Yes. The Microsoft.Extensions.Http.Resilience package provides AddStandardResilienceHandler() that configures HttpClient with Polly-based retry, circuit breaker, and timeout out of the box.

Q: Does Polly support async operations? A: Yes. All resilience pipelines support both synchronous and asynchronous execution via ExecuteAsync with CancellationToken support.

Q: How do I monitor Polly behavior in production? A: Polly v8 emits .NET metrics and supports TelemetryListeners. You can export these to OpenTelemetry, Application Insights, or any metrics backend.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产