提交 cc59f44e 编写于 作者: martianzhang's avatar martianzhang

add default dial timeout 3s

上级 dc3b709c
...@@ -238,9 +238,9 @@ type Dsn struct { ...@@ -238,9 +238,9 @@ type Dsn struct {
ServerPubKey string `yaml:"server-public-key"` // Server public key name ServerPubKey string `yaml:"server-public-key"` // Server public key name
MaxAllowedPacket int `ymal:"max-allowed-packet"` // Max packet size allowed MaxAllowedPacket int `ymal:"max-allowed-packet"` // Max packet size allowed
Params map[string]string `yaml:"params"` // Other Connection parameters, `SET param=val`, `SET NAMES charset` Params map[string]string `yaml:"params"` // Other Connection parameters, `SET param=val`, `SET NAMES charset`
Timeout int `yaml:"timeout"` // Dial timeout Timeout string `yaml:"timeout"` // Dial timeout
ReadTimeout int `yaml:"read-timeout"` // I/O read timeout ReadTimeout string `yaml:"read-timeout"` // I/O read timeout
WriteTimeout int `yaml:"write-timeout"` // I/O write timeout WriteTimeout string `yaml:"write-timeout"` // I/O write timeout
AllowNativePasswords bool `yaml:"allow-native-passwords"` // Allows the native password authentication method AllowNativePasswords bool `yaml:"allow-native-passwords"` // Allows the native password authentication method
AllowOldPasswords bool `yaml:"allow-old-passwords"` // Allows the old insecure password method AllowOldPasswords bool `yaml:"allow-old-passwords"` // Allows the old insecure password method
...@@ -255,6 +255,7 @@ func newDSN(cfg *mysql.Config) *Dsn { ...@@ -255,6 +255,7 @@ func newDSN(cfg *mysql.Config) *Dsn {
Net: "tcp", Net: "tcp",
Schema: "information_schema", Schema: "information_schema",
Charset: "utf8", Charset: "utf8",
Timeout: "3s",
AllowNativePasswords: true, AllowNativePasswords: true,
Params: make(map[string]string), Params: make(map[string]string),
MaxAllowedPacket: 4 << 20, // 4 MiB MaxAllowedPacket: 4 << 20, // 4 MiB
...@@ -282,16 +283,16 @@ func newDSN(cfg *mysql.Config) *Dsn { ...@@ -282,16 +283,16 @@ func newDSN(cfg *mysql.Config) *Dsn {
dsn.MaxAllowedPacket = cfg.MaxAllowedPacket dsn.MaxAllowedPacket = cfg.MaxAllowedPacket
dsn.ServerPubKey = cfg.ServerPubKey dsn.ServerPubKey = cfg.ServerPubKey
dsn.TLS = cfg.TLSConfig dsn.TLS = cfg.TLSConfig
dsn.Timeout = int(cfg.Timeout / time.Second) dsn.Timeout = cfg.Timeout.String()
dsn.ReadTimeout = int(cfg.ReadTimeout / time.Second) dsn.ReadTimeout = cfg.ReadTimeout.String()
dsn.WriteTimeout = int(cfg.WriteTimeout / time.Second) dsn.WriteTimeout = cfg.WriteTimeout.String()
dsn.AllowNativePasswords = cfg.AllowNativePasswords dsn.AllowNativePasswords = cfg.AllowNativePasswords
dsn.AllowOldPasswords = cfg.AllowOldPasswords dsn.AllowOldPasswords = cfg.AllowOldPasswords
return dsn return dsn
} }
// newMySQLConfig convert Dsn to go-sql-drive Config // newMySQLConfig convert Dsn to go-sql-drive Config
func (env *Dsn) newMySQLConifg() (*mysql.Config, error) { func (env *Dsn) newMySQLConfig() (*mysql.Config, error) {
var err error var err error
dsn := mysql.NewConfig() dsn := mysql.NewConfig()
...@@ -313,9 +314,18 @@ func (env *Dsn) newMySQLConifg() (*mysql.Config, error) { ...@@ -313,9 +314,18 @@ func (env *Dsn) newMySQLConifg() (*mysql.Config, error) {
dsn.MaxAllowedPacket = env.MaxAllowedPacket dsn.MaxAllowedPacket = env.MaxAllowedPacket
dsn.ServerPubKey = env.ServerPubKey dsn.ServerPubKey = env.ServerPubKey
dsn.TLSConfig = env.TLS dsn.TLSConfig = env.TLS
dsn.Timeout = time.Duration(env.Timeout) * time.Second if env.Timeout != "" {
dsn.ReadTimeout = time.Duration(env.ReadTimeout) * time.Second dsn.Timeout, err = time.ParseDuration(env.Timeout)
dsn.WriteTimeout = time.Duration(env.WriteTimeout) * time.Second LogIfError(err, "timeout: '%s'", env.Timeout)
}
if env.WriteTimeout != "" {
dsn.WriteTimeout, err = time.ParseDuration(env.WriteTimeout)
LogIfError(err, "writeTimeout: '%s'", env.WriteTimeout)
}
if env.ReadTimeout != "" {
dsn.ReadTimeout, err = time.ParseDuration(env.ReadTimeout)
LogIfError(err, "readTimeout: '%s'", env.ReadTimeout)
}
dsn.AllowNativePasswords = env.AllowNativePasswords dsn.AllowNativePasswords = env.AllowNativePasswords
dsn.AllowOldPasswords = env.AllowOldPasswords dsn.AllowOldPasswords = env.AllowOldPasswords
return dsn, err return dsn, err
...@@ -324,7 +334,7 @@ func (env *Dsn) newMySQLConifg() (*mysql.Config, error) { ...@@ -324,7 +334,7 @@ func (env *Dsn) newMySQLConifg() (*mysql.Config, error) {
// 解析命令行DSN输入 // 解析命令行DSN输入
func parseDSN(odbc string, d *Dsn) *Dsn { func parseDSN(odbc string, d *Dsn) *Dsn {
dsn := newDSN(nil) dsn := newDSN(nil)
var addr, user, password, schema, charset string var addr, user, password, schema, charset, timeout string
if odbc == FormatDSN(d) { if odbc == FormatDSN(d) {
return d return d
} }
...@@ -336,6 +346,7 @@ func parseDSN(odbc string, d *Dsn) *Dsn { ...@@ -336,6 +346,7 @@ func parseDSN(odbc string, d *Dsn) *Dsn {
password = d.Password password = d.Password
schema = d.Schema schema = d.Schema
charset = d.Charset charset = d.Charset
timeout = d.Timeout
} }
// 设置为空表示禁用环境 // 设置为空表示禁用环境
...@@ -398,6 +409,8 @@ func parseDSN(odbc string, d *Dsn) *Dsn { ...@@ -398,6 +409,8 @@ func parseDSN(odbc string, d *Dsn) *Dsn {
switch arg { switch arg {
case "charset": case "charset":
charset = val charset = val
case "timeout":
timeout = val
default: default:
} }
} }
...@@ -414,11 +427,17 @@ func parseDSN(odbc string, d *Dsn) *Dsn { ...@@ -414,11 +427,17 @@ func parseDSN(odbc string, d *Dsn) *Dsn {
charset = "utf8" charset = "utf8"
} }
// 默认连接数据库超时时间 3s
if timeout == "" {
timeout = "3s"
}
dsn.Addr = addr dsn.Addr = addr
dsn.User = user dsn.User = user
dsn.Password = password dsn.Password = password
dsn.Schema = schema dsn.Schema = schema
dsn.Charset = charset dsn.Charset = charset
dsn.Timeout = timeout
return dsn return dsn
} }
...@@ -437,7 +456,7 @@ func FormatDSN(env *Dsn) string { ...@@ -437,7 +456,7 @@ func FormatDSN(env *Dsn) string {
if env == nil || env.Disable { if env == nil || env.Disable {
return "" return ""
} }
dsn, err := env.newMySQLConifg() dsn, err := env.newMySQLConfig()
if err != nil { if err != nil {
return "" return ""
} }
......
...@@ -16,3 +16,7 @@ load test_helper ...@@ -16,3 +16,7 @@ load test_helper
[ $status -eq 0 ] [ $status -eq 0 ]
} }
@test "Check dial timeout" {
run timeout 1 ${SOAR_BIN} -test-dsn "1.1.1.1" -check-config
[ $status -eq 124 ]
}
...@@ -12,9 +12,9 @@ online-dsn: ...@@ -12,9 +12,9 @@ online-dsn:
maxallowedpacket: 419437 maxallowedpacket: 419437
params: params:
charset: utf8mb4 charset: utf8mb4
timeout: 60 timeout: 60s
read-timeout: 70 read-timeout: 70s
write-timeout: 80 write-timeout: 80s
allow-native-passwords: false allow-native-passwords: false
allow-old-passwords: true allow-old-passwords: true
disable: false disable: false
...@@ -32,9 +32,9 @@ test-dsn: ...@@ -32,9 +32,9 @@ test-dsn:
maxallowedpacket: 4194309 maxallowedpacket: 4194309
params: params:
charset: utf8mb4 charset: utf8mb4
timeout: 50 timeout: 50s
read-timeout: 40 read-timeout: 40s
write-timeout: 30 write-timeout: 30s
allow-native-passwords: false allow-native-passwords: false
allow-old-passwords: true allow-old-passwords: true
disable: false disable: false
......
...@@ -12,9 +12,9 @@ online-dsn: ...@@ -12,9 +12,9 @@ online-dsn:
maxallowedpacket: 4194304 maxallowedpacket: 4194304
params: params:
charset: utf8 charset: utf8
timeout: 0 timeout: 3s
read-timeout: 0 read-timeout: 0s
write-timeout: 0 write-timeout: 0s
allow-native-passwords: true allow-native-passwords: true
allow-old-passwords: false allow-old-passwords: false
disable: false disable: false
...@@ -32,9 +32,9 @@ test-dsn: ...@@ -32,9 +32,9 @@ test-dsn:
maxallowedpacket: 4194304 maxallowedpacket: 4194304
params: params:
charset: utf8 charset: utf8
timeout: 0 timeout: 3s
read-timeout: 0 read-timeout: 0s
write-timeout: 0 write-timeout: 0s
allow-native-passwords: true allow-native-passwords: true
allow-old-passwords: false allow-old-passwords: false
disable: false disable: false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册