连接池测试通过
This commit is contained in:
parent
fb31cee848
commit
dca451bb21
|
|
@ -15,3 +15,4 @@
|
|||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
go.sum
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
module myschools.me/suguo/yy-mysql
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
gorm.io/driver/mysql v1.1.2
|
||||
gorm.io/gorm v1.21.13
|
||||
gorm.io/plugin/dbresolver v1.1.0
|
||||
)
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
var (
|
||||
_db *gorm.DB
|
||||
_conf *Config
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ConnString string
|
||||
ConnMaxLifetime int64 //ConnMaxLifetime 最大连接时间,单位:小时
|
||||
MaxIdleConns int
|
||||
MaxOpenConns int
|
||||
}
|
||||
|
||||
//New 创建实例
|
||||
func New(conf *Config) (*gorm.DB, error) {
|
||||
if conf != nil {
|
||||
_conf = conf
|
||||
}
|
||||
|
||||
if _db != nil {
|
||||
return _db, nil
|
||||
}
|
||||
|
||||
_db, err := gorm.Open(mysql.Open(_conf.ConnString), &gorm.Config{
|
||||
SkipDefaultTransaction: true,
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_db.Use(
|
||||
dbresolver.Register(dbresolver.Config{
|
||||
Sources: []gorm.Dialector{mysql.Open(_conf.ConnString)},
|
||||
Replicas: []gorm.Dialector{mysql.Open(_conf.ConnString)},
|
||||
Policy: dbresolver.RandomPolicy{},
|
||||
}).SetConnMaxIdleTime(time.Hour).
|
||||
SetConnMaxLifetime(time.Duration(_conf.ConnMaxLifetime) * time.Hour).
|
||||
SetMaxIdleConns(_conf.MaxIdleConns).
|
||||
SetMaxOpenConns(_conf.MaxOpenConns))
|
||||
return _db, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Born *time.Time
|
||||
}
|
||||
|
||||
func TestMysql(t *testing.T) {
|
||||
connstring := "root:root@tcp(i.myschools.me:6006)/sandbox?charset=utf8&parseTime=True&loc=Local"
|
||||
db, err := New(&Config{
|
||||
ConnString: connstring,
|
||||
ConnMaxLifetime: 0,
|
||||
MaxIdleConns: 200,
|
||||
MaxOpenConns: 200,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.AutoMigrate(&User{})
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
go func(mydb *gorm.DB, ii int) {
|
||||
for j := 0; j < 100; j++ {
|
||||
err := mydb.Create(&User{
|
||||
Name: fmt.Sprintf("cccc%d-%d", ii, j),
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err.Error)
|
||||
}
|
||||
}
|
||||
}(db, i)
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Second)
|
||||
}
|
||||
|
||||
func TestMysql2(t *testing.T) {
|
||||
connstring := "aaaa:root@tcp(i.myschools.me:3306)/sandbox?charset=utf8&parseTime=True&loc=Local"
|
||||
db, err := New(&Config{
|
||||
ConnString: connstring,
|
||||
ConnMaxLifetime: 0,
|
||||
MaxIdleConns: 200,
|
||||
MaxOpenConns: 200,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.AutoMigrate(&User{})
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
go func(ii int) {
|
||||
mydb, err := New(nil)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
for j := 0; j < 100; j++ {
|
||||
if err := mydb.Create(&User{
|
||||
Name: fmt.Sprintf("eeee-%d-%d", ii, j),
|
||||
}).Error; err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Second)
|
||||
}
|
||||
Loading…
Reference in New Issue