forked from mirror/redis
Merge pull request #450 from go-redis/fix/remove-dial-limiter
Remove dial limiter.
This commit is contained in:
commit
a3eed908aa
|
@ -16,7 +16,6 @@ matrix:
|
||||||
- go: tip
|
- go: tip
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- go get gopkg.in/bsm/ratelimit.v1
|
|
||||||
- go get github.com/onsi/ginkgo
|
- go get github.com/onsi/ginkgo
|
||||||
- go get github.com/onsi/gomega
|
- go get github.com/onsi/gomega
|
||||||
- mkdir -p $HOME/gopath/src/gopkg.in
|
- mkdir -p $HOME/gopath/src/gopkg.in
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
|
|
||||||
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
|
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
|
||||||
connPool := pool.NewConnPool(dummyDialer, poolSize, time.Second, time.Hour, time.Hour)
|
connPool := pool.NewConnPool(dummyDialer, poolSize, time.Second, time.Hour, time.Hour)
|
||||||
connPool.DialLimiter = nil
|
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
|
@ -41,7 +40,6 @@ func BenchmarkPoolGetPut1000Conns(b *testing.B) {
|
||||||
|
|
||||||
func benchmarkPoolGetRemove(b *testing.B, poolSize int) {
|
func benchmarkPoolGetRemove(b *testing.B, poolSize int) {
|
||||||
connPool := pool.NewConnPool(dummyDialer, poolSize, time.Second, time.Hour, time.Hour)
|
connPool := pool.NewConnPool(dummyDialer, poolSize, time.Second, time.Hour, time.Hour)
|
||||||
connPool.DialLimiter = nil
|
|
||||||
removeReason := errors.New("benchmark")
|
removeReason := errors.New("benchmark")
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
|
@ -8,8 +8,6 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/bsm/ratelimit.v1"
|
|
||||||
|
|
||||||
"gopkg.in/redis.v5/internal"
|
"gopkg.in/redis.v5/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,9 +47,8 @@ type Pooler interface {
|
||||||
type dialer func() (net.Conn, error)
|
type dialer func() (net.Conn, error)
|
||||||
|
|
||||||
type ConnPool struct {
|
type ConnPool struct {
|
||||||
_dial dialer
|
dial dialer
|
||||||
DialLimiter *ratelimit.RateLimiter
|
OnClose func(*Conn) error
|
||||||
OnClose func(*Conn) error
|
|
||||||
|
|
||||||
poolTimeout time.Duration
|
poolTimeout time.Duration
|
||||||
idleTimeout time.Duration
|
idleTimeout time.Duration
|
||||||
|
@ -74,8 +71,7 @@ var _ Pooler = (*ConnPool)(nil)
|
||||||
|
|
||||||
func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout, idleCheckFrequency time.Duration) *ConnPool {
|
func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout, idleCheckFrequency time.Duration) *ConnPool {
|
||||||
p := &ConnPool{
|
p := &ConnPool{
|
||||||
_dial: dial,
|
dial: dial,
|
||||||
DialLimiter: ratelimit.New(3*poolSize, time.Second),
|
|
||||||
|
|
||||||
poolTimeout: poolTimeout,
|
poolTimeout: poolTimeout,
|
||||||
idleTimeout: idleTimeout,
|
idleTimeout: idleTimeout,
|
||||||
|
@ -90,23 +86,6 @@ func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout, idleCheckF
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) dial() (net.Conn, error) {
|
|
||||||
if p.DialLimiter != nil && p.DialLimiter.Limit() {
|
|
||||||
err := fmt.Errorf(
|
|
||||||
"redis: you open connections too fast (last_error=%q)",
|
|
||||||
p.loadLastErr(),
|
|
||||||
)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cn, err := p._dial()
|
|
||||||
if err != nil {
|
|
||||||
p.storeLastErr(err.Error())
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return cn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ConnPool) NewConn() (*Conn, error) {
|
func (p *ConnPool) NewConn() (*Conn, error) {
|
||||||
netConn, err := p.dial()
|
netConn, err := p.dial()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -292,7 +271,6 @@ func (p *ConnPool) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) closeConn(cn *Conn, reason error) error {
|
func (p *ConnPool) closeConn(cn *Conn, reason error) error {
|
||||||
p.storeLastErr(reason.Error())
|
|
||||||
if p.OnClose != nil {
|
if p.OnClose != nil {
|
||||||
_ = p.OnClose(cn)
|
_ = p.OnClose(cn)
|
||||||
}
|
}
|
||||||
|
@ -356,17 +334,6 @@ func (p *ConnPool) reaper(frequency time.Duration) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) storeLastErr(err string) {
|
|
||||||
p.lastErr.Store(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ConnPool) loadLastErr() string {
|
|
||||||
if v := p.lastErr.Load(); v != nil {
|
|
||||||
return v.(string)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
var idleCheckFrequency atomic.Value
|
var idleCheckFrequency atomic.Value
|
||||||
|
|
|
@ -23,21 +23,6 @@ var _ = Describe("ConnPool", func() {
|
||||||
connPool.Close()
|
connPool.Close()
|
||||||
})
|
})
|
||||||
|
|
||||||
It("rate limits dial", func() {
|
|
||||||
var rateErr error
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
cn, _, err := connPool.Get()
|
|
||||||
if err != nil {
|
|
||||||
rateErr = err
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = connPool.Remove(cn, errors.New("test"))
|
|
||||||
}
|
|
||||||
|
|
||||||
Expect(rateErr).To(MatchError(`redis: you open connections too fast (last_error="test")`))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("should unblock client when conn is removed", func() {
|
It("should unblock client when conn is removed", func() {
|
||||||
// Reserve one connection.
|
// Reserve one connection.
|
||||||
cn, _, err := connPool.Get()
|
cn, _, err := connPool.Get()
|
||||||
|
@ -220,7 +205,6 @@ var _ = Describe("race", func() {
|
||||||
It("does not happen on Get, Put, and Remove", func() {
|
It("does not happen on Get, Put, and Remove", func() {
|
||||||
connPool = pool.NewConnPool(
|
connPool = pool.NewConnPool(
|
||||||
dummyDialer, 10, time.Minute, time.Millisecond, time.Millisecond)
|
dummyDialer, 10, time.Minute, time.Millisecond, time.Millisecond)
|
||||||
connPool.DialLimiter = nil
|
|
||||||
|
|
||||||
perform(C, func(id int) {
|
perform(C, func(id int) {
|
||||||
for i := 0; i < N; i++ {
|
for i := 0; i < N; i++ {
|
||||||
|
@ -244,7 +228,6 @@ var _ = Describe("race", func() {
|
||||||
It("does not happen on Get and PopFree", func() {
|
It("does not happen on Get and PopFree", func() {
|
||||||
connPool = pool.NewConnPool(
|
connPool = pool.NewConnPool(
|
||||||
dummyDialer, 10, time.Minute, time.Second, time.Millisecond)
|
dummyDialer, 10, time.Minute, time.Second, time.Millisecond)
|
||||||
connPool.DialLimiter = nil
|
|
||||||
|
|
||||||
perform(C, func(id int) {
|
perform(C, func(id int) {
|
||||||
for i := 0; i < N; i++ {
|
for i := 0; i < N; i++ {
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
"gopkg.in/redis.v5"
|
"gopkg.in/redis.v5"
|
||||||
"gopkg.in/redis.v5/internal/pool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("pool", func() {
|
var _ = Describe("pool", func() {
|
||||||
|
@ -80,7 +79,6 @@ var _ = Describe("pool", func() {
|
||||||
|
|
||||||
It("respects max size on pubsub", func() {
|
It("respects max size on pubsub", func() {
|
||||||
connPool := client.Pool()
|
connPool := client.Pool()
|
||||||
connPool.(*pool.ConnPool).DialLimiter = nil
|
|
||||||
|
|
||||||
perform(1000, func(id int) {
|
perform(1000, func(id int) {
|
||||||
pubsub, err := client.Subscribe()
|
pubsub, err := client.Subscribe()
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
"gopkg.in/redis.v5"
|
"gopkg.in/redis.v5"
|
||||||
"gopkg.in/redis.v5/internal/pool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("races", func() {
|
var _ = Describe("races", func() {
|
||||||
|
@ -139,7 +138,6 @@ var _ = Describe("races", func() {
|
||||||
|
|
||||||
It("should PubSub", func() {
|
It("should PubSub", func() {
|
||||||
connPool := client.Pool()
|
connPool := client.Pool()
|
||||||
connPool.(*pool.ConnPool).DialLimiter = nil
|
|
||||||
|
|
||||||
perform(C, func(id int) {
|
perform(C, func(id int) {
|
||||||
for i := 0; i < N; i++ {
|
for i := 0; i < N; i++ {
|
||||||
|
|
Loading…
Reference in New Issue