forked from xingskycn/epooll
/
pool_new.go
52 lines (44 loc) · 1.44 KB
/
pool_new.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
package epooll
import (
"fmt"
"strconv"
"github.com/garyburd/redigo/redis"
"github.com/owner888/epooll/conf"
)
func newRedisPool() *redis.Pool {
poolNum, _ := strconv.Atoi(conf.GetValue("pool", "redis"))
fmt.Printf("初始化 Redis 连接池,连接数:%d \n", poolNum)
return &redis.Pool{
MaxIdle: 80,
MaxActive: poolNum, // max number of connections
Dial: func() (redis.Conn, error) {
host := conf.GetValue("redis", "host")
port := conf.GetValue("redis", "port")
c, err := redis.Dial("tcp", host+":"+port)
//if err != nil {
//panic(err.Error())
//}
return c, err
},
}
}
// 不是当前package 的,每次都会重新初始化
//redisDB := epooll.NewRedisPool()
// 其他package的,用下面调用
//redisDB := RedisConn.Get()
func newMysqlPool() *ConnPool {
poolNum, _ := strconv.Atoi(conf.GetValue("pool", "mysql"))
fmt.Printf("初始化 Mysql 连接池,连接数:%d \n", poolNum)
return &ConnPool{
MaxActive: poolNum,
//Dial: func() (*autorc.Conn, error) {
Dial: func() (interface{}, error) {
//conn := autorc.New("tcp", "", "localhost:3306", "root", "root", "test")
//conn.Register("set names utf8")
db, err := InitDB()
return db, err
},
}
}
var RedisConn = newRedisPool()
var MysqlConn = newMysqlPool()