From 9d929638e073dec6a94adf4654376b878982e90a Mon Sep 17 00:00:00 2001 From: Ben Meier Date: Thu, 12 Sep 2024 16:44:39 +0100 Subject: [PATCH] fix: adds 2 http retries to humanitec client requests --- go.mod | 1 + go.sum | 2 ++ internal/provider/provider.go | 29 ++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 68cdff5..e05b9d9 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/hashicorp/terraform-plugin-log v0.9.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 github.com/humanitec/humanitec-go-autogen v0.0.0-20240620130303-6979d29fd1fa + github.com/justinrixx/retryhttp v1.0.1 github.com/stretchr/testify v1.9.0 sigs.k8s.io/yaml v1.4.0 ) diff --git a/go.sum b/go.sum index 41a6dbf..8084f76 100644 --- a/go.sum +++ b/go.sum @@ -136,6 +136,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= +github.com/justinrixx/retryhttp v1.0.1 h1:hTCOBTOcmzR3/W7weMB2YBOl1LlQrqVPIZPAf40p+14= +github.com/justinrixx/retryhttp v1.0.1/go.mod h1:vGs79/0Ut0//fmOGS9HAgI/IbZS6oFV3jFOTCxGEXt0= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 90529e0..0a65643 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -3,14 +3,18 @@ package provider import ( "context" "crypto/tls" + "net" "net/http" "os" + "time" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-framework/provider/schema" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/justinrixx/retryhttp" + "github.com/humanitec/humanitec-go-autogen" ) @@ -170,16 +174,27 @@ func (p *HumanitecProvider) Configure(ctx context.Context, req provider.Configur // Not returning early allows the logic to collect all errors. } - var doer *http.Client + baseTransport := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } + if data.DisableSSLCertificateVerification.ValueBool() { - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - doer = &http.Client{Transport: tr} - } else { - doer = &http.Client{} + baseTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} } + doer := &http.Client{ + Timeout: time.Minute, + Transport: retryhttp.New(retryhttp.WithTransport(baseTransport)), + } client, err := NewHumanitecClient(apiPrefix, token, p.version, doer) if err != nil { resp.Diagnostics.AddError("Unable to create Humanitec client", err.Error())