Skip to content

Commit

Permalink
Merge pull request #864 from Shopify/remove_arbitrary_timeouts
Browse files Browse the repository at this point in the history
Taking out my arbitrary timeouts
  • Loading branch information
Tim Anema authored Dec 3, 2020
2 parents f5fd478 + 7fd8d33 commit 4e9ac30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v1.1.4
======
- Fix for theme get --list (#862)
- Removed too restrictive timeouts (#864)

v1.1.3 (Dec 2, 2020)
====================
Expand Down
42 changes: 14 additions & 28 deletions src/httpify/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package httpify

import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"runtime"
Expand All @@ -23,29 +21,11 @@ var (
ErrConnectionIssue = errors.New("DNS problem while connecting to Shopify, this indicates a problem with your internet connection")
// ErrInvalidProxyURL is returned if a proxy url has been passed but is improperly formatted
ErrInvalidProxyURL = errors.New("invalid proxy URI")
netDialer = &net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 1 * time.Second,
}
httpTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
IdleConnTimeout: time.Second,
TLSHandshakeTimeout: time.Second,
ExpectContinueTimeout: time.Second,
ResponseHeaderTimeout: time.Second,
MaxIdleConnsPerHost: 10,
DialContext: func(ctx context.Context, network, address string) (conn net.Conn, err error) {
if conn, err = netDialer.DialContext(ctx, network, address); err != nil {
return nil, err
}
deadline := time.Now().Add(5 * time.Second)
conn.SetReadDeadline(deadline)
return conn, conn.SetDeadline(deadline)
},
httpTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient = &http.Client{
Transport: httpTransport,
Timeout: 30 * time.Second,
Timeout: 30 * time.Second,
}
)

Expand Down Expand Up @@ -87,6 +67,7 @@ func NewClient(params Params) (*HTTPClient, error) {
return nil, ErrInvalidProxyURL
}
httpTransport.Proxy = http.ProxyURL(parsedURL)
httpClient.Transport = httpTransport
}

return &HTTPClient{
Expand Down Expand Up @@ -139,8 +120,12 @@ func (client *HTTPClient) do(method, path string, body interface{}, headers map[
}

func (client *HTTPClient) doWithRetry(req *http.Request, body interface{}) (*http.Response, error) {
var bodyData []byte
var err error
var (
bodyData []byte
resp *http.Response
err error
)

if body != nil {
bodyData, err = json.Marshal(body)
if err != nil {
Expand All @@ -149,15 +134,16 @@ func (client *HTTPClient) doWithRetry(req *http.Request, body interface{}) (*htt
}

for attempt := 0; attempt <= client.maxRetry; attempt++ {
resp, err := client.limit.GateReq(httpClient, req, bodyData)
resp, err = client.limit.GateReq(httpClient, req, bodyData)
if err == nil && resp.StatusCode >= 100 && resp.StatusCode < 500 {
return resp, nil
} else if strings.Contains(err.Error(), "no such host") {
} else if err != nil && strings.Contains(err.Error(), "no such host") {
return nil, ErrConnectionIssue
}
time.Sleep(time.Duration(attempt) * time.Second)
}
return nil, fmt.Errorf("request failed after %v retries", client.maxRetry)

return nil, fmt.Errorf("request failed after %v retries with error: %v", client.maxRetry, err)
}

func parseBaseURL(domain string) (*url.URL, error) {
Expand Down

0 comments on commit 4e9ac30

Please sign in to comment.