1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
package main
import (
"flag"
"net/http"
"time"
)
// ClientConfig provides the timeouts from CLI flags and default values for the
// example apps's configuration.
type ClientConfig struct {
KeepAlive bool
Timeouts Timeouts
}
// SetupFlags initializes the CLI flags.
func (c *ClientConfig) SetupFlags(prefix string, flagset *flag.FlagSet) {
prefix += "client."
flagset.BoolVar(&c.KeepAlive, prefix+"http-keep-alive", true,
"Specifies if HTTP keep alive is enabled.")
c.Timeouts.SetupFlags(prefix, flagset)
}
// Timeouts collection of HTTP client timeout values.
type Timeouts struct {
IdleConnection time.Duration
Connect time.Duration
TLSHandshake time.Duration
ExpectContinue time.Duration
ResponseHeader time.Duration
}
// SetupFlags initializes the CLI flags.
func (c *Timeouts) SetupFlags(prefix string, flagset *flag.FlagSet) {
prefix += "timeout."
flagset.DurationVar(&c.IdleConnection, prefix+"idle-conn", 90*time.Second,
"The `timeout` of idle connects to the remote host.")
flagset.DurationVar(&c.Connect, prefix+"connect", 30*time.Second,
"The `timeout` connecting to the remote host.")
defTR := http.DefaultTransport.(*http.Transport)
flagset.DurationVar(&c.TLSHandshake, prefix+"tls", defTR.TLSHandshakeTimeout,
"The `timeout` waiting for the TLS handshake to complete.")
c.ExpectContinue = defTR.ExpectContinueTimeout
flagset.DurationVar(&c.ResponseHeader, prefix+"response-header", defTR.ResponseHeaderTimeout,
"The `timeout` waiting for the TLS handshake to complete.")
}
|