From e747d60264b92f42e698c917631fd8e4316241c7 Mon Sep 17 00:00:00 2001 From: devad Date: Sat, 25 Nov 2023 16:20:04 +0800 Subject: [PATCH] =?UTF-8?q?gorm=20=E8=BF=9E=E6=8E=A5=E6=B1=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: devad Former-commit-id: 4ce1b2956b09efd0e10aa7eb1611794bddfe5e59 --- api/Makefile | 2 +- api/internal/svc/servicecontext.go | 11 +++++++++++ rpc/internal/svc/servicecontext.go | 12 +++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) 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")