pool: discard connection and log error when buffer has unread data.

This commit is contained in:
Vladimir Mihailenco 2014-03-04 20:40:55 +02:00
parent 75754ff436
commit f76984bbcf
1 changed files with 13 additions and 7 deletions

View File

@ -27,12 +27,14 @@ type pool interface {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type conn struct { type conn struct {
cn net.Conn cn net.Conn
rd reader rd reader
inUse bool inUse bool
usedAt time.Time usedAt time.Time
readTimeout, writeTimeout time.Duration readTimeout time.Duration
writeTimeout time.Duration
elem *list.Element elem *list.Element
} }
@ -164,8 +166,11 @@ func (p *connPool) Get() (*conn, bool, error) {
func (p *connPool) Put(cn *conn) error { func (p *connPool) Put(cn *conn) error {
if cn.rd.Buffered() != 0 { if cn.rd.Buffered() != 0 {
panic("redis: attempt to put connection with buffered data") b, _ := cn.rd.ReadN(cn.rd.Buffered())
glog.Errorf("redis: connection has unread data: %q", b)
return p.Remove(cn)
} }
if p.idleTimeout > 0 { if p.idleTimeout > 0 {
cn.usedAt = time.Now() cn.usedAt = time.Now()
} }
@ -180,6 +185,7 @@ func (p *connPool) Put(cn *conn) error {
p.idleNum++ p.idleNum++
p.cond.Signal() p.cond.Signal()
p.cond.L.Unlock() p.cond.L.Unlock()
return nil return nil
} }
@ -205,14 +211,14 @@ func (p *connPool) remove(cn *conn) error {
return cn.Close() return cn.Close()
} }
// Returns number of idle connections. // Len returns number of idle connections.
func (p *connPool) Len() int { func (p *connPool) Len() int {
defer p.cond.L.Unlock() defer p.cond.L.Unlock()
p.cond.L.Lock() p.cond.L.Lock()
return p.idleNum return p.idleNum
} }
// Returns number of connections in the pool. // Size returns number of connections in the pool.
func (p *connPool) Size() int { func (p *connPool) Size() int {
defer p.cond.L.Unlock() defer p.cond.L.Unlock()
p.cond.L.Lock() p.cond.L.Lock()