support http connect timeout setting (#27)
* connection support timeout settings. * use transport dial timeout to replace http timeout. * fix config test.
This commit is contained in:
parent
e32549f72a
commit
82eb265471
|
@ -24,6 +24,8 @@ import (
|
|||
|
||||
"github.com/yunify/qingcloud-sdk-go/logger"
|
||||
"github.com/yunify/qingcloud-sdk-go/utils"
|
||||
"time"
|
||||
"net"
|
||||
)
|
||||
|
||||
// A Config stores a configuration of this sdk.
|
||||
|
@ -36,6 +38,7 @@ type Config struct {
|
|||
Protocol string `yaml:"protocol"`
|
||||
URI string `yaml:"uri"`
|
||||
ConnectionRetries int `yaml:"connection_retries"`
|
||||
ConnectionTimeout int `yaml:"connection_timeout"`
|
||||
|
||||
LogLevel string `yaml:"log_level"`
|
||||
|
||||
|
@ -66,7 +69,16 @@ func NewDefault() (*Config, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Connection = &http.Client{}
|
||||
|
||||
timeout := time.Duration(config.ConnectionTimeout) * time.Second
|
||||
transport := &http.Transport{
|
||||
Dial: func (network, addr string) (net.Conn, error) {
|
||||
return net.DialTimeout(network, addr, timeout)
|
||||
},
|
||||
}
|
||||
config.Connection = &http.Client{
|
||||
Transport:transport,
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
@ -126,5 +138,15 @@ func (c *Config) LoadConfigFromContent(content []byte) error {
|
|||
|
||||
logger.SetLevel(c.LogLevel)
|
||||
|
||||
timeout := time.Duration(c.ConnectionTimeout) * time.Second
|
||||
transport := &http.Transport{
|
||||
Dial: func (network, addr string) (net.Conn, error) {
|
||||
return net.DialTimeout(network, addr, timeout)
|
||||
},
|
||||
}
|
||||
c.Connection = &http.Client{
|
||||
Transport:transport,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
|
@ -35,6 +35,7 @@ port: 443
|
|||
protocol: 'https'
|
||||
uri: '/iaas'
|
||||
connection_retries: 3
|
||||
connection_timeout: 30
|
||||
|
||||
# Valid log levels are "debug", "info", "warn", "error", and "fatal".
|
||||
log_level: 'warn'
|
||||
|
|
Loading…
Reference in New Issue