forked from goinbox/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
53 lines (38 loc) · 768 Bytes
/
pool.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
package redis
import (
"github.com/goinbox/pool"
)
type PConfig struct {
pool.Config
NewClientFunc func() (*Client, error)
}
type Pool struct {
pl *pool.Pool
config *PConfig
}
func NewPool(config *PConfig) *Pool {
p := &Pool{
config: config,
}
config.NewConnFunc = p.newConn
config.KeepAliveFunc = keepAlive
p.pl = pool.NewPool(&p.config.Config)
return p
}
func (p *Pool) Get() (*Client, error) {
conn, err := p.pl.Get()
if err != nil {
return nil, err
}
return conn.(*Client), nil
}
func (p *Pool) Put(client *Client) error {
return p.pl.Put(client)
}
func (p *Pool) newConn() (pool.IConn, error) {
return p.config.NewClientFunc()
}
func keepAlive(conn pool.IConn) error {
client := conn.(*Client)
return client.Do("ping").Err
}