Skip to content

Commit

Permalink
Merge pull request #29 from ClareChu/mysql-dev
Browse files Browse the repository at this point in the history
gorm connection mysql add autoReconnect
  • Loading branch information
john-deng authored Feb 12, 2020
2 parents f79d279 + 54c73b2 commit e057991
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 38 deletions.
1 change: 1 addition & 0 deletions examples/gorm/config/application-gorm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gorm:
password: Duahbi3GqMmeS7ogPw743xtmsmVFiDIaNwh24BokqC1BnUIah8doCRRhVuIqtWs3tl8nHRNkEWMIbuKAo6GleN4FwSoO56B8HAxqP3Kv8Jr3A6L2bam1bglFJRDx6rRkSuX4zrO3D/35t1YSFdPKf+n2PWIEeEJI6zcULo7UVKA=
charset: utf8
parseTime: true
retry_times: 1
loc: Asia/Shanghai
config:
decrypt: true
Expand Down
6 changes: 6 additions & 0 deletions examples/gorm/config/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ server:

logging:
level: info
#
#gorm:
# host: 10.10.12.14
# database: demo
# username: root
# password: mN632xQ/rlS3E0r0pGz/bSvbEAoAGQSMSzxc1EmHUdjf6oGhMNtQ1EQmd14+LSMcqnr6Ln1UNoQvzgZMCyqAty4tXVFAxd+h+YABDcf+gBS+b+I4R4iQmxVdbT/FA2OcXiqT4XW0YKHh1KuHw/fBqhn2QwqH2IVIaUzA7689dpw=
2 changes: 1 addition & 1 deletion starter/gorm/autoconfigure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func TestConfiguration(t *testing.T) {
})

repo := conf.Repository()
assert.Equal(t, nil, repo)
assert.NotEqual(t, nil, repo)
}
76 changes: 52 additions & 24 deletions starter/gorm/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,9 @@ func (d *dataSource) Init(repository Repository) {

func (d *dataSource) Open(p *Properties) error {
var err error
password := p.Password
if p.Config.Decrypt {
pwd, err := rsa.DecryptBase64([]byte(password), []byte(p.Config.DecryptKey))
if err == nil {
password = string(pwd)
}
}
loc := strings.Replace(p.Loc, "/", "%2F", -1)
databaseName := strings.Replace(p.Database, "-", "_", -1)
parseTime := "False"
if p.ParseTime {
parseTime = "True"
}
source := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v&parseTime=%v&loc=%v",
p.Username, password, p.Host, p.Port, databaseName, p.Charset, parseTime, loc)

d.repository, err = gorm.Open(p.Type, source)
err = d.Connect(p)
if err != nil {
log.Errorf("dataSource connection failed: %v (%v)", err, p)
defer func() {
d.repository.Close()
d.repository = nil
}()
return err
} else {
log.Infof("connected to dataSource %v@%v:%v/%v", p.Username, p.Host, p.Port, databaseName)
}
db := d.repository.SqlDB()
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
Expand Down Expand Up @@ -119,3 +96,54 @@ func (d *dataSource) Close() error {
func (d *dataSource) Repository() gorm.Repository {
return d.repository
}

func (d *dataSource) Interval(p *Properties) error {
duration, err := time.ParseDuration(p.Interval)
if err != nil {
log.Errorf("dataSource parse duration failed: %v", err)
return err
}
time.Sleep(duration)
return nil
}

func (d *dataSource) Connect(p *Properties) (err error) {
password := p.Password
if p.Config.Decrypt {
pwd, err := rsa.DecryptBase64([]byte(password), []byte(p.Config.DecryptKey))
if err == nil {
password = string(pwd)
}
}
loc := strings.Replace(p.Loc, "/", "%2F", -1)
databaseName := strings.Replace(p.Database, "-", "_", -1)
parseTime := "False"
if p.ParseTime {
parseTime = "True"
}
source := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v&parseTime=%v&loc=%v",
p.Username, password, p.Host, p.Port, databaseName, p.Charset, parseTime, loc)
d.repository, err = gorm.Open(p.Type, source)
if err != nil {
log.Errorf("dataSource connection failed: %v (%v)", err, p)
if p.AutoReconnect {
// 重试间隔时间
err := d.Interval(p)
if err != nil {
return err
}
if p.RetryTimes == -1 {
err = d.Connect(p)
} else if p.RetryTimes > p.NowRetryTimes {
p.NowRetryTimes++
err = d.Connect(p)
}

} else {
return err
}
} else {
log.Infof("connected to dataSource %v@%v:%v/%v", p.Username, p.Host, p.Port, databaseName)
}
return err
}
26 changes: 15 additions & 11 deletions starter/gorm/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ func init() {

func TestDataSourceOpen(t *testing.T) {
prop := &Properties{
Type: "mysql",
Host: "mysql-dev",
Port: "3306",
Username: "test",
Password: "LcNxqoI4zZjAnpiTD7JQxLJR/IgL2iTiSZ2nd7KPEBgxMV+FVhPSzM+fgH93XqZJNpboN4F/buX22yLTXK38AcVGTfID3rmQAOAc9A2DIWNy5v9+3NOY00M8z4dR1XHojheK0681cY9QVjtlJ70jFFDXb7PjFc2fQ0GIyIjBQDY=",
Database: "test",
ParseTime: true,
Charset: "utf8",
Loc: "Asia/Shanghai",
Type: "mysql",
Host: "mysql-dev",
Port: "3306",
Username: "test",
Password: "LcNxqoI4zZjAnpiTD7JQxLJR/IgL2iTiSZ2nd7KPEBgxMV+FVhPSzM+fgH93XqZJNpboN4F/buX22yLTXK38AcVGTfID3rmQAOAc9A2DIWNy5v9+3NOY00M8z4dR1XHojheK0681cY9QVjtlJ70jFFDXb7PjFc2fQ0GIyIjBQDY=",
Database: "test",
ParseTime: true,
Charset: "utf8",
Loc: "Asia/Shanghai",
NowRetryTimes: 0,
RetryTimes: 2,
Interval: "1s",
AutoReconnect: true,
Config: Config{
Decrypt: true,
},
Decrypt: true,
},
}
dataSource := new(dataSource)

Expand Down
8 changes: 6 additions & 2 deletions starter/gorm/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type Properties struct {
Loc string `json:"loc" default:"Asia/Shanghai"`
Config Config `json:"config"`
ConnMaxLifetime string `json:"connMaxLifetime" default:"60s"`
MaxIdleConns int `json:"maxIdleConns" default:"20"`
MaxOpenConns int `json:"maxOpenConns" default:"200"`
MaxIdleConns int `json:"maxIdle_conns" default:"20"`
MaxOpenConns int `json:"maxOpen_conns" default:"200"`
AutoReconnect bool `json:"auto_reconnect" default:"true"`
RetryTimes int `json:"retry_times" default:"-1"`
Interval string `json:"interval" default:"3s"`
NowRetryTimes int `json:"now_retry_times" default:"0"`
}

0 comments on commit e057991

Please sign in to comment.