Skip to content

Commit

Permalink
reduce idle connection timeout (#2496)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarimo authored Jul 22, 2024
1 parent 6545125 commit aa5251d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions datadog/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ func defaultConfigureFunc(p *FrameworkProvider, request *provider.ConfigureReque
}
}

ddClientConfig.HTTPClient = utils.NewHTTPClient()
datadogClient := datadog.NewAPIClient(ddClientConfig)

p.DatadogApiInstances = &utils.ApiInstances{HttpClient: datadogClient}
Expand Down
25 changes: 25 additions & 0 deletions datadog/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strings"
"sync"
"time"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
frameworkDiag "github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -81,6 +82,30 @@ type Resource interface {
GetOk(string) (interface{}, bool)
}

// NewTransport returns new transport with default values borrowed from http.DefaultTransport
func NewTransport() *http.Transport {
return &http.Transport{
// Default values copied from http.DefaultTransport
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 45 * time.Second, // Reduced idle connection timeout from default of 90s
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}

// NewHTTPClient returns new http.Client
func NewHTTPClient() *http.Client {
return &http.Client{
Transport: NewTransport(),
}
}

// FrameworkErrorDiag return error diag
func FrameworkErrorDiag(err error, msg string) frameworkDiag.ErrorDiagnostic {
var summary string
Expand Down
1 change: 1 addition & 0 deletions datadog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
})
}

config.HTTPClient = utils.NewHTTPClient()
datadogClient := datadog.NewAPIClient(config)
apiInstances := &utils.ApiInstances{HttpClient: datadogClient}
if validate {
Expand Down

0 comments on commit aa5251d

Please sign in to comment.