Skip to content

Commit

Permalink
Add exponential backoff for specific JSON RPC errors
Browse files Browse the repository at this point in the history
Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Sep 26, 2023
1 parent 7a13658 commit 47f5c13
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
59 changes: 39 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"time"

"github.com/cenkalti/backoff"
gorillawebsocket "github.com/gorilla/websocket"
"github.com/sourcegraph/jsonrpc2"
"github.com/sourcegraph/jsonrpc2/websocket"
Expand Down Expand Up @@ -227,33 +228,51 @@ func NewClient(config Config) (XOClient, error) {
}, nil
}

func (c *Client) Call(method string, params, result interface{}, opt ...jsonrpc2.CallOption) error {
err := c.rpc.Call(context.Background(), method, params, result, opt...)
var callRes interface{}
t := reflect.TypeOf(result)
if t == nil || t.Kind() != reflect.Ptr {
callRes = result
} else {
callRes = reflect.ValueOf(result).Elem()
func IsRetryableError(err jsonrpc2.Error) bool {
if err.Code == 11 {
return true
}
log.Printf("[TRACE] Made rpc call `%s` with params: %v and received %+v: result with error: %v\n", method, params, callRes, err)

if err != nil {
rpcErr, ok := err.(*jsonrpc2.Error)
return false
}

if !ok {
return err
func (c *Client) Call(method string, params, result interface{}) error {
operation := func() error {
err := c.rpc.Call(context.Background(), method, params, result)
var callRes interface{}
t := reflect.TypeOf(result)
if t == nil || t.Kind() != reflect.Ptr {
callRes = result
} else {
callRes = reflect.ValueOf(result).Elem()
}
log.Printf("[TRACE] Made rpc call `%s` with params: %v and received %+v: result with error: %v\n", method, params, callRes, err)

data := rpcErr.Data
if err != nil {
rpcErr, ok := err.(*jsonrpc2.Error)

if data == nil {
return err
}
if !ok {
return backoff.PermanentError{err}

Check failure on line 254 in client/client.go

View workflow job for this annotation

GitHub Actions / Build (1.16, ubuntu-latest)

cannot use backoff.PermanentError{...} (type backoff.PermanentError) as type error in return argument:
}

return errors.New(fmt.Sprintf("%s: %s", err, *data))
if IsRetryableError(rpcErr) {

Check failure on line 257 in client/client.go

View workflow job for this annotation

GitHub Actions / Build (1.16, ubuntu-latest)

cannot use rpcErr (type *jsonrpc2.Error) as type jsonrpc2.Error in argument to IsRetryableError
return err
}

data := rpcErr.Data

if data == nil {
return backoff.PermanentError{err}

Check failure on line 264 in client/client.go

View workflow job for this annotation

GitHub Actions / Build (1.16, ubuntu-latest)

cannot use backoff.PermanentError{...} (type backoff.PermanentError) as type error in return argument:
}

return backoff.PermanentError{errors.New(fmt.Sprintf("%s: %s", err, *data))}

Check failure on line 267 in client/client.go

View workflow job for this annotation

GitHub Actions / Build (1.16, ubuntu-latest)

cannot use backoff.PermanentError{...} (type backoff.PermanentError) as type error in return argument:
}
return nil
}
return nil

bo := backoff.NewExponentialBackOff()
// TODO(ddelnano): See if we need to update from the default 15 mins
// bo.MaxElapsedTime = maxElapsedTime
return backoff.Retry(operation, bo)
}

type RefreshComparison interface {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ddelnano/terraform-provider-xenorchestra
go 1.16

require (
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-00010101000000-000000000000
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/cenkalti/backoff/v3 v3.2.2 h1:cfUAAO3yvKMYKPrvhDuHSwQnhZNk/RMHKdZqKTxfm6M=
github.com/cenkalti/backoff/v3 v3.2.2/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down

0 comments on commit 47f5c13

Please sign in to comment.