From cc59f44e3900cb8b57b4dc904fe0bb0269b931bf Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Thu, 14 Feb 2019 15:57:43 +0800 Subject: [PATCH] add default dial timeout 3s --- common/config.go | 43 +++++++++++++------ test/env.bats | 4 ++ ...k_the_default_config_of_the_changes.golden | 12 +++--- .../test_Run_default_printconfig_cases.golden | 12 +++--- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/common/config.go b/common/config.go index 0a15220..fec8d19 100644 --- a/common/config.go +++ b/common/config.go @@ -238,9 +238,9 @@ type Dsn struct { ServerPubKey string `yaml:"server-public-key"` // Server public key name 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` - Timeout int `yaml:"timeout"` // Dial timeout - ReadTimeout int `yaml:"read-timeout"` // I/O read timeout - WriteTimeout int `yaml:"write-timeout"` // I/O write timeout + Timeout string `yaml:"timeout"` // Dial timeout + ReadTimeout string `yaml:"read-timeout"` // I/O read timeout + WriteTimeout string `yaml:"write-timeout"` // I/O write timeout AllowNativePasswords bool `yaml:"allow-native-passwords"` // Allows the native password authentication method AllowOldPasswords bool `yaml:"allow-old-passwords"` // Allows the old insecure password method @@ -255,6 +255,7 @@ func newDSN(cfg *mysql.Config) *Dsn { Net: "tcp", Schema: "information_schema", Charset: "utf8", + Timeout: "3s", AllowNativePasswords: true, Params: make(map[string]string), MaxAllowedPacket: 4 << 20, // 4 MiB @@ -282,16 +283,16 @@ func newDSN(cfg *mysql.Config) *Dsn { dsn.MaxAllowedPacket = cfg.MaxAllowedPacket dsn.ServerPubKey = cfg.ServerPubKey dsn.TLS = cfg.TLSConfig - dsn.Timeout = int(cfg.Timeout / time.Second) - dsn.ReadTimeout = int(cfg.ReadTimeout / time.Second) - dsn.WriteTimeout = int(cfg.WriteTimeout / time.Second) + dsn.Timeout = cfg.Timeout.String() + dsn.ReadTimeout = cfg.ReadTimeout.String() + dsn.WriteTimeout = cfg.WriteTimeout.String() dsn.AllowNativePasswords = cfg.AllowNativePasswords dsn.AllowOldPasswords = cfg.AllowOldPasswords return dsn } // 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 dsn := mysql.NewConfig() @@ -313,9 +314,18 @@ func (env *Dsn) newMySQLConifg() (*mysql.Config, error) { dsn.MaxAllowedPacket = env.MaxAllowedPacket dsn.ServerPubKey = env.ServerPubKey dsn.TLSConfig = env.TLS - dsn.Timeout = time.Duration(env.Timeout) * time.Second - dsn.ReadTimeout = time.Duration(env.ReadTimeout) * time.Second - dsn.WriteTimeout = time.Duration(env.WriteTimeout) * time.Second + if env.Timeout != "" { + dsn.Timeout, err = time.ParseDuration(env.Timeout) + 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.AllowOldPasswords = env.AllowOldPasswords return dsn, err @@ -324,7 +334,7 @@ func (env *Dsn) newMySQLConifg() (*mysql.Config, error) { // 解析命令行DSN输入 func parseDSN(odbc string, d *Dsn) *Dsn { dsn := newDSN(nil) - var addr, user, password, schema, charset string + var addr, user, password, schema, charset, timeout string if odbc == FormatDSN(d) { return d } @@ -336,6 +346,7 @@ func parseDSN(odbc string, d *Dsn) *Dsn { password = d.Password schema = d.Schema charset = d.Charset + timeout = d.Timeout } // 设置为空表示禁用环境 @@ -398,6 +409,8 @@ func parseDSN(odbc string, d *Dsn) *Dsn { switch arg { case "charset": charset = val + case "timeout": + timeout = val default: } } @@ -414,11 +427,17 @@ func parseDSN(odbc string, d *Dsn) *Dsn { charset = "utf8" } + // 默认连接数据库超时时间 3s + if timeout == "" { + timeout = "3s" + } + dsn.Addr = addr dsn.User = user dsn.Password = password dsn.Schema = schema dsn.Charset = charset + dsn.Timeout = timeout return dsn } @@ -437,7 +456,7 @@ func FormatDSN(env *Dsn) string { if env == nil || env.Disable { return "" } - dsn, err := env.newMySQLConifg() + dsn, err := env.newMySQLConfig() if err != nil { return "" } diff --git a/test/env.bats b/test/env.bats index ab884d2..81da2d5 100644 --- a/test/env.bats +++ b/test/env.bats @@ -16,3 +16,7 @@ load test_helper [ $status -eq 0 ] } +@test "Check dial timeout" { + run timeout 1 ${SOAR_BIN} -test-dsn "1.1.1.1" -check-config + [ $status -eq 124 ] +} diff --git a/test/fixture/test_Check_the_default_config_of_the_changes.golden b/test/fixture/test_Check_the_default_config_of_the_changes.golden index 7b23075..8ae88d2 100644 --- a/test/fixture/test_Check_the_default_config_of_the_changes.golden +++ b/test/fixture/test_Check_the_default_config_of_the_changes.golden @@ -12,9 +12,9 @@ online-dsn: maxallowedpacket: 419437 params: charset: utf8mb4 - timeout: 60 - read-timeout: 70 - write-timeout: 80 + timeout: 60s + read-timeout: 70s + write-timeout: 80s allow-native-passwords: false allow-old-passwords: true disable: false @@ -32,9 +32,9 @@ test-dsn: maxallowedpacket: 4194309 params: charset: utf8mb4 - timeout: 50 - read-timeout: 40 - write-timeout: 30 + timeout: 50s + read-timeout: 40s + write-timeout: 30s allow-native-passwords: false allow-old-passwords: true disable: false diff --git a/test/fixture/test_Run_default_printconfig_cases.golden b/test/fixture/test_Run_default_printconfig_cases.golden index 4624675..20894fb 100644 --- a/test/fixture/test_Run_default_printconfig_cases.golden +++ b/test/fixture/test_Run_default_printconfig_cases.golden @@ -12,9 +12,9 @@ online-dsn: maxallowedpacket: 4194304 params: charset: utf8 - timeout: 0 - read-timeout: 0 - write-timeout: 0 + timeout: 3s + read-timeout: 0s + write-timeout: 0s allow-native-passwords: true allow-old-passwords: false disable: false @@ -32,9 +32,9 @@ test-dsn: maxallowedpacket: 4194304 params: charset: utf8 - timeout: 0 - read-timeout: 0 - write-timeout: 0 + timeout: 3s + read-timeout: 0s + write-timeout: 0s allow-native-passwords: true allow-old-passwords: false disable: false -- GitLab