Factors server error checking into a new function.
This commit is contained in:
parent
cab27440e1
commit
dd4610e917
15
api/api.go
15
api/api.go
|
@ -273,6 +273,21 @@ func durToMsec(dur time.Duration) string {
|
||||||
return fmt.Sprintf("%dms", ms)
|
return fmt.Sprintf("%dms", ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serverError is a string we look for to detect 500 errors.
|
||||||
|
const serverError = "Unexpected response code: 500"
|
||||||
|
|
||||||
|
// IsServerError returns true for 500 errors from the Consul servers, these are
|
||||||
|
// usually retryable at a later time.
|
||||||
|
func IsServerError(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO (slackpad) - Make a real error type here instead of using
|
||||||
|
// a string check.
|
||||||
|
return strings.Contains(err.Error(), serverError)
|
||||||
|
}
|
||||||
|
|
||||||
// setWriteOptions is used to annotate the request with
|
// setWriteOptions is used to annotate the request with
|
||||||
// additional write options
|
// additional write options
|
||||||
func (r *request) setWriteOptions(q *WriteOptions) {
|
func (r *request) setWriteOptions(q *WriteOptions) {
|
||||||
|
|
|
@ -272,3 +272,17 @@ func TestAPI_durToMsec(t *testing.T) {
|
||||||
t.Fatalf("bad: %s", ms)
|
t.Fatalf("bad: %s", ms)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAPI_IsServerError(t *testing.T) {
|
||||||
|
if IsServerError(nil) {
|
||||||
|
t.Fatalf("should not be a server error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsServerError(fmt.Errorf("not the error you are looking for")) {
|
||||||
|
t.Fatalf("should not be a server error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !IsServerError(fmt.Errorf(serverError)) {
|
||||||
|
t.Fatalf("should be a server error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -362,15 +361,11 @@ WAIT:
|
||||||
RETRY:
|
RETRY:
|
||||||
pair, meta, err := kv.Get(l.opts.Key, opts)
|
pair, meta, err := kv.Get(l.opts.Key, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO (slackpad) - Make a real error type here instead of using
|
|
||||||
// a string check.
|
|
||||||
const serverError = "Unexpected response code: 500"
|
|
||||||
|
|
||||||
// If configured we can try to ride out a brief Consul unavailability
|
// If configured we can try to ride out a brief Consul unavailability
|
||||||
// by doing retries. Note that we have to attempt the retry in a non-
|
// by doing retries. Note that we have to attempt the retry in a non-
|
||||||
// blocking fashion so that we have a clean place to reset the retry
|
// blocking fashion so that we have a clean place to reset the retry
|
||||||
// counter if service is restored.
|
// counter if service is restored.
|
||||||
if retries > 0 && strings.Contains(err.Error(), serverError) {
|
if retries > 0 && IsServerError(err) {
|
||||||
time.Sleep(l.opts.MonitorRetryTime)
|
time.Sleep(l.opts.MonitorRetryTime)
|
||||||
retries--
|
retries--
|
||||||
opts.WaitIndex = 0
|
opts.WaitIndex = 0
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -488,15 +487,11 @@ WAIT:
|
||||||
RETRY:
|
RETRY:
|
||||||
pairs, meta, err := kv.List(s.opts.Prefix, opts)
|
pairs, meta, err := kv.List(s.opts.Prefix, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO (slackpad) - Make a real error type here instead of using
|
|
||||||
// a string check.
|
|
||||||
const serverError = "Unexpected response code: 500"
|
|
||||||
|
|
||||||
// If configured we can try to ride out a brief Consul unavailability
|
// If configured we can try to ride out a brief Consul unavailability
|
||||||
// by doing retries. Note that we have to attempt the retry in a non-
|
// by doing retries. Note that we have to attempt the retry in a non-
|
||||||
// blocking fashion so that we have a clean place to reset the retry
|
// blocking fashion so that we have a clean place to reset the retry
|
||||||
// counter if service is restored.
|
// counter if service is restored.
|
||||||
if retries > 0 && strings.Contains(err.Error(), serverError) {
|
if retries > 0 && IsServerError(err) {
|
||||||
time.Sleep(s.opts.MonitorRetryTime)
|
time.Sleep(s.opts.MonitorRetryTime)
|
||||||
retries--
|
retries--
|
||||||
opts.WaitIndex = 0
|
opts.WaitIndex = 0
|
||||||
|
|
Loading…
Reference in New Issue