forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
36 lines (32 loc) · 1.02 KB
/
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
package sql
import (
"github.com/go-kratos/kratos/pkg/log"
"github.com/go-kratos/kratos/pkg/net/netutil/breaker"
"github.com/go-kratos/kratos/pkg/time"
// database driver
_ "github.com/go-sql-driver/mysql"
)
// Config mysql config.
type Config struct {
DSN string // write data source name.
ReadDSN []string // read data source name.
Active int // pool
Idle int // pool
IdleTimeout time.Duration // connect max life time.
QueryTimeout time.Duration // query sql timeout
ExecTimeout time.Duration // execute sql timeout
TranTimeout time.Duration // transaction sql timeout
Breaker *breaker.Config // breaker
}
// NewMySQL new db and retry connection when has error.
func NewMySQL(c *Config) (db *DB) {
if c.QueryTimeout == 0 || c.ExecTimeout == 0 || c.TranTimeout == 0 {
panic("mysql must be set query/execute/transction timeout")
}
db, err := Open(c)
if err != nil {
log.Error("open mysql error(%v)", err)
panic(err)
}
return
}