Skip to content

Commit 160e6c3

Browse files
henrybarretogustavosbarreto
authored andcommitted
feat(pkg): add retry wait time options on internal http client
1 parent 09617ad commit 160e6c3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pkg/api/internalclient/client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net"
66
"net/http"
7+
"time"
78

89
resty "github.com/go-resty/resty/v2"
910
"github.com/shellhub-io/shellhub/pkg/worker"
@@ -23,6 +24,10 @@ type Client interface {
2324
type Config struct {
2425
// RetryCount defines how many times the client should retry a request in case of failure.
2526
RetryCount int
27+
// RetryWaitTime defines the wait time between retries.
28+
RetryWaitTime time.Duration
29+
// RetryMaxWaitTime defines the maximum wait time between retries.
30+
RetryMaxWaitTime time.Duration
2631
}
2732

2833
type client struct {
@@ -50,7 +55,10 @@ func NewClient(opts ...clientOption) (Client, error) {
5055
c := &client{
5156
http: httpClient,
5257
Config: &Config{
53-
RetryCount: 3,
58+
// NOTE: Default values can be overridden using the WithConfig option.
59+
RetryCount: 3,
60+
RetryWaitTime: 5 * time.Second,
61+
RetryMaxWaitTime: 20 * time.Second,
5462
},
5563
}
5664

@@ -66,6 +74,8 @@ func NewClient(opts ...clientOption) (Client, error) {
6674

6775
httpClient.SetBaseURL("http://api:8080")
6876
httpClient.SetRetryCount(c.Config.RetryCount)
77+
httpClient.SetRetryWaitTime(c.Config.RetryWaitTime)
78+
httpClient.SetRetryMaxWaitTime(c.Config.RetryMaxWaitTime)
6979
httpClient.AddRetryCondition(func(r *resty.Response, err error) bool {
7080
if _, ok := err.(net.Error); ok { // if the error is a network error, retry.
7181
return true

0 commit comments

Comments
 (0)