diff --git a/api/Makefile b/api/Makefile index 073f564a..663d2dba 100644 --- a/api/Makefile +++ b/api/Makefile @@ -2,7 +2,7 @@ api-gen: goctl api go -api desc/pcm.api -dir . -style gozero --home ../deploy/goctl Generate-AMD64: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o pcm-coordinator-api pcm-coordinator-api pcm.go + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o pcm-coordinator-api pcm.go Generate-ARM64: CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOARM=7 go build -o pcm-coordinator-api pcm.go diff --git a/api/internal/svc/servicecontext.go b/api/internal/svc/servicecontext.go index aab8a913..24035301 100644 --- a/api/internal/svc/servicecontext.go +++ b/api/internal/svc/servicecontext.go @@ -39,6 +39,7 @@ import ( "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/schema" + "time" ) type ServiceContext struct { @@ -84,6 +85,16 @@ func NewServiceContext(c config.Config) *ServiceContext { SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user` }, }) + sqlDB, err := dbEngin.DB() + // SetMaxIdleConns 设置空闲连接池中连接的最大数量 + sqlDB.SetMaxIdleConns(10) + + // SetMaxOpenConns 设置打开数据库连接的最大数量。 + sqlDB.SetMaxOpenConns(50) + + // SetConnMaxLifetime 设置了连接可复用的最大时间。 + sqlDB.SetConnMaxLifetime(time.Hour) + dockerClient, err := client.NewClientWithOpts() if err != nil { logx.Error(err.Error()) diff --git a/rpc/internal/svc/servicecontext.go b/rpc/internal/svc/servicecontext.go index 32043af5..cdce8c48 100644 --- a/rpc/internal/svc/servicecontext.go +++ b/rpc/internal/svc/servicecontext.go @@ -25,6 +25,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" + "time" ) type ServiceContext struct { @@ -42,8 +43,17 @@ func NewServiceContext(c config.Config) *ServiceContext { }, Logger: logger.Default.LogMode(logger.Warn), }) + sqlDB, err := dbEngin.DB() + // SetMaxIdleConns 设置空闲连接池中连接的最大数量 + sqlDB.SetMaxIdleConns(10) + + // SetMaxOpenConns 设置打开数据库连接的最大数量。 + sqlDB.SetMaxOpenConns(50) + + // SetConnMaxLifetime 设置了连接可复用的最大时间。 + sqlDB.SetConnMaxLifetime(time.Hour) //添加snowflake支持 - err := utils.InitSnowflake(c.SnowflakeConf.MachineId) + err = utils.InitSnowflake(c.SnowflakeConf.MachineId) if err != nil { logx.Errorf("InitSnowflake err: ", err) panic("InitSnowflake err")