最近更新时间:2025-03-17 18:49:11
SDK内置了多种重试等待策略,默认使用指数等待策略,您也可根据需要选择其他等待策略或自定义等待策略。
适用于需要快速增加等待时间的场景,如网络请求重试,避免短时间内频繁重试导致服务器压力过大。
指数等待策略的等待时间随着重试次数的增加呈指数增长,公式如下:delay = baseDelay * (2 ^ (attempts - 1))
参数说明:
参数名称 | 说明 |
---|---|
baseDelay | 基础等待时间,默认值为 200ms。 |
attempts | 当前重试次数。 |
maxDelay | 单次最大等待时间,默认值为20s。 |
假设 baseDelay = 200ms
,maxDelay = 20s
:
第 1 次重试:200ms * (2^0) = 200ms
,重试间隔时间为200ms。
第 2 次重试:200ms * (2^1) = 400ms
,重试间隔时间为400ms。
第 3 次重试:200ms * (2^2) = 800ms
,重试间隔时间为800ms。
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
RetryRule: retry.DefaultExponentialRetryRule, // 使用默认指数等待策略,baseDelay=200ms,maxDelay=20s
//RetryRule: retry.NewExponentialRetryRule(100*time.Millisecond, 10*time.Second), // 使用自定义指数等待策略,baseDelay=100ms,maxDelay=10s
})
适用于需要逐步增加等待时间的场景,如任务队列处理,避免短时间内大量重试。
线性等待策略的等待时间随着重试次数的增加呈线性增长,公式如下:delay = baseDelay * attempts
参数说明:
参数名称 | 说明 |
---|---|
baseDelay | 基础等待时间,默认值为 200ms。 |
attempts | 当前重试次数。 |
maxDelay | 单次最大等待时间,默认值为20s。 |
假设 baseDelay = 200ms
,maxDelay = 20s
:
第 1 次重试:200ms * 1 = 200ms
,重试间隔时间为200ms。
第 2 次重试:200ms * 2 = 400ms
,重试间隔时间为400ms。
第 3 次重试:200ms * 3 = 600ms
,重试间隔时间为600ms。
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
RetryRule: retry.DefaultLinearRetryRule, // 使用默认线性等待策略,baseDelay=200ms,maxDelay=20s
//RetryRule: retry.NewLinearRetryRule(100*time.Millisecond, 10*time.Second), // 使用自定义线性等待策略,baseDelay=100ms,maxDelay=10s
})
适用于需要稳定等待时间的场景,如固定间隔的重试逻辑。
固定等待策略的等待时间始终为一个固定值,公式如下:delay = baseDelay
参数说明:
参数名称 | 说明 |
---|---|
baseDelay | 基础等待时间,默认值为 200ms。 |
假设 baseDelay = 200ms
:
第 1 次重试间隔时间为200ms。
第 2 次重试间隔时间为200ms。
第 3 次重试间隔时间为200ms。
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
RetryRule: retry.DefaultFixedRetryRule, // 使用默认固定等待策略,baseDelay=200ms
//RetryRule: retry.NewFixedRetryRule(100 * time.Millisecond), // 使用自定义固定等待策略,baseDelay=100ms
})
适用于需要避免重试请求同时到达的场景,如分布式系统中的请求重试,通过抖动减少请求冲突。
抖动等待策略的等待时间在指定的取值范围内取随机值,即在 [minDelay, maxDelay] 区间取随机值。
参数说明:
参数名称 | 说明 |
---|---|
minDelay | 最小等待时间,默认值为 0。 |
maxDelay | 最大等待时间,默认值为200ms。 |
假设 minDelay = 0
,maxDelay = 200ms
:
第 1 次重试:在0和200ms之间取随机值,重试间隔时间为152ms。
第 2 次重试:在0和200ms之间取随机值,重试间隔时间为65ms。
第 3 次重试:在0和200ms之间取随机值,重试间隔时间为118ms。
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
RetryRule: retry.DefaultRandomRetryRule, // 使用默认抖动等待策略,minDelay=0,maxDelay=200ms
//RetryRule: retry.NewRandomRetryRule(200*time.Millisecond, 400*time.Millisecond), // 使用自定义抖动等待策略,minDelay=200ms,maxDelay=400ms
})
适用于需要请求失败后立即重试的场景。
不等待策略的等待时间始终为0,相当于立即重试。
第 1 次重试:重试间隔时间为0。
第 2 次重试:重试间隔时间为0。
第 3 次重试:重试间隔时间为0。
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
RetryRule: retry.DefaultNoDelayRetryRule, // 使用默认不等待策略
})
若SDK内置等待策略无法满足需要,也可以根据实际场景,自定义重试等待策略。RetryRule的参数类型为retry.RetryRule
,接口定义如下:
// RetryRule 重试等待策略
type RetryRule interface {
// GetDelay 获取重试等待时间
// attempts 当前重试次数, 值从1开始
// 返回值 重试等待时间
GetDelay(attempts int) time.Duration
}
自定义等待策略需要实现GetDelay方法,SDK会调用该方法获取等待时间,因此需要根据当前重试次数设计合适的等待策略,返回需要等待的时间。
创建自定义等待策略代码如下所示:
package main
import (
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
"math"
"time"
)
// CustomRetryRule 自定义等待策略
type CustomRetryRule struct {
baseDelay time.Duration // 基础等待时间
maxDelay time.Duration // 单次最大等待时间
}
// NewCustomRetryRule 创建自定义等待策略
func NewCustomRetryRule(baseDelay time.Duration, maxDelay time.Duration) CustomRetryRule {
return CustomRetryRule{
baseDelay: baseDelay,
maxDelay: maxDelay,
}
}
// GetDelay 获取重试等待时间
// attempts 当前重试次数, 值从1开始
// 返回值 重试等待时间
func (r CustomRetryRule) GetDelay(attempts int) time.Duration {
delay := r.baseDelay * time.Duration(math.Pow(2, float64(attempts-1)))
if delay > r.maxDelay {
return r.maxDelay
}
return delay
}
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
RetryRule: NewCustomRetryRule(1*time.Second, 10*time.Second), // 自定义等待策略
})
}
ShouldRetry参数用于设置重试的条件,表明遇到什么错误时应当重试,其类型为函数,函数定义为:func(error) bool
。该函数会传入一个错误对象,需要根据该错误判断是否需要重试,若需要重试则返回true,否则返回false。
追加需要重试的错误,实现自定义重试条件代码如下所示:
package main
import (
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/aws/retry"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
)
// CustomShouldRetry 自定义重试条件,根据err判断是否需要重试,若需要重试则返回true,否则返回false
func CustomShouldRetry(err error) bool {
// 如果该错误为SDK中定义的需要重试的错误,则返回true
if retry.ShouldRetry(err) {
return true
}
// 根据错误判断是否需要重试,如果需要重试则返回true,以下需要自行实现
// 如果不需要重试,则返回false
return false
}
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建Ks3Client
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证,必填
Region: "BEIJING", // 访问的地域,必填
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 访问的域名,必填
ShouldRetry: CustomShouldRetry, // 自定义重试条件
})
}
纯净模式