Skip to content

Commit 32d8ff8

Browse files
authored
Merge pull request #720 from exercism/configurable-timeout
Make the timeout configurable globally
2 parents e6277a7 + bf809ab commit 32d8ff8

4 files changed

Lines changed: 16 additions & 7 deletions

File tree

api/client.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ var (
1414
// It's overridden from the root command so that we can set the version.
1515
UserAgent = "github.com/exercism/cli"
1616

17-
// DefaultHTTPClient configures a timeout to use by default.
18-
DefaultHTTPClient = &http.Client{Timeout: 10 * time.Second}
17+
// TimeoutInSeconds is the timeout the default HTTP client will use.
18+
TimeoutInSeconds = 60
19+
// HTTPClient is the client used to make HTTP calls in the cli package.
20+
HTTPClient = &http.Client{Timeout: time.Duration(TimeoutInSeconds) * time.Second}
1921
)
2022

2123
// Client is an http client that is configured for Exercism.
@@ -29,7 +31,7 @@ type Client struct {
2931
// NewClient returns an Exercism API client.
3032
func NewClient(token, baseURL string) (*Client, error) {
3133
return &Client{
32-
Client: DefaultHTTPClient,
34+
Client: HTTPClient,
3335
Token: token,
3436
APIBaseURL: baseURL,
3537
}, nil
@@ -38,7 +40,7 @@ func NewClient(token, baseURL string) (*Client, error) {
3840
// NewRequest returns an http.Request with information for the Exercism API.
3941
func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
4042
if c.Client == nil {
41-
c.Client = DefaultHTTPClient
43+
c.Client = HTTPClient
4244
}
4345

4446
req, err := http.NewRequest(method, url, body)

cli/cli.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ var (
4343
)
4444

4545
var (
46+
// TimeoutInSeconds is the timeout the default HTTP client will use.
47+
TimeoutInSeconds = 60
4648
// HTTPClient is the client used to make HTTP calls in the cli package.
47-
HTTPClient = &http.Client{Timeout: 10 * time.Second}
49+
HTTPClient = &http.Client{Timeout: time.Duration(TimeoutInSeconds) * time.Second}
4850
// ReleaseURL is the endpoint that provides information about cli releases.
4951
ReleaseURL = "https://api.github.com/repos/exercism/cli/releases"
5052
)

cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime"
88

99
"github.com/exercism/cli/api"
10+
"github.com/exercism/cli/cli"
1011
"github.com/exercism/cli/config"
1112
"github.com/exercism/cli/debug"
1213
"github.com/spf13/cobra"
@@ -39,6 +40,10 @@ Download exercises and submit your solutions.`,
3940
if verbose, _ := cmd.Flags().GetBool("verbose"); verbose {
4041
debug.Verbose = verbose
4142
}
43+
if timeout, _ := cmd.Flags().GetInt("timeout"); timeout > 0 {
44+
cli.TimeoutInSeconds = timeout
45+
api.TimeoutInSeconds = timeout
46+
}
4247
},
4348
}
4449

@@ -57,4 +62,5 @@ func init() {
5762
In = os.Stdin
5863
api.UserAgent = fmt.Sprintf("github.com/exercism/cli v%s (%s/%s)", Version, runtime.GOOS, runtime.GOARCH)
5964
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
65+
RootCmd.PersistentFlags().IntP("timeout", "", 0, "override the default HTTP timeout (seconds)")
6066
}

cmd/troubleshoot.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"html/template"
7-
"net/http"
87
"runtime"
98
"strings"
109
"sync"
@@ -30,7 +29,7 @@ If you're running into trouble, copy and paste the output from the troubleshoot
3029
command into a GitHub issue so we can help figure out what's going on.
3130
`,
3231
RunE: func(cmd *cobra.Command, args []string) error {
33-
cli.HTTPClient = &http.Client{Timeout: 20 * time.Second}
32+
cli.TimeoutInSeconds = cli.TimeoutInSeconds * 2
3433
c := cli.New(Version)
3534

3635
cfg := config.NewConfig()

0 commit comments

Comments
 (0)