Skip to content

Commit

Permalink
feat: use retriable http client instaed of standard http
Browse files Browse the repository at this point in the history
Signed-off-by: nabil salah <[email protected]>
  • Loading branch information
Nabil-Salah committed Jan 1, 2025
1 parent 19e423d commit 6f88363
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 59 deletions.
9 changes: 5 additions & 4 deletions client/_http/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"path/filepath"

"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/threefoldtech/zos4/pkg/gridtypes"
)
Expand Down Expand Up @@ -85,7 +86,7 @@ func (n *NodeClient) Deploy(dl *gridtypes.Deployment, update bool) error {
return errors.Wrap(err, "failed to sign request")
}

response, err := http.DefaultClient.Do(request)
response, err := retryablehttp.NewClient().Do(request)
if err != nil {
return err
}
Expand All @@ -110,7 +111,7 @@ func (n *NodeClient) Get(twin, deployment uint32) (dl gridtypes.Deployment, err
return dl, errors.Wrap(err, "failed to sign request")
}

response, err := http.DefaultClient.Do(request)
response, err := retryablehttp.NewClient().Do(request)
if err != nil {
return dl, err
}
Expand All @@ -135,7 +136,7 @@ func (n *NodeClient) Delete(twin, deployment uint32) (err error) {
return errors.Wrap(err, "failed to sign request")
}

response, err := http.DefaultClient.Do(request)
response, err := retryablehttp.NewClient().Do(request)
if err != nil {
return err
}
Expand All @@ -155,7 +156,7 @@ func (n *NodeClient) Counters() (total gridtypes.Capacity, used gridtypes.Capaci
return total, used, errors.Wrap(err, "failed to build request")
}

response, err := http.DefaultClient.Do(request)
response, err := retryablehttp.NewClient().Do(request)
if err != nil {
return total, used, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmds/identityd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/cenkalti/backoff"
"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zos4/pkg"
"github.com/threefoldtech/zos4/pkg/environment"
Expand Down Expand Up @@ -72,7 +73,7 @@ func manageSSHKeys() error {

for _, user := range authorizedUsers {
fetchKey := func() error {
res, err := http.Get(fmt.Sprintf("https://github.com/%s.keys", user))
res, err := retryablehttp.Get(fmt.Sprintf("https://github.com/%s.keys", user))

if err != nil {
return fmt.Errorf("failed to fetch user keys: %+w", err)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ require (
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hanwen/go-fuse/v2 v2.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/klauspost/compress v1.16.7 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,12 @@ github.com/hanwen/go-fuse/v2 v2.3.0/go.mod h1:xKwi1cF7nXAOBCXujD5ie0ZKsxc8GGSA1r
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
Expand Down
9 changes: 5 additions & 4 deletions pkg/environment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -51,11 +52,11 @@ func GetConfig() (base Config, err error) {

// GetConfig returns extend config for specific run mode
func GetConfigForMode(mode RunMode) (Config, error) {
httpClient := &http.Client{
Timeout: defaultHttpTimeout,
}
httpClient := retryablehttp.NewClient()
httpClient.RetryMax = 5
httpClient.HTTPClient.Timeout = defaultHttpTimeout

return getConfig(mode, baseExtendedURL, httpClient)
return getConfig(mode, baseExtendedURL, httpClient.StandardClient())
}

func uniqueStr(slice []string) []string {
Expand Down
24 changes: 13 additions & 11 deletions pkg/flist/flist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zos4/pkg"
Expand Down Expand Up @@ -125,7 +126,7 @@ type flistModule struct {
commander commander
system system

httpClient *http.Client
httpClient *retryablehttp.Client
}

func newFlister(root string, storage volumeAllocator, commander commander, system system) *flistModule {
Expand Down Expand Up @@ -154,6 +155,10 @@ func newFlister(root string, storage volumeAllocator, commander commander, syste
}
}

httpClient := retryablehttp.NewClient()
httpClient.HTTPClient.Timeout = defaultHubCallTimeout
httpClient.RetryMax = 5

return &flistModule{
root: root,
flist: filepath.Join(root, "flist"),
Expand All @@ -167,9 +172,7 @@ func newFlister(root string, storage volumeAllocator, commander commander, syste
commander: commander,
system: system,

httpClient: &http.Client{
Timeout: defaultHubCallTimeout,
},
httpClient: httpClient,
}
}

Expand Down Expand Up @@ -725,13 +728,12 @@ func (f *flistModule) downloadInNamespace(name, u string) (resp *http.Response,
if err != nil {
return errors.Wrap(err, "failed to start tcp connection")
}

cl := http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return con, nil
},
cl:= retryablehttp.NewClient()
cl.RetryMax = 5
cl.HTTPClient.Transport = &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return con, nil
},
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/gateway/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zos4/pkg"
Expand Down Expand Up @@ -139,12 +140,11 @@ func metrics(rawUrl string) (map[string]*metric, error) {

defer con.Close()

cl := http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return con, nil
},
cl := retryablehttp.NewClient()
cl.HTTPClient.Transport = &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return con, nil
},
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/geoip/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package geoip
import (
"encoding/json"
"errors"
"net/http"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog/log"
)

Expand All @@ -22,9 +22,7 @@ type Location struct {
var (
geoipURLs = []string{"https://geoip.grid.tf/", "https://02.geoip.grid.tf/", "https://03.geoip.grid.tf/"}

defaultHttpClient = &http.Client{
Timeout: 10 * time.Second,
}
defaultHttpClient = retryablehttp.NewClient()
)

// Fetch retrieves the location of the system calling this function
Expand Down Expand Up @@ -52,6 +50,8 @@ func getLocation(geoIPService string) (Location, error) {
City: "Unknown",
}

defaultHttpClient.RetryMax = 5
defaultHttpClient.HTTPClient.Timeout = 10 * time.Second
resp, err := defaultHttpClient.Get(geoIPService)
if err != nil {
return l, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/perf/publicip/publicip_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cenkalti/backoff/v3"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zos4/pkg/environment"
Expand Down Expand Up @@ -266,12 +267,11 @@ func getRealPublicIP() (net.IP, error) {

defer con.Close()

cl := http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return con, nil
},
cl := retryablehttp.NewClient()
cl.HTTPClient.Transport = &http.Transport{
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return con, nil
},
}
response, err := cl.Get("https://api.ipify.org/")
Expand Down
8 changes: 4 additions & 4 deletions pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/cenkalti/backoff/v3"
"github.com/hashicorp/go-retryablehttp"
"github.com/joncrlsn/dque"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -1210,11 +1211,10 @@ func isTwinVerified(twinID uint32) (verified bool, err error) {
q.Set("twin_id", fmt.Sprint(twinID))
request.URL.RawQuery = q.Encode()

cl := &http.Client{
Timeout: 10 * time.Second,
}
cl := retryablehttp.NewClient()
cl.HTTPClient.Timeout = 10* time.Second

response, err := cl.Do(request)
response, err := cl.StandardClient().Do(request)
if err != nil {
return
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/upgrade/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/0-fs/meta"
Expand Down Expand Up @@ -65,15 +66,16 @@ func MatchType(typ FListType) FListFilter {

// HubClient API for f-list
type HubClient struct {
httpClient *http.Client
httpClient *retryablehttp.Client
}

// NewHubClient create new hub client with the passed option for the http client
func NewHubClient(timeout time.Duration) *HubClient {
httpClient := retryablehttp.NewClient()
httpClient.RetryMax = 5
httpClient.HTTPClient.Timeout = timeout
return &HubClient{
httpClient: &http.Client{
Timeout: timeout,
},
httpClient: httpClient,
}
}

Expand Down Expand Up @@ -295,9 +297,9 @@ func (b *Regular) Files(repo string) ([]FileInfo, error) {
}

u.Path = filepath.Join("api", "flist", repo, b.Name)
cl := &http.Client{
Timeout: defaultHubCallTimeout,
}
cl := retryablehttp.NewClient()
cl.RetryMax = 5
cl.HTTPClient.Timeout = defaultHubCallTimeout

response, err := cl.Get(u.String())
if err != nil {
Expand Down
26 changes: 14 additions & 12 deletions pkg/vm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"net"
"net/http"

"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
)

// Client to a cloud hypervisor instance
type Client struct {
client http.Client
client *retryablehttp.Client
}

type VMData struct {
Expand All @@ -24,15 +25,16 @@ type VMData struct {

// NewClient creates a new instance of client
func NewClient(unix string) *Client {
client := Client{
client: http.Client{
Transport: &http.Transport{
Dial: func(network, _ string) (net.Conn, error) {
return net.Dial("unix", unix)
},
},
httpClient := retryablehttp.NewClient()
httpClient.RetryMax = 5
httpClient.HTTPClient.Transport = &http.Transport{
Dial: func(network, _ string) (net.Conn, error) {
return net.Dial("unix", unix)
},
}
client := Client{
client: httpClient,
}

return &client
}
Expand All @@ -43,7 +45,7 @@ func (c *Client) Shutdown(ctx context.Context) error {
if err != nil {
return err
}
response, err := c.client.Do(request)
response, err := c.client.StandardClient().Do(request)
if err != nil {
return errors.Wrap(err, "error calling machine shutdown")
}
Expand All @@ -61,7 +63,7 @@ func (c *Client) Pause(ctx context.Context) error {
if err != nil {
return err
}
response, err := c.client.Do(request)
response, err := c.client.StandardClient().Do(request)
if err != nil {
return errors.Wrap(err, "error calling machine pause")
}
Expand All @@ -79,7 +81,7 @@ func (c *Client) Resume(ctx context.Context) error {
if err != nil {
return err
}
response, err := c.client.Do(request)
response, err := c.client.StandardClient().Do(request)
if err != nil {
return errors.Wrap(err, "error calling machine pause")
}
Expand All @@ -100,7 +102,7 @@ func (c *Client) Inspect(ctx context.Context) (VMData, error) {
}
request.Header.Add("content-type", "application/json")

response, err := c.client.Do(request)
response, err := c.client.StandardClient().Do(request)
if err != nil {
return VMData{}, errors.Wrap(err, "error calling machine info")
}
Expand Down

0 comments on commit 6f88363

Please sign in to comment.