Skip to content

Commit

Permalink
Merge pull request #1013 from or-shachar/f-1012
Browse files Browse the repository at this point in the history
feat: default http client timeout override
  • Loading branch information
abdelDriowya authored Nov 7, 2023
2 parents c16c285 + 9dc48a4 commit 43878aa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/header/global.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package header

import "time"

// When the CLI is initialized, we set this to a string of the current CLI subcommand
// (e.g. `circleci orb list`) with no args or flags, so we include it as a header in API requests.
var cliCommandStr string = ""
Expand All @@ -11,3 +13,9 @@ func SetCommandStr(commandStr string) {
func GetCommandStr() string {
return cliCommandStr
}

const defaultTimeout = 60 * time.Second

func GetDefaultTimeout() time.Duration {
return defaultTimeout
}
11 changes: 10 additions & 1 deletion api/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -38,9 +39,17 @@ func NewFromConfig(host string, config *settings.Config) *Client {
}

baseURL, _ := url.Parse(host)
timeout := header.GetDefaultTimeout()
if timeoutEnv, ok := os.LookupEnv("CIRCLECI_CLI_TIMEOUT"); ok {
if parsedTimeout, err := time.ParseDuration(timeoutEnv); err == nil {
timeout = parsedTimeout
} else {
fmt.Printf("failed to parse CIRCLECI_CLI_TIMEOUT: %s\n", err.Error())
}
}

client := config.HTTPClient
client.Timeout = 10 * time.Second
client.Timeout = timeout

return New(
baseURL.ResolveReference(&url.URL{Path: endpoint}),
Expand Down
22 changes: 22 additions & 0 deletions api/rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/CircleCI-Public/circleci-cli/api/header"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/version"
)

func TestNewFromConfigTimeout(t *testing.T) {
cfg := &settings.Config{
Debug: false,
Token: "fake-token",
RestEndpoint: "api/v2",
Endpoint: "api/v2",
HTTPClient: http.DefaultClient,
}
t.Run("create new client without custom timeout", func(t *testing.T) {
api := NewFromConfig("host", cfg)
assert.Equal(t, api.client.Timeout, header.GetDefaultTimeout())
})
t.Run("create new client with custom timeout", func(t *testing.T) {
customTimeout := 20 * time.Second
os.Setenv("CIRCLECI_CLI_TIMEOUT", customTimeout.String())
api := NewFromConfig("host", cfg)
assert.Equal(t, api.client.Timeout, customTimeout)
})
}
func TestClient_DoRequest(t *testing.T) {
t.Run("PUT with req and resp", func(t *testing.T) {
fix := &fixture{}
Expand Down
3 changes: 2 additions & 1 deletion settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

yaml "gopkg.in/yaml.v3"

"github.com/CircleCI-Public/circleci-cli/api/header"
"github.com/CircleCI-Public/circleci-cli/data"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -287,7 +288,7 @@ func (cfg *Config) WithHTTPClient() error {
customTransport.TLSClientConfig = tlsConfig

cfg.HTTPClient = &http.Client{
Timeout: 60 * time.Second,
Timeout: header.GetDefaultTimeout(),
Transport: customTransport,
}

Expand Down

0 comments on commit 43878aa

Please sign in to comment.