Retry checking refactored using util function.

This commit is contained in:
Musab Gültekin 2021-04-14 09:32:42 +03:00
parent 46c4db6b1a
commit be4d13c0ef
3 changed files with 18 additions and 10 deletions

View File

@ -105,15 +105,13 @@ func (c *Client) DoRequest(req *Request) (resp *Response, err error) {
} }
// Retry on http status codes // Retry on http status codes
for _, statusCode := range c.opt.RetryHTTPCodes { if internal.ContainsInt(c.opt.RetryHTTPCodes, resp.StatusCode) {
if resp.StatusCode == statusCode {
if req.retryCounter < c.opt.RetryTimes { if req.retryCounter < c.opt.RetryTimes {
req.retryCounter++ req.retryCounter++
internal.Logger.Println("Retrying:", req.URL.String(), resp.StatusCode) internal.Logger.Println("Retrying:", req.URL.String(), resp.StatusCode)
return c.DoRequest(req) return c.DoRequest(req)
} }
} }
}
return resp, err return resp, err
} }

View File

@ -16,8 +16,18 @@ func DefaultRune(val rune, valDefault rune) rune {
return valDefault return valDefault
} }
// Contains checks whether []string Contains string // ContainsString checks whether []string ContainsString string
func Contains(s []string, e string) bool { 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 { for _, a := range s {
if a == e { if a == e {
return true return true

View File

@ -13,7 +13,7 @@ type AllowedDomains struct {
} }
func (a *AllowedDomains) ProcessRequest(r *client.Request) { 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 { if _, logged := a.logOnlyOnce.LoadOrStore(r.Host, struct{}{}); !logged {
internal.Logger.Printf("Domain not allowed: %s\n", r.Host) internal.Logger.Printf("Domain not allowed: %s\n", r.Host)
} }