Skip to content

Commit 63f6d3f

Browse files
authored
Merge pull request #45 from appbaseio/hotfix/retry-http-requests
hotfix: add retry logic to ES proxy
2 parents a607132 + dc46647 commit 63f6d3f

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

plugins/elasticsearch/handlers.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"log"
66
"net/http"
7+
"time"
78

89
"github.com/appbaseio/arc/model/acl"
910
"github.com/appbaseio/arc/model/category"
@@ -39,12 +40,19 @@ func (es *elasticsearch) handler() http.HandlerFunc {
3940

4041
// Forward the request to elasticsearch
4142
client := util.HTTPClient()
42-
response, err := client.Do(r)
43-
if err != nil {
44-
log.Printf("%s: error fetching response for %s: %v\n", logTag, r.URL.Path, err)
45-
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
46-
return
47-
}
43+
44+
var response *http.Response
45+
util.Retry(3, 100*time.Millisecond, func() bool {
46+
response, err = client.Do(r)
47+
if err != nil {
48+
log.Printf("%s: error fetching response for %s: %v\n", logTag, r.URL.Path, err)
49+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
50+
return false
51+
}
52+
err = nil
53+
return true
54+
})
55+
4856
defer response.Body.Close()
4957

5058
// Copy the headers

util/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func IntervalForRange(from, to string) (string, error) {
219219
return fmt.Sprintf("%ds", int64(intervalInSecs)), nil
220220
}
221221

222+
// DecodeBase64Key decodes a base64 input
222223
func DecodeBase64Key(encoded string) ([]byte, error) {
223224
decoded, err := base64.StdEncoding.DecodeString(encoded)
224225
if err != nil {
@@ -229,3 +230,16 @@ func DecodeBase64Key(encoded string) ([]byte, error) {
229230
}
230231
return decoded, nil
231232
}
233+
234+
// Retry is a general purpose retrier for a function call
235+
func Retry(numberRetries int, sleepInterval time.Duration, testFunc func() bool) {
236+
executionPassed := false
237+
executionCount := 0
238+
for !executionPassed && executionCount < numberRetries {
239+
executionPassed = testFunc()
240+
if !executionPassed {
241+
executionCount++
242+
time.Sleep(sleepInterval)
243+
}
244+
}
245+
}

0 commit comments

Comments
 (0)