Skip to content

Commit

Permalink
通用重试逻辑,当请求无返回时自动重试
Browse files Browse the repository at this point in the history
  • Loading branch information
zshongyi committed Jan 4, 2025
1 parent 076822b commit 0bf5c5f
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions alibabacloudstack/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ type AlibabacloudStackClient struct {
bssopenapiconn *bssopenapi.Client
rdsconn *rds.Client
ramconn *ram.Client
essconn *ess.Client
gpdbconn *gpdb.Client
drdsconn *drds.Client
elasticsearchconn *elasticsearch.Client
Expand Down Expand Up @@ -275,16 +274,16 @@ func (client *AlibabacloudStackClient) WithCloudApiClient(do func(*cloudapi.Clie
}

func (client *AlibabacloudStackClient) WithEssClient(do func(*ess.Client) (interface{}, error)) (interface{}, error) {
if client.essconn == nil {
conn, error := client.WithProductSDKClient(ESSCode)
if error != nil {
return nil, error
}
client.essconn = &ess.Client{
Client: *conn,
}
conn, err := client.getConnectClient("ESS")
if err != nil {
return nil, err
}
essconn := &ess.Client{
Client: *conn,
}
return do(client.essconn)
return retryDo(func() (interface{}, error) {
return do(essconn)
})
}

func (client *AlibabacloudStackClient) WithOnsClient(do func(*ons.Client) (interface{}, error)) (interface{}, error) {
Expand Down Expand Up @@ -1396,3 +1395,23 @@ func SetResourceData(d *schema.ResourceData, value interface{}, keys ...string)
}
return nil
}

func retryDo(do func() (interface{}, error)) (interface{}, error) {
var response interface{}
var err error
wait := IncrementalWait(3*time.Second, 3*time.Second)
resource.Retry(5*time.Minute, func() *resource.RetryError {
//仅在请求无正常返回时重试
response, err = do()
if err == nil {
return nil
}
if response == nil {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)

})
return response, err
}

0 comments on commit 0bf5c5f

Please sign in to comment.