~emersion/hut-dev

Make timeout configurable v1 NEEDS REVISION

Blallo: 1
 Make timeout configurable

 2 files changed, 15 insertions(+), 4 deletions(-)
Simon Ser:
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/hut-dev/patches/32322/mbox | git am -3
Learn more about email & git

[PATCH] Make timeout configurable Export this patch

The http client used for interacting with the hut instance had a
hardcoded 30 seconds timeout. While this might be ok in most cases, I
happened to hit one of the corner cases when trying to push a tarball to
site.ht. This commit adds a global flag to set the value, defaulting to
the old one.
---
 config.go | 18 ++++++++++++++----
 main.go   |  1 +
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/config.go b/config.go
index 1f9c786..fa0b8e3 100644
--- a/config.go
+++ b/config.go
@@ -114,6 +114,11 @@ func createClient(service string, cmd *cobra.Command) *Client {
}

func createClientWithInstance(service string, cmd *cobra.Command, instanceName string) *Client {
	timeout, err := cmd.Flags().GetDuration("timeout")
	if err != nil {
		log.Fatal(err)
	}

	customConfigFile := true
	configFile, err := cmd.Flags().GetString("config")
	if err != nil {
@@ -184,14 +189,14 @@ func createClientWithInstance(service string, cmd *cobra.Command, instanceName s
	if baseURL == "" {
		log.Fatalf("failed to get origin for service %q in instance %q", service, inst.Name)
	}
	return createClientWithToken(baseURL, token)
	return createClientWithToken(baseURL, token, timeout)
}

func createClientWithToken(baseURL, token string) *Client {
func createClientWithToken(baseURL, token string, timeout time.Duration) *Client {
	gqlEndpoint := baseURL + "/query"
	tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	httpClient := oauth2.NewClient(context.Background(), tokenSrc)
	httpClient.Timeout = 30 * time.Second
	httpClient.Timeout = timeout
	return &Client{
		Client:  gqlclient.New(gqlEndpoint, httpClient),
		BaseURL: baseURL,
@@ -233,6 +238,11 @@ func newInitCommand() *cobra.Command {
	cmd.Run = func(cmd *cobra.Command, args []string) {
		ctx := cmd.Context()

		timeout, err := cmd.Flags().GetDuration("timeout")
		if err != nil {
			log.Fatal(err)
		}

		filename, err := cmd.Flags().GetString("config")
		if err != nil {
			log.Fatal(err)
@@ -263,7 +273,7 @@ func newInitCommand() *cobra.Command {

		config := fmt.Sprintf("instance %q {\n	access-token %q\n}\n", instance, token)

		c := createClientWithToken(baseURL, token)
		c := createClientWithToken(baseURL, token, timeout)
		user, err := metasrht.FetchMe(c.Client, ctx)
		if err != nil {
			log.Fatalf("failed to check OAuth2 token: %v", err)
diff --git a/main.go b/main.go
index cb548fa..398bb93 100644
--- a/main.go
+++ b/main.go
@@ -36,6 +36,7 @@ func main() {
	cmd.PersistentFlags().String("instance", "", "sr.ht instance to use")
	cmd.RegisterFlagCompletionFunc("instance", cobra.NoFileCompletions)
	cmd.PersistentFlags().String("config", "", "config file to use")
	cmd.PersistentFlags().Duration("timeout", 30*time.Second, "timeout to use")

	cmd.AddCommand(newBuildsCommand())
	cmd.AddCommand(newExportCommand())
-- 
2.36.1