You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
6 years ago
|
package sql
|
||
|
|
||
|
import (
|
||
6 years ago
|
"github.com/bilibili/kratos/pkg/log"
|
||
|
"github.com/bilibili/kratos/pkg/net/netutil/breaker"
|
||
|
"github.com/bilibili/kratos/pkg/time"
|
||
6 years ago
|
|
||
|
// database driver
|
||
|
_ "github.com/go-sql-driver/mysql"
|
||
|
)
|
||
|
|
||
|
// Config mysql config.
|
||
|
type Config struct {
|
||
|
Addr string // for trace
|
||
|
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
|
||
|
}
|