From be4d13c0ef3ca354900ec064e2575aa002a05c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Musab=20G=C3=BCltekin?= Date: Wed, 14 Apr 2021 09:32:42 +0300 Subject: [PATCH] Retry checking refactored using util function. --- client/client.go | 12 +++++------- internal/strings.go | 14 ++++++++++++-- middleware/allowed_domains.go | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/client/client.go b/client/client.go index b4b1e4d..4ea3d86 100644 --- a/client/client.go +++ b/client/client.go @@ -105,13 +105,11 @@ func (c *Client) DoRequest(req *Request) (resp *Response, err error) { } // Retry on http status codes - for _, statusCode := range c.opt.RetryHTTPCodes { - if resp.StatusCode == statusCode { - if req.retryCounter < c.opt.RetryTimes { - req.retryCounter++ - internal.Logger.Println("Retrying:", req.URL.String(), resp.StatusCode) - return c.DoRequest(req) - } + if internal.ContainsInt(c.opt.RetryHTTPCodes, resp.StatusCode) { + if req.retryCounter < c.opt.RetryTimes { + req.retryCounter++ + internal.Logger.Println("Retrying:", req.URL.String(), resp.StatusCode) + return c.DoRequest(req) } } diff --git a/internal/strings.go b/internal/strings.go index 8be1945..ffed90e 100644 --- a/internal/strings.go +++ b/internal/strings.go @@ -16,8 +16,18 @@ func DefaultRune(val rune, valDefault rune) rune { return valDefault } -// Contains checks whether []string Contains string -func Contains(s []string, e string) bool { +// ContainsString checks whether []string ContainsString string +func ContainsString(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} + +// ContainsInt checks whether []int contains int +func ContainsInt(s []int, e int) bool { for _, a := range s { if a == e { return true diff --git a/middleware/allowed_domains.go b/middleware/allowed_domains.go index 051c7d9..1ad39d4 100644 --- a/middleware/allowed_domains.go +++ b/middleware/allowed_domains.go @@ -13,7 +13,7 @@ type AllowedDomains struct { } func (a *AllowedDomains) ProcessRequest(r *client.Request) { - if len(a.AllowedDomains) != 0 && !internal.Contains(a.AllowedDomains, r.Host) { + if len(a.AllowedDomains) != 0 && !internal.ContainsString(a.AllowedDomains, r.Host) { if _, logged := a.logOnlyOnce.LoadOrStore(r.Host, struct{}{}); !logged { internal.Logger.Printf("Domain not allowed: %s\n", r.Host) }