Merge pull request #1112 from patrickwhite256/master

Close single conn connection pool
This commit is contained in:
Vladimir Mihailenco 2019-08-03 17:17:52 +03:00 committed by GitHub
commit d6a99e7be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package internal
import ( import (
"context" "context"
"errors"
"io" "io"
"net" "net"
"strings" "strings"
@ -9,6 +10,8 @@ import (
"github.com/go-redis/redis/internal/proto" "github.com/go-redis/redis/internal/proto"
) )
var ErrSingleConnPoolClosed = errors.New("redis: SingleConnPool is closed")
func IsRetryableError(err error, retryTimeout bool) bool { func IsRetryableError(err error, retryTimeout bool) bool {
switch err { switch err {
case nil, context.Canceled, context.DeadlineExceeded: case nil, context.Canceled, context.DeadlineExceeded:
@ -22,6 +25,10 @@ func IsRetryableError(err error, retryTimeout bool) bool {
} }
return true return true
} }
if err == ErrSingleConnPoolClosed {
return true
}
s := err.Error() s := err.Error()
if s == "ERR max number of clients reached" { if s == "ERR max number of clients reached" {
return true return true

View File

@ -1,9 +1,14 @@
package pool package pool
import "context" import (
"context"
"github.com/go-redis/redis/internal"
)
type SingleConnPool struct { type SingleConnPool struct {
cn *Conn cn *Conn
cnClosed bool
} }
var _ Pooler = (*SingleConnPool)(nil) var _ Pooler = (*SingleConnPool)(nil)
@ -23,6 +28,9 @@ func (p *SingleConnPool) CloseConn(*Conn) error {
} }
func (p *SingleConnPool) Get(ctx context.Context) (*Conn, error) { func (p *SingleConnPool) Get(ctx context.Context) (*Conn, error) {
if p.cnClosed {
return nil, internal.ErrSingleConnPoolClosed
}
return p.cn, nil return p.cn, nil
} }
@ -36,9 +44,13 @@ func (p *SingleConnPool) Remove(cn *Conn) {
if p.cn != cn { if p.cn != cn {
panic("p.cn != cn") panic("p.cn != cn")
} }
p.cnClosed = true
} }
func (p *SingleConnPool) Len() int { func (p *SingleConnPool) Len() int {
if p.cnClosed {
return 0
}
return 1 return 1
} }