chore: stabilize lint and verify builds
This commit is contained in:
@@ -2,6 +2,7 @@ package postgres
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -50,15 +51,19 @@ type Config struct {
|
||||
ApplicationName string // application_name
|
||||
}
|
||||
|
||||
func (m Config) GormSlowThreshold() time.Duration {
|
||||
if m.SlowThresholdMs == 0 {
|
||||
func (config Config) GormSlowThreshold() time.Duration {
|
||||
if config.SlowThresholdMs == 0 {
|
||||
return 200 * time.Millisecond // 默认200ms
|
||||
}
|
||||
return time.Duration(m.SlowThresholdMs) * time.Millisecond
|
||||
if config.SlowThresholdMs > math.MaxInt64/uint(time.Millisecond) {
|
||||
return time.Duration(math.MaxInt64)
|
||||
}
|
||||
|
||||
return time.Duration(config.SlowThresholdMs) * time.Millisecond
|
||||
}
|
||||
|
||||
func (m Config) GormLogLevel() logger.LogLevel {
|
||||
switch m.LogLevel {
|
||||
func (config Config) GormLogLevel() logger.LogLevel {
|
||||
switch config.LogLevel {
|
||||
case "silent":
|
||||
return logger.Silent
|
||||
case "error":
|
||||
@@ -72,65 +77,67 @@ func (m Config) GormLogLevel() logger.LogLevel {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Config) checkDefault() {
|
||||
if m.MaxIdleConns == 0 {
|
||||
m.MaxIdleConns = 10
|
||||
func (config *Config) checkDefault() {
|
||||
if config.MaxIdleConns == 0 {
|
||||
config.MaxIdleConns = 10
|
||||
}
|
||||
|
||||
if m.MaxOpenConns == 0 {
|
||||
m.MaxOpenConns = 100
|
||||
if config.MaxOpenConns == 0 {
|
||||
config.MaxOpenConns = 100
|
||||
}
|
||||
|
||||
if m.Username == "" {
|
||||
m.Username = "postgres"
|
||||
if config.Username == "" {
|
||||
config.Username = "postgres"
|
||||
}
|
||||
|
||||
if m.SslMode == "" {
|
||||
m.SslMode = "disable"
|
||||
if config.SslMode == "" {
|
||||
config.SslMode = "disable"
|
||||
}
|
||||
|
||||
if m.TimeZone == "" {
|
||||
m.TimeZone = "Asia/Shanghai"
|
||||
if config.TimeZone == "" {
|
||||
config.TimeZone = "Asia/Shanghai"
|
||||
}
|
||||
|
||||
if m.Port == 0 {
|
||||
m.Port = 5432
|
||||
if config.Port == 0 {
|
||||
config.Port = 5432
|
||||
}
|
||||
|
||||
if m.Schema == "" {
|
||||
m.Schema = "public"
|
||||
if config.Schema == "" {
|
||||
config.Schema = "public"
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Config) EmptyDsn() string {
|
||||
func (config *Config) EmptyDsn() string {
|
||||
// 基本 DSN
|
||||
dsnTpl := "host=%s user=%s password=%s port=%d dbname=%s sslmode=%s TimeZone=%s"
|
||||
m.checkDefault()
|
||||
base := fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port, m.Database, m.SslMode, m.TimeZone)
|
||||
config.checkDefault()
|
||||
base := fmt.Sprintf(dsnTpl, config.Host, config.Username, config.Password, config.Port, config.Database, config.SslMode, config.TimeZone)
|
||||
// 附加可选参数
|
||||
extras := ""
|
||||
if m.UseSearchPath && m.Schema != "" {
|
||||
extras += " search_path=" + m.Schema
|
||||
if config.UseSearchPath && config.Schema != "" {
|
||||
extras += " search_path=" + config.Schema
|
||||
}
|
||||
if m.ApplicationName != "" {
|
||||
extras += " application_name=" + strconv.Quote(m.ApplicationName)
|
||||
if config.ApplicationName != "" {
|
||||
extras += " application_name=" + strconv.Quote(config.ApplicationName)
|
||||
}
|
||||
|
||||
return base + extras
|
||||
}
|
||||
|
||||
// DSN connection dsn
|
||||
func (m *Config) DSN() string {
|
||||
func (config *Config) DSN() string {
|
||||
// 基本 DSN
|
||||
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
||||
m.checkDefault()
|
||||
base := fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
||||
config.checkDefault()
|
||||
base := fmt.Sprintf(dsnTpl, config.Host, config.Username, config.Password, config.Database, config.Port, config.SslMode, config.TimeZone)
|
||||
// 附加可选参数
|
||||
extras := ""
|
||||
if m.UseSearchPath && m.Schema != "" {
|
||||
extras += " search_path=" + m.Schema
|
||||
if config.UseSearchPath && config.Schema != "" {
|
||||
extras += " search_path=" + config.Schema
|
||||
}
|
||||
if m.ApplicationName != "" {
|
||||
extras += " application_name=" + strconv.Quote(m.ApplicationName)
|
||||
if config.ApplicationName != "" {
|
||||
extras += " application_name=" + strconv.Quote(config.ApplicationName)
|
||||
}
|
||||
|
||||
return base + extras
|
||||
}
|
||||
|
||||
@@ -3,9 +3,10 @@ package postgres
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
logrus "github.com/sirupsen/logrus"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"gorm.io/driver/postgres"
|
||||
@@ -70,10 +71,18 @@ 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.ConnMaxLifetimeSeconds > math.MaxInt64/uint(time.Second) {
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(math.MaxInt64))
|
||||
} else {
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifetimeSeconds) * time.Second)
|
||||
}
|
||||
}
|
||||
if conf.ConnMaxIdleTimeSeconds > 0 {
|
||||
sqlDB.SetConnMaxIdleTime(time.Duration(conf.ConnMaxIdleTimeSeconds) * time.Second)
|
||||
if conf.ConnMaxIdleTimeSeconds > math.MaxInt64/uint(time.Second) {
|
||||
sqlDB.SetConnMaxIdleTime(time.Duration(math.MaxInt64))
|
||||
} else {
|
||||
sqlDB.SetConnMaxIdleTime(time.Duration(conf.ConnMaxIdleTimeSeconds) * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// Ping 校验
|
||||
|
||||
Reference in New Issue
Block a user