-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_mysql.go
81 lines (74 loc) · 2.12 KB
/
conn_mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package xmysql
import (
"context"
"database/sql"
"fmt"
"github.com/jinares/xpkg/xlog"
"github.com/jinares/xpkg/xtools"
"google.golang.org/grpc/codes"
"sync"
"time"
)
type (
DBHandler func() (*sql.DB, error)
)
var (
mysqlMutex sync.RWMutex
mysqlPool = map[string]*sql.DB{}
)
func ConnMYSQL(item MYSQLConfig) (*sql.DB, error) {
if item.DNS == "" {
return nil, xtools.XErr(codes.Unimplemented, "dns is empty", true)
}
mysqlMutex.RLock()
mysqldb, isok := mysqlPool[item.DNS]
mysqlMutex.RUnlock()
if isok == true {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Second)
defer cancel()
err := mysqldb.PingContext(ctx)
if err != nil {
fmt.Println(fmt.Sprintf("mysql-ping-err:%s", err.Error()))
xlog.Err(err).WithField("data", mysqldb.Stats()).Error("mysql-ping")
return nil, xtools.MErr(err, codes.ResourceExhausted, "请稍后再试", true)
}
return mysqldb, nil
}
mysqlMutex.Lock()
defer mysqlMutex.Unlock()
//避免并发多次写入
mysqldb, isok = mysqlPool[item.DNS]
if isok {
return mysqldb, nil
}
//delete(mysqlPool, item.DNS)
mysqldb, err := sql.Open("mysql", item.DNS)
if err != nil {
fmt.Println(fmt.Sprintf("mysql-connection-err:%s", err.Error()))
xlog.Err(err).Error("mysql-connection")
return nil, xtools.XErr(codes.Internal, err.Error(), true)
}
mysqldb.SetMaxOpenConns(item.MaxPoolSize)
mysqldb.SetMaxIdleConns(item.MaxIdleConns)
if item.MaxIdleTime > 0 {
mysqldb.SetConnMaxIdleTime(time.Duration(item.MaxIdleTime) * time.Second)
}
if item.ConnMaxLifetime > 0 {
mysqldb.SetConnMaxLifetime(time.Duration(item.ConnMaxLifetime) * time.Second)
} else if item.ConnMaxLifetime < 0 {
mysqldb.SetConnMaxLifetime(30 * time.Second)
} else {
mysqldb.SetConnMaxLifetime(0)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err = mysqldb.PingContext(ctx)
if err != nil {
fmt.Println(fmt.Sprintf("mysql-ping2-err:%s", err.Error()))
//mysqldb.Close()
xlog.Err(err).Error("mysql-ping2")
return nil, xtools.MErr(err, codes.ResourceExhausted, "请稍后再试", true)
}
mysqlPool[item.DNS] = mysqldb
return mysqldb, nil
}