mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 22:06:20 +00:00
Run the waited-for function before sleeping, and ramp up the sleep exponentially.
This commit is contained in:
parent
8c33ddbee2
commit
8493d239ac
@ -310,7 +310,6 @@ func (s *TestServer) waitForLeader() {
|
||||
|
||||
// Ensure we have a leader and a node registration.
|
||||
if leader := resp.Header.Get("X-Consul-KnownLeader"); leader != "true" {
|
||||
fmt.Println(leader)
|
||||
return false, fmt.Errorf("Consul leader status: %#v", leader)
|
||||
}
|
||||
index, err = strconv.ParseInt(resp.Header.Get("X-Consul-Index"), 10, 64)
|
||||
|
@ -10,19 +10,28 @@ import (
|
||||
type testFn func() (bool, error)
|
||||
type errorFn func(error)
|
||||
|
||||
func WaitForResult(test testFn, error errorFn) {
|
||||
for retries := 100; retries > 0; retries-- {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
const (
|
||||
baseWait = 1 * time.Millisecond
|
||||
maxWait = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
success, err := test()
|
||||
func WaitForResult(try testFn, fail errorFn) {
|
||||
var err error
|
||||
wait := baseWait
|
||||
for retries := 100; retries > 0; retries-- {
|
||||
var success bool
|
||||
success, err = try()
|
||||
if success {
|
||||
return
|
||||
}
|
||||
|
||||
if retries == 0 {
|
||||
error(err)
|
||||
time.Sleep(wait)
|
||||
wait *= 2
|
||||
if wait > maxWait {
|
||||
wait = maxWait
|
||||
}
|
||||
}
|
||||
fail(err)
|
||||
}
|
||||
|
||||
type rpcFn func(string, interface{}, interface{}) error
|
||||
|
Loading…
x
Reference in New Issue
Block a user