Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default http client timeout override #1013

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 HTTP_TIMEOUT_SECONDS: %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
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