Skip to content

Commit 7531873

Browse files
bietkulsiddharthlatest
authored andcommitted
feat: use retrable (#46)
1 parent 63f6d3f commit 7531873

4 files changed

Lines changed: 22 additions & 27 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/gobuffalo/packr v1.22.0
77
github.com/google/uuid v1.0.0
88
github.com/gorilla/mux v1.7.1
9+
github.com/hashicorp/go-retryablehttp v0.6.3
910
github.com/olivere/elastic v6.2.21+incompatible
1011
github.com/olivere/elastic/v7 v7.0.4
1112
github.com/robfig/cron v1.1.0

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ github.com/gorilla/pat v0.0.0-20180118222023-199c85a7f6d1/go.mod h1:YeAe0gNeiNT5
235235
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
236236
github.com/gorilla/sessions v1.1.2/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
237237
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
238+
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
239+
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
240+
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
241+
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
242+
github.com/hashicorp/go-retryablehttp v0.6.3 h1:tuulM+WnToeqa05z83YLmKabZxrySOmJAd4mJ+s2Nfg=
243+
github.com/hashicorp/go-retryablehttp v0.6.3/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
238244
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
239245
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
240246
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=

plugins/elasticsearch/handlers.go

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

98
"github.com/appbaseio/arc/model/acl"
109
"github.com/appbaseio/arc/model/category"
1110
"github.com/appbaseio/arc/model/op"
1211
"github.com/appbaseio/arc/util"
12+
"github.com/hashicorp/go-retryablehttp"
1313
)
1414

1515
func (es *elasticsearch) handler() http.HandlerFunc {
@@ -37,21 +37,22 @@ func (es *elasticsearch) handler() http.HandlerFunc {
3737
return
3838
}
3939
log.Printf(`%s: category="%s", acl="%s", op="%s"\n`, logTag, *reqCategory, *reqACL, *reqOp)
40-
4140
// Forward the request to elasticsearch
42-
client := util.HTTPClient()
41+
client := retryablehttp.NewClient()
4342

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-
})
43+
request, err := retryablehttp.FromRequest(r)
44+
if err != nil {
45+
log.Printf("%s: error while converting to retryable request for %s: %v\n", logTag, r.URL.Path, err)
46+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
47+
return
48+
}
49+
response, err := client.Do(request)
50+
51+
if err != nil {
52+
log.Printf("%s: error fetching response for %s: %v\n", logTag, r.URL.Path, err)
53+
util.WriteBackError(w, err.Error(), http.StatusInternalServerError)
54+
return
55+
}
5556

5657
defer response.Body.Close()
5758

util/util.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,3 @@ func DecodeBase64Key(encoded string) ([]byte, error) {
230230
}
231231
return decoded, nil
232232
}
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)