Skip to content

Commit

Permalink
fix: adds 2 http retries to humanitec client requests
Browse files Browse the repository at this point in the history
  • Loading branch information
astromechza committed Sep 12, 2024
1 parent 65999d1 commit 9d92963
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
29 changes: 22 additions & 7 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 9d92963

Please sign in to comment.