retry with 504 status code (timeouts) (#2852)

* Incase of 504/timeouts, do retry

* Update the version and pass the custom policy from outside
dev
Jaideep Khandelwal 2022-12-12 16:43:21 +05:30 committed by GitHub
parent 8a75cb2574
commit 19b56570b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package nucleicloud
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
@ -27,10 +28,21 @@ const (
resultSize = 100
)
// HTTPErrorRetryPolicy is to retry for HTTPCodes >= 500.
func HTTPErrorRetryPolicy() func(ctx context.Context, resp *http.Response, err error) (bool, error) {
return func(ctx context.Context, resp *http.Response, err error) (bool, error) {
if resp.StatusCode >= http.StatusInternalServerError {
return true, errors.New(resp.Status)
}
return retryablehttp.CheckRecoverableErrors(ctx, resp, err)
}
}
// New returns a nuclei-cloud API client
func New(baseURL, apiKey string) *Client {
options := retryablehttp.DefaultOptionsSingle
options.Timeout = 15 * time.Second
options.CheckRetry = HTTPErrorRetryPolicy()
client := retryablehttp.NewClient(options)
baseAppURL := baseURL