Skip to content

Commit

Permalink
few more db config options
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Sep 21, 2024
1 parent 6b94211 commit 3d2eb3a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
8 changes: 8 additions & 0 deletions config_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ database_info:
password: "12345"
db: "plugnmeet"
prefix: "pnm_"
# https://github.com/go-sql-driver/mysql?tab=readme-ov-file#charset
charset: "utf8mb4"
# https://github.com/go-sql-driver/mysql?tab=readme-ov-file#loc
loc: "UTC"
# default 4 minutes
conn_max_lifetime: 4m
# default 10 connections
max_open_conns: 10
nats_info:
nats_urls:
- "nats://host.docker.internal:4222"
Expand Down
18 changes: 11 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,17 @@ type CopyrightConf struct {
}

type DatabaseInfo struct {
DriverName string `yaml:"driver_name"`
Host string `yaml:"host"`
Port int32 `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DBName string `yaml:"db"`
Prefix string `yaml:"prefix"`
DriverName string `yaml:"driver_name"`
Host string `yaml:"host"`
Port int32 `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DBName string `yaml:"db"`
Prefix string `yaml:"prefix"`
Charset *string `yaml:"charset"`
Loc *string `yaml:"loc"`
ConnMaxLifetime *time.Duration `yaml:"conn_max_lifetime"`
MaxOpenConns *int `yaml:"max_open_conns"`
}

type RedisInfo struct {
Expand Down
28 changes: 24 additions & 4 deletions pkg/factory/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"strings"
"time"
)

func NewDatabaseConnection(appCnf *config.AppConfig) error {
info := appCnf.DatabaseInfo
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=UTC", info.Username, info.Password, info.Host, info.Port, info.DBName)
charset := "utf8mb4"
loc := "UTC"

if info.Charset != nil && *info.Charset != "" {
charset = *info.Charset
}
if info.Loc != nil && *info.Loc != "" {
loc = strings.ReplaceAll(*info.Loc, "/", "%2F")
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s", info.Username, info.Password, info.Host, info.Port, info.DBName, charset, loc)

mysqlCnf := mysql.Config{
DSN: dsn, // data source name
}

cnf := &gorm.Config{}

if !appCnf.Client.Debug {
Expand Down Expand Up @@ -45,8 +54,19 @@ func NewDatabaseConnection(appCnf *config.AppConfig) error {
return err
}

d.SetConnMaxLifetime(time.Minute * 4)
d.SetMaxOpenConns(100)
connMaxLifetime := time.Minute * 4
if info.ConnMaxLifetime != nil && *info.ConnMaxLifetime > 0 {
connMaxLifetime = *info.ConnMaxLifetime
}
maxOpenConns := 10
if info.MaxOpenConns != nil && *info.MaxOpenConns > 0 {
maxOpenConns = *info.MaxOpenConns
}

// https://github.com/go-sql-driver/mysql?tab=readme-ov-file#important-settings
d.SetConnMaxLifetime(connMaxLifetime)
d.SetMaxOpenConns(maxOpenConns)
d.SetMaxIdleConns(maxOpenConns)

appCnf.DB = db
return nil
Expand Down

0 comments on commit 3d2eb3a

Please sign in to comment.