Skip to content

Commit 179d836

Browse files
authored
Add a missing retry in consul client (#16)
1 parent 4cebe43 commit 179d836

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

consulClient.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,24 @@ func (cp *consulPlugin) NewClient(endpoints []string) (API, error) {
7474
// verify we can reach the consul
7575
_, _, err = client.KV().List("/", nil)
7676
if err != nil {
77-
log.Errorf("Error connecting to consul. Err: %v", err)
78-
return nil, err
77+
if api.IsServerError(err) || strings.Contains(err.Error(), "EOF") ||
78+
strings.Contains(err.Error(), "connection refused") {
79+
for i := 0; i < maxConsulRetries; i++ {
80+
_, _, err = client.KV().List("/", nil)
81+
if err == nil {
82+
break
83+
}
84+
85+
// Retry after a delay
86+
time.Sleep(time.Second)
87+
}
88+
}
89+
90+
// return error if it failed after retries
91+
if err != nil {
92+
log.Errorf("Error connecting to consul. Err: %v", err)
93+
return nil, err
94+
}
7995
}
8096

8197
return cc, nil

0 commit comments

Comments
 (0)