-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from maxlaverse/log_retries
log request retries
- Loading branch information
Showing
3 changed files
with
92 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package webapi | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
func CustomRetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error) { | ||
debugInfo := map[string]interface{}{ | ||
"error": err, | ||
} | ||
if resp != nil { | ||
debugInfo["status_code"] = resp.StatusCode | ||
debugInfo["status_message"] = resp.Status | ||
} | ||
|
||
var willRetry bool | ||
var handlerErr error | ||
|
||
if err != nil { | ||
if err == context.DeadlineExceeded { | ||
willRetry = false | ||
} else if netErr, ok := err.(net.Error); ok && netErr.Timeout() { | ||
willRetry = false | ||
} | ||
} else { | ||
willRetry, handlerErr = retryablehttp.DefaultRetryPolicy(ctx, resp, err) | ||
} | ||
|
||
debugInfo["will_retry"] = willRetry | ||
debugInfo["handler_error"] = handlerErr | ||
tflog.Trace(ctx, "retry_handler", debugInfo) | ||
|
||
return willRetry, handlerErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package webapi | ||
|
||
import "net/http" | ||
|
||
type Options func(c Client) | ||
|
||
func DisableRetries() Options { | ||
return func(c Client) { | ||
c.(*client).httpClient.RetryMax = 0 | ||
} | ||
} | ||
|
||
func WithCustomClient(httpClient http.Client) Options { | ||
return func(c Client) { | ||
c.(*client).httpClient.HTTPClient = &httpClient | ||
} | ||
} | ||
|
||
func WithDeviceIdentifier(deviceIdentifier string) Options { | ||
return func(c Client) { | ||
c.(*client).deviceIdentifier = deviceIdentifier | ||
} | ||
} |