Authentication-Results: mail-b.sr.ht; dkim=pass header.d=autistici.org header.i=@autistici.org Received: from confino.investici.org (confino.investici.org [93.190.126.19]) by mail-b.sr.ht (Postfix) with ESMTPS id D473411EFD2 for <~emersion/hut-dev@lists.sr.ht>; Mon, 16 May 2022 15:02:21 +0000 (UTC) Received: from mx1.investici.org (unknown [127.0.0.1]) by confino.investici.org (Postfix) with ESMTP id 4L22Xr5wdlz10xf; Mon, 16 May 2022 15:02:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1652713340; bh=lhUv7JjN9YLLV4ZqtXdaEHIAmRyubZApNesUhOHUw4Q=; h=From:To:Cc:Subject:Date:From; b=slyvQvvCzXWLSr3gAeC234zrPNk42wGb5UvtnDx18FZt3jR+c8l5HqxjLF8jeHBtz eJWjRAvjOcnSZ+LNyU+j2HLtYJ6U/P4ER6m1HwUb7P3AB/dbp07+nvKVItmTb/CSap k6xU39CqEJ869Fcj8B9J+xUkMsq43zjaJclkLFpk= Received: from [93.190.126.19] (mx1.investici.org [93.190.126.19]) (Authenticated sender: problemi.ma@anche.no) by localhost (Postfix) with ESMTPSA id 4L22Xr4HGvz10xV; Mon, 16 May 2022 15:02:20 +0000 (UTC) From: Blallo To: ~emersion/hut-dev@lists.sr.ht Cc: Blallo Subject: [PATCH] Make timeout configurable Date: Mon, 16 May 2022 17:05:40 +0200 Message-Id: <20220516150540.42563-1-blallo@autistici.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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