use exponential back off func from realis lib
This commit is contained in:
parent
23430cbf30
commit
7538f366ea
5 changed files with 317 additions and 444 deletions
32
retry.go
32
retry.go
|
@ -19,14 +19,26 @@ limitations under the License.
|
|||
package realis
|
||||
|
||||
import (
|
||||
"time"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/paypal/gorealis/gen-go/apache/aurora"
|
||||
)
|
||||
|
||||
const (
|
||||
ConnRefusedErr = "connection refused"
|
||||
NoLeaderFoundErr = "No leader found"
|
||||
)
|
||||
|
||||
var RetryConnErr = errors.New("error occured during with aurora retrying")
|
||||
|
||||
// ConditionFunc returns true if the condition is satisfied, or an error
|
||||
// if the loop should be aborted.
|
||||
type ConditionFunc func() (done bool, err error)
|
||||
|
||||
type AuroraFunc func() (resp *aurora.Response, err error)
|
||||
|
||||
// ExponentialBackoff repeats a condition check with exponential backoff.
|
||||
//
|
||||
// It checks the condition up to Steps times, increasing the wait by multiplying
|
||||
|
@ -54,3 +66,21 @@ func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
|
|||
}
|
||||
return errors.New("Timed out while retrying")
|
||||
}
|
||||
|
||||
// CheckAndRetryConn function takes realis client and a trhift API function to call and returns response and error
|
||||
// If Error from the APi call is Retry able . THe functions re establishes the connection with aurora by getting the latest aurora master from zookeeper.
|
||||
// If Error is retyable return resp and RetryConnErr error.
|
||||
func CheckAndRetryConn(r Realis, aurorajob AuroraFunc) (*aurora.Response, error) {
|
||||
resp, cliErr := aurorajob()
|
||||
if strings.Contains(cliErr.Error(), ConnRefusedErr) || strings.Contains(cliErr.Error(), NoLeaderFoundErr) {
|
||||
conErr := r.ReestablishConn()
|
||||
if conErr != nil {
|
||||
return resp, RetryConnErr
|
||||
}
|
||||
return resp, RetryConnErr
|
||||
}
|
||||
if resp != nil && resp.GetResponseCode() != aurora.ResponseCode_ERROR_TRANSIENT {
|
||||
return resp, RetryConnErr
|
||||
}
|
||||
return resp, cliErr
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue