RetryPolicy class
重试策略配置类
用于配置请求重试行为,支持固定间隔、指数退避、指数退避+抖动三种策略。
使用示例
// 固定间隔重试(向后兼容)
RxNet.get()
.setPath("/api/data")
.setRetryCount(3, interval: Duration(seconds: 2))
.request();
// 指数退避重试
RxNet.get()
.setPath("/api/data")
.setRetryPolicy(RetryPolicy(
maxRetries: 3,
strategy: RetryStrategy.exponentialBackoff,
baseInterval: Duration(seconds: 1),
))
.request();
// 指数退避 + 抖动重试
RxNet.get()
.setPath("/api/data")
.setRetryPolicy(RetryPolicy(
maxRetries: 5,
strategy: RetryStrategy.exponentialBackoffWithJitter,
baseInterval: Duration(seconds: 1),
maxInterval: Duration(seconds: 30),
))
.request();
Constructors
- RetryPolicy({int maxRetries = 3, RetryStrategy strategy = RetryStrategy.fixed, Duration baseInterval = const Duration(seconds: 1), Duration? maxInterval, double backoffMultiplier = 2.0})
-
const
- RetryPolicy.exponentialBackoff({int maxRetries = 3, Duration baseInterval = const Duration(seconds: 1), Duration? maxInterval, double backoffMultiplier = 2.0})
-
指数退避重试的工厂方法
factory
- RetryPolicy.exponentialBackoffWithJitter({int maxRetries = 5, Duration baseInterval = const Duration(seconds: 1), Duration? maxInterval, double backoffMultiplier = 2.0})
-
指数退避 + 抖动重试的工厂方法
factory
- RetryPolicy.fixed({int maxRetries = 3, Duration interval = const Duration(seconds: 1)})
-
固定间隔重试的工厂方法(兼容旧 API 行为)
factory
Properties
- backoffMultiplier → double
-
退避倍数(默认 2.0)
final
- baseInterval → Duration
-
基础间隔时间(固定间隔使用,指数退避的基础值)
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- maxInterval → Duration?
-
最大间隔时间(仅指数退避策略使用,防止间隔过长)
final
- maxRetries → int
-
最大重试次数
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- strategy → RetryStrategy
-
重试策略
final
Methods
-
getDelay(
int attempt) → Duration -
计算第
attempt次重试的延迟时间(从 0 开始计数) -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited