2012-08-05 16:09:43 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2012-08-09 14:12:41 +04:00
|
|
|
"bufio"
|
2012-08-05 16:09:43 +04:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Conn struct {
|
|
|
|
RW io.ReadWriter
|
2012-08-09 14:12:41 +04:00
|
|
|
Rd *bufio.Reader
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(rw io.ReadWriter) *Conn {
|
|
|
|
return &Conn{
|
|
|
|
RW: rw,
|
2012-08-09 14:12:41 +04:00
|
|
|
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)
|
|
|
|
Add(*Conn)
|
|
|
|
Remove(*Conn)
|
|
|
|
Len() int
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type MultiConnPool struct {
|
2012-08-05 16:09:43 +04:00
|
|
|
Logger *log.Logger
|
|
|
|
cond *sync.Cond
|
|
|
|
conns []*Conn
|
|
|
|
OpenConn OpenConnFunc
|
|
|
|
CloseConn CloseConnFunc
|
|
|
|
cap, MaxCap int64
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:33:49 +04:00
|
|
|
func NewMultiConnPool(openConn OpenConnFunc, closeConn CloseConnFunc, maxCap int64) *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-06 12:33:49 +04:00
|
|
|
func (p *MultiConnPool) Add(conn *Conn) {
|
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-06 12:33:49 +04:00
|
|
|
func (p *MultiConnPool) Remove(conn *Conn) {
|
2012-08-05 16:09:43 +04:00
|
|
|
p.cond.L.Lock()
|
|
|
|
p.cap--
|
|
|
|
p.cond.Signal()
|
|
|
|
p.cond.L.Unlock()
|
|
|
|
|
|
|
|
if p.CloseConn != nil && conn != nil {
|
|
|
|
p.CloseConn(conn.RW)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06 18:04:30 +04:00
|
|
|
type SingleConnPool struct {
|
2012-08-06 12:33:49 +04:00
|
|
|
conn *Conn
|
|
|
|
}
|
|
|
|
|
2012-08-06 18:04:30 +04:00
|
|
|
func NewSingleConnPool(conn *Conn) *SingleConnPool {
|
|
|
|
return &SingleConnPool{conn: conn}
|
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-06 12:33:49 +04:00
|
|
|
return p.conn, false, nil
|
|
|
|
}
|
|
|
|
|
2012-08-06 18:04:30 +04:00
|
|
|
func (p *SingleConnPool) Add(conn *Conn) {}
|
2012-08-06 12:33:49 +04:00
|
|
|
|
2012-08-06 18:04:30 +04:00
|
|
|
func (p *SingleConnPool) Remove(conn *Conn) {}
|
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
|
|
|
|
}
|