import (
"database/sql"
"errors"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
var ConnPoll chan *Conn
var maxIdleConns = 10 // 最大空闲
var maxIdleTime = 10 // 最大空闲时间,10s
var maxOpenConns = 20 // 最大连接数
const (
host = "xxxxxx"
user = "xxxxxx"
pass = "xxxxxx"
db = "xxxxxx"
)
type Conn struct {
conn *sql.DB
lastUse time.Time
}
func addConn(conn *Conn) {
if ConnPoll == nil {
ConnPoll = make(chan *Conn, maxOpenConns)
}
if len(ConnPoll) >= maxOpenConns {
conn.conn.Close()
return
}
ConnPoll <- conn
}
func initDB() {
if len(ConnPoll) == 0 {
ConnPoll = make(chan *Conn, maxOpenConns)
for i := 0; i < maxOpenConns/2; i++ {
db, err := sql.Open("mysql", user+":"+pass+"@tcp("+host+":3306)/"+db+"?charset=utf8")
if err != nil {
log.Println(err)
}
addConn(&Conn{conn: db})
}
}
}
func GetConn() (conn *Conn, err error) {
if ConnPoll == nil || len(ConnPoll) == 0 {
initDB()
}
// 获取连接,超时控制
select {
case <-time.After(time.Second * time.Duration(3)):
err = errors.New("timeout")
case conn = <-ConnPoll:
}
return conn, nil
}
主要使用channel来实现。