2012-08-05 16:09:43 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2012-08-12 22:41:44 +04:00
|
|
|
|
|
|
|
"github.com/vmihailenco/bufio"
|
2012-08-05 16:09:43 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Conn struct {
|
2012-08-17 22:36:48 +04:00
|
|
|
RW io.ReadWriteCloser
|
|
|
|
Rd reader
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func NewConn(rw io.ReadWriteCloser) *Conn {
|
2012-08-05 16:09:43 +04:00
|
|
|
return &Conn{
|
2012-08-17 22:36:48 +04:00
|
|
|
RW: rw,
|
|
|
|
Rd: bufio.NewReaderSize(rw, 1024),
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
type ConnPool interface {
|
|
|
|
Get() (*Conn, bool, error)
|
2012-08-11 18:42:10 +04:00
|
|
|
Add(*Conn) error
|
|
|
|
Remove(*Conn) error
|
2012-08-06 12:33:49 +04:00
|
|
|
Len() int
|
2012-08-11 18:42:10 +04:00
|
|
|
Close() error
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type MultiConnPool struct {
|
2012-08-05 16:09:43 +04:00
|
|
|
Logger *log.Logger
|
|
|
|
cond *sync.Cond
|
|
|
|
conns []*Conn
|
|
|
|
OpenConn OpenConnFunc
|
|
|
|
CloseConn CloseConnFunc
|
2012-08-11 18:42:10 +04:00
|
|
|
cap, MaxCap int
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func NewMultiConnPool(openConn OpenConnFunc, closeConn CloseConnFunc, maxCap int) *MultiConnPool {
|
2012-08-05 16:09:43 +04:00
|
|
|
logger := log.New(
|
|
|
|
os.Stdout,
|
|
|
|
"redis.connpool: ",
|
|
|
|
log.Ldate|log.Ltime|log.Lshortfile,
|
|
|
|
)
|
2012-08-06 12:33:49 +04:00
|
|
|
return &MultiConnPool{
|
2012-08-05 16:09:43 +04:00
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
Logger: logger,
|
|
|
|
conns: make([]*Conn, 0),
|
|
|
|
OpenConn: openConn,
|
|
|
|
CloseConn: closeConn,
|
|
|
|
MaxCap: maxCap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
func (p *MultiConnPool) Get() (*Conn, bool, error) {
|
2012-08-05 16:09:43 +04:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
for len(p.conns) == 0 && p.cap >= p.MaxCap {
|
|
|
|
p.cond.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.conns) == 0 {
|
|
|
|
rw, err := p.OpenConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.cap++
|
|
|
|
return NewConn(rw), true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
last := len(p.conns) - 1
|
|
|
|
conn := p.conns[last]
|
|
|
|
p.conns[last] = nil
|
|
|
|
p.conns = p.conns[:last]
|
|
|
|
|
|
|
|
return conn, false, nil
|
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (p *MultiConnPool) Add(conn *Conn) error {
|
2012-08-05 16:09:43 +04:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.conns = append(p.conns, conn)
|
|
|
|
p.cond.Signal()
|
2012-08-11 18:42:10 +04:00
|
|
|
return nil
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (p *MultiConnPool) Remove(conn *Conn) error {
|
|
|
|
defer func() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
p.cap--
|
|
|
|
p.cond.Signal()
|
|
|
|
p.cond.L.Unlock()
|
|
|
|
}()
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
2012-08-11 18:42:10 +04:00
|
|
|
return p.closeConn(conn)
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
func (p *MultiConnPool) Len() int {
|
2012-08-05 16:09:43 +04:00
|
|
|
return len(p.conns)
|
|
|
|
}
|
2012-08-06 12:33:49 +04:00
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (p *MultiConnPool) Close() error {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
for _, conn := range p.conns {
|
|
|
|
err := p.closeConn(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.conns = make([]*Conn, 0)
|
|
|
|
p.cap = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MultiConnPool) closeConn(conn *Conn) error {
|
|
|
|
if p.CloseConn != nil {
|
|
|
|
err := p.CloseConn(conn.RW)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conn.RW.Close()
|
|
|
|
}
|
2012-08-09 18:06:26 +04:00
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-08-06 18:04:30 +04:00
|
|
|
type SingleConnPool struct {
|
2012-08-11 18:42:10 +04:00
|
|
|
mtx sync.Mutex
|
|
|
|
pool ConnPool
|
|
|
|
conn *Conn
|
|
|
|
isReusable bool
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func NewSingleConnPoolConn(pool ConnPool, conn *Conn, isReusable bool) *SingleConnPool {
|
2012-08-09 18:06:26 +04:00
|
|
|
return &SingleConnPool{
|
2012-08-11 18:42:10 +04:00
|
|
|
pool: pool,
|
|
|
|
conn: conn,
|
|
|
|
isReusable: isReusable,
|
2012-08-09 18:06:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func NewSingleConnPool(pool ConnPool, isReusable bool) *SingleConnPool {
|
|
|
|
return NewSingleConnPoolConn(pool, nil, isReusable)
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
2012-08-06 18:04:30 +04:00
|
|
|
func (p *SingleConnPool) Get() (*Conn, bool, error) {
|
2012-08-09 18:06:26 +04:00
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
|
|
|
if p.conn != nil {
|
|
|
|
return p.conn, false, nil
|
|
|
|
}
|
|
|
|
conn, isNew, err := p.pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
p.conn = conn
|
|
|
|
return p.conn, isNew, nil
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (p *SingleConnPool) Add(conn *Conn) error {
|
|
|
|
return nil
|
|
|
|
}
|
2012-08-06 12:33:49 +04:00
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (p *SingleConnPool) Remove(conn *Conn) error {
|
|
|
|
return nil
|
|
|
|
}
|
2012-08-06 12:33:49 +04:00
|
|
|
|
2012-08-06 18:04:30 +04:00
|
|
|
func (p *SingleConnPool) Len() int {
|
2012-08-06 12:33:49 +04:00
|
|
|
return 1
|
|
|
|
}
|
2012-08-09 18:06:26 +04:00
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (p *SingleConnPool) Close() error {
|
2012-08-09 18:06:26 +04:00
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2012-08-11 18:42:10 +04:00
|
|
|
|
|
|
|
if p.conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if p.isReusable {
|
|
|
|
err = p.pool.Add(p.conn)
|
|
|
|
} else {
|
|
|
|
err = p.pool.Remove(p.conn)
|
|
|
|
}
|
2012-08-09 18:06:26 +04:00
|
|
|
p.conn = nil
|
2012-08-11 18:42:10 +04:00
|
|
|
return err
|
2012-08-09 18:06:26 +04:00
|
|
|
}
|