6-Retry-zh.md

May 27, 2026 · View on GitHub

← 超时配置 | 重试机制(English) | 异常处理 →


重试机制

请求的处理逻辑内置了网络异常重试逻辑,即当遇到网络异常问题或限流错误时,系统会自动尝试重新发起请求,以确保服务的稳定性和可靠性。若请求因业务逻辑错误而报错,例如参数错误、资源不存在等情况,SDK 将不会执行重试操作,这是因为业务层面的错误通常需要应用程序根据具体的错误信息做出相应的处理或调整,而非简单地重复尝试。

重试延迟说明

SDK 为了防止请求风暴,提高系统稳定性,并有效缓解服务雪崩风险,增加重试间的延迟,实现对服务端压力的自适应控制。

延迟时间公式

$ \text{delay} = \text{min}(\text{MaxDelay}, 2ⁿ \times \text{minDelay} \times (1 + \text{Rand}[0, 1)) + \text{Retry}-\text{After} $

参数说明
maxDelay最大延迟时间,计算的最大延迟不会超过 maxDelay,默认 300s(DefaultRetryerMaxRetryDelay
minDelay最小延迟时间基线,默认 30ms(DefaultRetryerMinRetryDelay
2ⁿ纯指数增长
(1 + Rand[0, 1))把结果随机放大 1 ~ 2 倍,避免「惊群效应」
min(...)防止无限增长,超过 MaxDelay
Retry-After服务端如果显式告知休眠时长,则先按它要求静默

举例说明(演示用:把 MaxDelay 缩小到 500ms 便于观察封顶行为)

MaxDelay=500msminDelay=30ms

重试次数(从第 0 次开始)指数退避×抖动区间(ms)Retry-After(ms)最终延迟时间(指数退避×抖动区间 + Retry-After)
0[30, 60]10[40, 70]
1[60, 120]20[80, 140]
3[120, 240]30[150, 270]
...... ≤ 500ms10≤ 510

开启重试机制

默认

  • 开启(3 次)

如果想关闭,可以把最大尝试次数改为 0。

func main() {
	region := "cn-beijing"
	// Configure retry settings
	config := volcengine.NewConfig().
		WithRegion(region).
		WithDisableSSL(true).
		WithCredentials(credentials.NewEnvCredentials()). //需要配置环境变量
		// 关闭重试
		WithMaxRetries(0)

	sess, err := session.NewSession(config)
	if err != nil {
		panic(err)
	}
	svc := ecs.New(sess)
}

重试次数

默认

  • 3 次

设置最大重试次数:

func main() {
	region := "cn-beijing"
	// Configure retry settings
	config := volcengine.NewConfig().
		WithRegion(region).
		WithDisableSSL(true).
		WithCredentials(credentials.NewEnvCredentials()). //需要配置环境变量
		// 设置最大重试次数 (default is DefaultRetryerMaxNumRetries)
		WithMaxRetries(4)

	sess, err := session.NewSession(config)
	if err != nil {
		panic(err)
	}
	svc := ecs.New(sess)
}

自定义重试错误码

在调用接口的时候可以根据业务需求,自定义重试的错误码(服务端返回的错误码)。公共错误码可以参考:公共错误码

func main() {
	region := "cn-beijing"
	// Configure retry settings
	config := volcengine.NewConfig().
		WithRegion(region).
		WithDisableSSL(true).
		WithCredentials(credentials.NewEnvCredentials()). //需要配置环境变量
		// 设置最大重试次数 (default is DefaultRetryerMaxNumRetries)
		WithMaxRetries(4)

	sess, err := session.NewSession(config)
	if err != nil {
		panic(err)
	}
	svc := ecs.New(sess)
	describeAvailableResourceInput := &ecs.DescribeAvailableResourceInput{
		DestinationResource: volcengine.String("InstanceType"),
		InstanceTypeId:      volcengine.String("ecs.g2i.large"),
		ZoneId:              volcengine.String("cn-*****"),
	}
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	resp, err := svc.DescribeAvailableResourceWithContext(ctx, describeAvailableResourceInput, func(request *request.Request) {
		// 自定义重试错误码
		request.RetryErrorCodes = []string{"InvalidAccessKey"}
	})
	if err != nil {
		panic(err)
	}
}

← 超时配置 | 重试机制(English) | 异常处理 →