Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set client timeout https://github.com/Kong/deck/issues/449 #450

Merged
merged 9 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func init() {
"share anonymized data to help improve decK")
viper.BindPFlag("analytics",
rootCmd.PersistentFlags().Lookup("analytics"))

rootCmd.PersistentFlags().Int("timeout", 10,
"set requests timeout client to connect with kong, value in seconds.")
viper.BindPFlag("timeout",
rootCmd.PersistentFlags().Lookup("timeout"))
}

// initConfig reads in config file and ENV variables if set.
Expand Down Expand Up @@ -177,6 +182,7 @@ func initConfig() {
rootConfig.Headers = viper.GetStringSlice("headers")
rootConfig.SkipWorkspaceCrud = viper.GetBool("skip-workspace-crud")
rootConfig.Debug = (viper.GetInt("verbose") >= 1)
rootConfig.Timeout = (viper.GetInt("timeout"))

color.NoColor = (color.NoColor || viper.GetBool("no-color"))

Expand Down
13 changes: 7 additions & 6 deletions utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
"github.com/kong/go-kong/kong/custom"
)

const (
defaultClientTimeout = 10 * time.Second
)
var clientTimeout time.Duration

// KongRawState contains all of Kong Data
type KongRawState struct {
Expand Down Expand Up @@ -94,6 +92,8 @@ type KongClientConfig struct {
Headers []string

HTTPClient *http.Client

Timeout int
}

type KonnectConfig struct {
Expand Down Expand Up @@ -130,6 +130,7 @@ func GetKongClient(opt KongClientConfig) (*kong.Client, error) {
tlsConfig.RootCAs = certPool
}

clientTimeout = time.Duration(opt.Timeout) * time.Second
c := opt.HTTPClient
if c == nil {
c = HTTPClient()
Expand Down Expand Up @@ -204,12 +205,12 @@ func CleanAddress(address string) string {
// sane default timeouts.
func HTTPClient() *http.Client {
return &http.Client{
Timeout: defaultClientTimeout,
Timeout: clientTimeout,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: defaultClientTimeout,
Timeout: clientTimeout,
}).DialContext,
TLSHandshakeTimeout: defaultClientTimeout,
TLSHandshakeTimeout: clientTimeout,
},
}
}