This commit is contained in:
2023-12-21 22:56:24 +03:00
parent 85a42dc405
commit 0bebf8ba8a
7 changed files with 111 additions and 11 deletions

View File

@ -24,7 +24,7 @@ func (c *Client) GetBlockGoodsCategory(ctx context.Context, idC int, idR int, so
dump, err := httputil.DumpRequestOut(req, false)
log.Printf("%s\n", string(dump))
resp, err := c.httpCli.Do(req)
resp, err := c.doWithRetry(req, 200)
if err != nil {
return nil, fmt.Errorf("http do: %w", err)
}
@ -46,6 +46,32 @@ func (c *Client) GetBlockGoodsCategory(ctx context.Context, idC int, idR int, so
return goods, nil
}
func (c *Client) doWithRetry(req *http.Request, expectedCode int) (*http.Response, error) {
attempt := 0
maxAttempts := 5
for {
resp, err := c.httpCli().Do(req)
if err != nil {
if attempt < maxAttempts-1 {
continue
} else {
return resp, err
}
}
if resp.StatusCode != expectedCode {
if attempt < maxAttempts-1 {
resp.Body.Close()
continue
} else {
return resp, err
}
}
return resp, nil
}
}
type Good struct {
Name string
GoodLink string