Skip to content

Commit

Permalink
fix: fix the unit of ConnMaxIdleTime and ConnMaxLifeTime fields in th…
Browse files Browse the repository at this point in the history
…e database connection pool to second

- optimize database configuration to facilitate linking to sqlite and other databases
  • Loading branch information
OldSmokeGun committed Oct 26, 2023
1 parent d6d1c42 commit f34514a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 89 deletions.
24 changes: 4 additions & 20 deletions etc/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,26 @@ services:

# db:
# driver: "mysql"
# addr: "127.0.0.1:3306"
# database: "go-scaffold"
# username: "root"
# password: "root"
# options: "charset=utf8mb4&parseTime=True&loc=Local"
# dsn: "root:root@tcp(127.0.0.1:3306)/go-scaffold?charset=utf8mb4&parseTime=True&loc=Local"
# maxIdleConn: 20
# maxOpenConn: 40
# connMaxIdleTime: 120
# connMaxLifeTime: 120
# logInfo: true
# resolvers:
# - type: "replica"
# addr: "127.0.0.1:3306"
# database: "go-scaffold"
# username: "root"
# password: "root"
# options: "charset=utf8mb4&parseTime=True&loc=Local"
# dsn: "root:root@tcp(127.0.0.1:3306)/go-scaffold?charset=utf8mb4&parseTime=True&loc=Local"

# driver: "postgres"
# addr: "127.0.0.1:5432"
# database: "go-scaffold"
# username: "postgres"
# password: "root"
# options: "sslmode=disable&TimeZone=Asia/Shanghai"
# dsn: "host=127.0.0.1 port=5432 user=postgres password=root dbname=go-scaffold sslmode=disable&TimeZone=Asia/Shanghai"
# maxIdleConn: 20
# maxOpenConn: 40
# connMaxIdleTime: 120
# connMaxLifeTime: 120
# logInfo: true
# resolvers:
# - type: "replica"
# addr: "127.0.0.1:5432"
# database: "go-scaffold"
# username: "postgres"
# password: "root"
# options: "sslmode=disable&TimeZone=Asia/Shanghai"
# dsn: "host=127.0.0.1 port=5432 user=postgres password=root dbname=go-scaffold sslmode=disable&TimeZone=Asia/Shanghai"

##################### database #####################

Expand Down
32 changes: 4 additions & 28 deletions internal/app/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"errors"
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
_ "github.com/jackc/pgx"
Expand All @@ -21,9 +21,7 @@ func New(ctx context.Context, conf config.DBConn) (*sql.DB, error) {
return nil, ErrUnsupportedDriver
}

dsn := buildDSN(conf)

db, err := sql.Open(conf.Driver.String(), dsn)
db, err := sql.Open(conf.Driver.String(), conf.DSN)
if err != nil {
return nil, err
}
Expand All @@ -37,11 +35,11 @@ func New(ctx context.Context, conf config.DBConn) (*sql.DB, error) {
}

if conf.ConnMaxIdleTime > 0 {
db.SetConnMaxLifetime(conf.ConnMaxIdleTime)
db.SetConnMaxLifetime(conf.ConnMaxIdleTime * time.Second)
}

if conf.ConnMaxLifeTime > 0 {
db.SetConnMaxLifetime(conf.ConnMaxLifeTime)
db.SetConnMaxLifetime(conf.ConnMaxLifeTime * time.Second)
}

if err := db.PingContext(ctx); err != nil {
Expand All @@ -50,25 +48,3 @@ func New(ctx context.Context, conf config.DBConn) (*sql.DB, error) {

return db, nil
}

func buildDSN(c config.DBConn) string {
dsn := ""

switch c.Driver {
case config.Postgres:
var host, port string
s := strings.SplitN(c.Addr, ":", 2)
if len(s) == 2 {
host = s[0]
port = s[1]
}

dsn = "host=" + host + " port=" + port + " user=" + c.Username + " password=" + c.Password + " dbname=" + c.Database + " " + c.Options
case config.MySQL:
fallthrough
default:
dsn = c.Username + ":" + c.Password + "@tcp(" + c.Addr + ")/" + c.Database + "?" + c.Options
}

return dsn
}
25 changes: 9 additions & 16 deletions internal/app/pkg/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,20 @@ func registerResolver(ctx context.Context, gdb *gorm.DB, conf config.DB) error {
for _, rc := range rcs {
rc.Driver = conn.Driver

rcp := conn.DBConnPool
if rc.MaxIdleConn > 0 {
rcp.MaxIdleConn = rc.MaxIdleConn
if rc.MaxIdleConn == 0 {
rc.MaxIdleConn = conn.MaxIdleConn
}
if rc.MaxOpenConn > 0 {
rcp.MaxOpenConn = rc.MaxOpenConn
if rc.MaxOpenConn == 0 {
rc.MaxOpenConn = conn.MaxOpenConn
}
if rc.ConnMaxIdleTime > 0 {
rcp.ConnMaxIdleTime = rc.ConnMaxIdleTime
if rc.ConnMaxIdleTime == 0 {
rc.ConnMaxIdleTime = conn.ConnMaxIdleTime
}
if rc.ConnMaxLifeTime > 0 {
rcp.ConnMaxLifeTime = rc.ConnMaxLifeTime
if rc.ConnMaxLifeTime == 0 {
rc.ConnMaxLifeTime = conn.ConnMaxLifeTime
}

resolvers = append(resolvers, &config.DBResolver{
Type: rc.Type,
DBConn: config.DBConn{
DBDsn: rc.DBDsn,
DBConnPool: rcp,
},
})
resolvers = append(resolvers, rc)
}

plugin, err := buildResolver(ctx, resolvers)
Expand Down
38 changes: 13 additions & 25 deletions internal/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,35 @@ func (DB) GetName() string {

// DBConn database connection config
type DBConn struct {
DBDsn
DBConnPool
}

// DBDsn database dsn config
type DBDsn struct {
Driver DBDriver `json:"driver"`
Addr string `json:"addr"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"password"`
Options string `json:"options"`
Driver DBDriver `json:"driver"`
DSN string `json:"dsn"`
MaxIdleConn int `json:"maxIdleConn"`
MaxOpenConn int `json:"maxOpenConn"`
ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"`
ConnMaxLifeTime time.Duration `json:"connMaxLifeTime"`
}

// EnableMultiStatement enable the execution of multi sql statement
func (d *DBDsn) EnableMultiStatement() error {
func (d *DBConn) EnableMultiStatement() error {
if d.Driver != MySQL {
return nil
}

options, err := url.ParseQuery(d.Options)
options, err := url.Parse(d.DSN)
if err != nil {
return err
}

if !options.Has("multiStatements") {
options.Set("multiStatements", "true")
if !options.Query().Has("multiStatements") {
q := options.Query()
q.Set("multiStatements", "true")
options.RawQuery = q.Encode()
}

d.Options = options.Encode()
d.DSN = options.String()
return nil
}

// DBConnPool database connection pool config
type DBConnPool struct {
MaxIdleConn int `json:"maxIdleConn"`
MaxOpenConn int `json:"maxOpenConn"`
ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"`
ConnMaxLifeTime time.Duration `json:"connMaxLifeTime"`
}

// DBDriver database driver type
type DBDriver string

Expand Down

0 comments on commit f34514a

Please sign in to comment.