Skip to content

Commit

Permalink
feat: add sane default timeouts to http.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
hbagdi committed Apr 26, 2021
1 parent 6849b6c commit 37eeec8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"net/http"
"os"

"github.com/fatih/color"
Expand All @@ -20,7 +19,7 @@ import (

func syncKonnect(ctx context.Context,
filenames []string, dry bool, parallelism int) error {
httpClient := http.DefaultClient
httpClient := utils.HTTPClient()

// read target file
targetContent, err := file.GetContentFromFiles(filenames)
Expand Down
3 changes: 1 addition & 2 deletions cmd/konnect_dump.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"net/http"
"strings"

"github.com/kong/deck/konnect"
Expand Down Expand Up @@ -29,7 +28,7 @@ a file on disk. The file can then be read using the Sync or Diff command to agai
configure Konnect.` + konnectAlphaState,
Args: validateNoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
httpClient := http.DefaultClient
httpClient := utils.HTTPClient()

if yes, err := confirmFileOverwrite(konnectDumpCmdKongStateFile, dumpCmdStateFormat, assumeYes); err != nil {
return err
Expand Down
24 changes: 22 additions & 2 deletions utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/kong/deck/konnect"
"net"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"

"github.com/kong/deck/konnect"
"github.com/kong/go-kong/kong"
"github.com/kong/go-kong/kong/custom"
"github.com/pkg/errors"
)

const (
defaultClientTimeout = 10 * time.Second
)

// KongRawState contains all of Kong Data
type KongRawState struct {
Services []*kong.Service
Expand Down Expand Up @@ -150,7 +156,7 @@ func GetKongClient(opt KongClientConfig) (*kong.Client, error) {

c := opt.HTTPClient
if c == nil {
c = &http.Client{}
c = HTTPClient()
}
defaultTransport := http.DefaultTransport.(*http.Transport)
defaultTransport.TLSClientConfig = &tlsConfig
Expand Down Expand Up @@ -200,3 +206,17 @@ func CleanAddress(address string) string {
re := regexp.MustCompile("[/]+$")
return re.ReplaceAllString(address, "")
}

// HTTPClient returns a new Go stdlib's net/http.Client with
// sane default timeouts.
func HTTPClient() *http.Client {
return &http.Client{
Timeout: defaultClientTimeout,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: defaultClientTimeout,
}).DialContext,
TLSHandshakeTimeout: defaultClientTimeout,
},
}
}

0 comments on commit 37eeec8

Please sign in to comment.