feat: 增强 PostgreSQL 配置,添加连接生命周期和日志配置选项

This commit is contained in:
Rogee
2025-09-11 16:26:47 +08:00
parent 55a335e9ad
commit da4875fc16
2 changed files with 105 additions and 12 deletions

View File

@@ -1,6 +1,9 @@
package postgres
import (
"context"
"time"
"github.com/sirupsen/logrus"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
@@ -18,10 +21,24 @@ func Provide(opts ...opt.Option) error {
}
return container.Container.Provide(func() (*gorm.DB, *Config, error) {
dbConfig := postgres.Config{
DSN: conf.DSN(), // DSN data source name
}
logrus.Info("Open PostgreSQL:", conf.DSN())
dbConfig := postgres.Config{DSN: conf.DSN()}
//
logrus.
WithFields(
logrus.Fields{
"host": conf.Host,
"port": conf.Port,
"db": conf.Database,
"schema": conf.Schema,
"ssl": conf.SslMode,
},
).
Info("opening PostgreSQL connection")
//
lvl := conf.GormLogLevel()
slow := conf.GormSlowThreshold()
gormConfig := gorm.Config{
NamingStrategy: schema.NamingStrategy{
@@ -29,11 +46,14 @@ func Provide(opts ...opt.Option) error {
SingularTable: conf.Singular,
},
DisableForeignKeyConstraintWhenMigrating: true,
PrepareStmt: conf.PrepareStmt,
SkipDefaultTransaction: conf.SkipDefaultTransaction,
Logger: logger.New(logrus.StandardLogger(), logger.Config{
SlowThreshold: 200, // SQL
LogLevel: logger.Info,
IgnoreRecordNotFoundError: true, // ErrRecordNotFound
SlowThreshold: slow,
LogLevel: lvl,
IgnoreRecordNotFoundError: true,
Colorful: false,
ParameterizedQueries: conf.ParameterizedQueries,
}),
}
@@ -48,7 +68,23 @@ func Provide(opts ...opt.Option) error {
}
sqlDB.SetMaxIdleConns(conf.MaxIdleConns)
sqlDB.SetMaxOpenConns(conf.MaxOpenConns)
if conf.ConnMaxLifetimeSeconds > 0 {
sqlDB.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifetimeSeconds) * time.Second)
}
if conf.ConnMaxIdleTimeSeconds > 0 {
sqlDB.SetConnMaxIdleTime(time.Duration(conf.ConnMaxIdleTimeSeconds) * time.Second)
}
return db, &conf, err
// Ping
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := sqlDB.PingContext(ctx); err != nil {
return nil, nil, err
}
//
container.AddCloseAble(func() { _ = sqlDB.Close() })
return db, &conf, nil
}, o.DiOptions()...)
}