lib: use permanent error instead of context cancellation (#1429)

This commit is contained in:
Ludovic Fernandez 2021-06-11 11:07:38 +02:00 committed by GitHub
parent 9b44e4a262
commit 1dbebce7a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 21 deletions

View file

@ -2,7 +2,6 @@ package api
import ( import (
"bytes" "bytes"
"context"
"crypto" "crypto"
"encoding/json" "encoding/json"
"errors" "errors"
@ -83,8 +82,6 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
bo.MaxInterval = 5 * time.Second bo.MaxInterval = 5 * time.Second
bo.MaxElapsedTime = 20 * time.Second bo.MaxElapsedTime = 20 * time.Second
ctx, cancel := context.WithCancel(context.Background())
var resp *http.Response var resp *http.Response
operation := func() error { operation := func() error {
var err error var err error
@ -96,8 +93,7 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
return err return err
} }
cancel() return backoff.Permanent(err)
return err
} }
return nil return nil
@ -107,7 +103,7 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
log.Infof("retry due to: %v", err) log.Infof("retry due to: %v", err)
} }
err := backoff.RetryNotify(operation, backoff.WithContext(bo, ctx), notify) err := backoff.RetryNotify(operation, bo, notify)
if err != nil { if err != nil {
return resp, err return resp, err
} }

2
go.mod
View file

@ -16,7 +16,7 @@ require (
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.1.0 github.com/akamai/AkamaiOPEN-edgegrid-golang v1.1.0
github.com/aliyun/alibaba-cloud-sdk-go v1.61.976 github.com/aliyun/alibaba-cloud-sdk-go v1.61.976
github.com/aws/aws-sdk-go v1.37.27 github.com/aws/aws-sdk-go v1.37.27
github.com/cenkalti/backoff/v4 v4.1.0 github.com/cenkalti/backoff/v4 v4.1.1
github.com/cloudflare/cloudflare-go v0.14.0 github.com/cloudflare/cloudflare-go v0.14.0
github.com/cpu/goacmedns v0.1.1 github.com/cpu/goacmedns v0.1.1
github.com/dnsimple/dnsimple-go v0.63.0 github.com/dnsimple/dnsimple-go v0.63.0

4
go.sum
View file

@ -78,8 +78,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7FVlAwDAVw= github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7FVlAwDAVw=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc= github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=

View file

@ -2,7 +2,6 @@ package internal
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -125,8 +124,6 @@ func (c Client) GetRootDomain(hostname string) (*DNSHostname, error) {
func (c Client) doRetry(method, uri string, body []byte, data interface{}) error { func (c Client) doRetry(method, uri string, body []byte, data interface{}) error {
var resp *http.Response var resp *http.Response
ctx, cancel := context.WithCancel(context.Background())
operation := func() error { operation := func() error {
var reqBody io.Reader var reqBody io.Reader
if len(body) > 0 { if len(body) > 0 {
@ -147,8 +144,7 @@ func (c Client) doRetry(method, uri string, body []byte, data interface{}) error
} }
if err != nil { if err != nil {
cancel() return backoff.Permanent(fmt.Errorf("client error: %w", err))
return fmt.Errorf("client error: %w", err)
} }
return nil return nil
@ -161,7 +157,7 @@ func (c Client) doRetry(method, uri string, body []byte, data interface{}) error
bo := backoff.NewExponentialBackOff() bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 1 * time.Second bo.InitialInterval = 1 * time.Second
err := backoff.RetryNotify(operation, backoff.WithContext(bo, ctx), notify) err := backoff.RetryNotify(operation, bo, notify)
if err != nil { if err != nil {
return err return err
} }

View file

@ -2,7 +2,6 @@ package hostingde
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -57,15 +56,12 @@ func (d *DNSProvider) updateZone(updateRequest ZoneUpdateRequest) (*ZoneUpdateRe
} }
func (d *DNSProvider) getZone(findRequest ZoneConfigsFindRequest) (*ZoneConfig, error) { func (d *DNSProvider) getZone(findRequest ZoneConfigsFindRequest) (*ZoneConfig, error) {
ctx, cancel := context.WithCancel(context.Background())
var zoneConfig *ZoneConfig var zoneConfig *ZoneConfig
operation := func() error { operation := func() error {
findResponse, err := d.listZoneConfigs(findRequest) findResponse, err := d.listZoneConfigs(findRequest)
if err != nil { if err != nil {
cancel() return backoff.Permanent(err)
return err
} }
if findResponse.Response.Data[0].Status != "active" { if findResponse.Response.Data[0].Status != "active" {
@ -83,7 +79,7 @@ func (d *DNSProvider) getZone(findRequest ZoneConfigsFindRequest) (*ZoneConfig,
bo.MaxElapsedTime = 100 * bo.InitialInterval bo.MaxElapsedTime = 100 * bo.InitialInterval
// retry in case the zone was edited recently and is not yet active // retry in case the zone was edited recently and is not yet active
err := backoff.Retry(operation, backoff.WithContext(bo, ctx)) err := backoff.Retry(operation, bo)
if err != nil { if err != nil {
return nil, err return nil, err
} }