Skip to content

Commit

Permalink
Fixes vercel#2280 adds a client timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tm1000 committed Oct 27, 2022
1 parent 19ccc18 commit 22f76a7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cli/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type RemoteConfig struct {
TeamID string
TeamSlug string
APIURL string
Timeout uint64
}

// Opts holds values for configuring the behavior of the API client
Expand All @@ -74,7 +75,7 @@ func NewClient(remoteConfig RemoteConfig, logger hclog.Logger, turboVersion stri
turboVersion: turboVersion,
HttpClient: &retryablehttp.Client{
HTTPClient: &http.Client{
Timeout: time.Duration(20 * time.Second),
Timeout: time.Duration(time.Duration(remoteConfig.Timeout) * time.Second),
},
RetryWaitMin: 2 * time.Second,
RetryWaitMax: 10 * time.Second,
Expand Down
4 changes: 4 additions & 0 deletions cli/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Test_sendToServer(t *testing.T) {
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Timeout: 20,
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})

Expand Down Expand Up @@ -89,6 +90,7 @@ func Test_PutArtifact(t *testing.T) {
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Timeout: 20,
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
expectedArtifactBody := []byte("My string artifact")
Expand All @@ -115,6 +117,7 @@ func Test_PutWhenCachingDisabled(t *testing.T) {
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Timeout: 20,
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
expectedArtifactBody := []byte("My string artifact")
Expand Down Expand Up @@ -142,6 +145,7 @@ func Test_FetchWhenCachingDisabled(t *testing.T) {
TeamSlug: "my-team-slug",
APIURL: ts.URL,
Token: "my-token",
Timeout: 20,
}
apiClient := NewClient(remoteConfig, hclog.Default(), "v1", Opts{})
// Test Put Artifact
Expand Down
8 changes: 8 additions & 0 deletions cli/internal/config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (rc *RepoConfig) GetRemoteConfig(token string) client.RemoteConfig {
TeamID: rc.repoViper.GetString("teamid"),
TeamSlug: rc.repoViper.GetString("teamslug"),
APIURL: rc.repoViper.GetString("apiurl"),
Timeout: rc.repoViper.GetUint64("timeout"),
}
}

Expand Down Expand Up @@ -128,6 +129,7 @@ func DefaultUserConfigPath() turbopath.AbsoluteSystemPath {

const (
_defaultAPIURL = "https://vercel.com/api"
_defaultTimeout = uint64(20)
_defaultLoginURL = "https://vercel.com"
)

Expand All @@ -141,10 +143,12 @@ func ReadRepoConfigFile(path turbopath.AbsoluteSystemPath, flags *pflag.FlagSet)
repoViper.SetConfigType("json")
repoViper.SetEnvPrefix("turbo")
repoViper.MustBindEnv("apiurl", "TURBO_API")
repoViper.MustBindEnv("timeout", "TURBO_TIMEOUT")
repoViper.MustBindEnv("loginurl", "TURBO_LOGIN")
repoViper.MustBindEnv("teamslug", "TURBO_TEAM")
repoViper.MustBindEnv("teamid")
repoViper.SetDefault("apiurl", _defaultAPIURL)
repoViper.SetDefault("timeout", _defaultTimeout)
repoViper.SetDefault("loginurl", _defaultLoginURL)
if err := repoViper.BindPFlag("loginurl", flags.Lookup("login")); err != nil {
return nil, err
Expand All @@ -155,6 +159,9 @@ func ReadRepoConfigFile(path turbopath.AbsoluteSystemPath, flags *pflag.FlagSet)
if err := repoViper.BindPFlag("teamslug", flags.Lookup("team")); err != nil {
return nil, err
}
if err := repoViper.BindPFlag("timeout", flags.Lookup("timeout")); err != nil {
return nil, err
}
if err := repoViper.ReadInConfig(); err != nil && !os.IsNotExist(err) {
return nil, err
}
Expand All @@ -174,6 +181,7 @@ func AddRepoConfigFlags(flags *pflag.FlagSet) {
flags.String("team", "", "Set the team slug for API calls")
flags.String("api", "", "Override the endpoint for API calls")
flags.String("login", "", "Override the login endpoint")
flags.String("timeout", "20", "Override the timeout")
}

// GetRepoConfigPath reads the user-specific configuration values
Expand Down
11 changes: 11 additions & 0 deletions cli/internal/config/config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"strconv"
"testing"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -35,6 +36,8 @@ func TestReadRepoConfigSetTeamAndAPIFlag(t *testing.T) {
assert.NilError(t, flags.Set("team", slug), "flags.Set")
apiURL := "http://my-login-url"
assert.NilError(t, flags.Set("api", apiURL), "flags.Set")
timeout := "20"
assert.NilError(t, flags.Set("timeout", timeout), "flags.Set")

config, err := ReadRepoConfigFile(testConfigFile, flags)
if err != nil {
Expand All @@ -50,6 +53,10 @@ func TestReadRepoConfigSetTeamAndAPIFlag(t *testing.T) {
if remoteConfig.APIURL != apiURL {
t.Errorf("APIURL got %v, want %v", remoteConfig.APIURL, apiURL)
}
number, err := strconv.ParseUint(string(timeout), 10, 64)
if remoteConfig.Timeout != number {
t.Errorf("Timeout got %v, want %v", remoteConfig.Timeout, number)
}
}

func TestRepoConfigIncludesDefaults(t *testing.T) {
Expand All @@ -58,6 +65,7 @@ func TestRepoConfigIncludesDefaults(t *testing.T) {
AddRepoConfigFlags(flags)

expectedTeam := "my-team"
expectedTimeout := uint64(20)

assert.NilError(t, testConfigFile.EnsureDir(), "EnsureDir")
assert.NilError(t, testConfigFile.WriteFile([]byte(fmt.Sprintf(`{"teamSlug":"%v"}`, expectedTeam)), 0644), "WriteFile")
Expand All @@ -74,6 +82,9 @@ func TestRepoConfigIncludesDefaults(t *testing.T) {
if remoteConfig.TeamSlug != expectedTeam {
t.Errorf("team slug got %v, want %v", remoteConfig.TeamSlug, expectedTeam)
}
if remoteConfig.Timeout != expectedTimeout {
t.Errorf("timeout got %v, want %v", remoteConfig.Timeout, expectedTimeout)
}
}

func TestWriteRepoConfig(t *testing.T) {
Expand Down

0 comments on commit 22f76a7

Please sign in to comment.