2016-03-12 15:42:12 +03:00
|
|
|
package pool_test
|
|
|
|
|
|
|
|
import (
|
2019-07-04 11:18:06 +03:00
|
|
|
"context"
|
2018-05-28 17:27:24 +03:00
|
|
|
"sync"
|
2016-03-12 15:42:12 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-07-15 17:27:11 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/pool"
|
2016-03-12 15:42:12 +03:00
|
|
|
)
|
|
|
|
|
2016-03-17 14:48:04 +03:00
|
|
|
var _ = Describe("ConnPool", func() {
|
2020-08-15 15:36:02 +03:00
|
|
|
ctx := context.Background()
|
2016-03-17 14:48:04 +03:00
|
|
|
var connPool *pool.ConnPool
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2017-06-29 14:26:52 +03:00
|
|
|
connPool = pool.NewConnPool(&pool.Options{
|
|
|
|
Dialer: dummyDialer,
|
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: time.Hour,
|
|
|
|
IdleTimeout: time.Millisecond,
|
|
|
|
IdleCheckFrequency: time.Millisecond,
|
|
|
|
})
|
2016-03-17 14:48:04 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
connPool.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should unblock client when conn is removed", func() {
|
|
|
|
// Reserve one connection.
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2016-03-17 14:48:04 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
// Reserve all other connections.
|
|
|
|
var cns []*pool.Conn
|
|
|
|
for i := 0; i < 9; i++ {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2016-03-17 14:48:04 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
cns = append(cns, cn)
|
|
|
|
}
|
|
|
|
|
|
|
|
started := make(chan bool, 1)
|
|
|
|
done := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
|
|
|
|
started <- true
|
2020-08-15 15:36:02 +03:00
|
|
|
_, err := connPool.Get(ctx)
|
2016-03-17 14:48:04 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
done <- true
|
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cn)
|
2016-03-17 14:48:04 +03:00
|
|
|
}()
|
|
|
|
<-started
|
|
|
|
|
|
|
|
// Check that Get is blocked.
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
Fail("Get is not blocked")
|
2018-05-28 17:27:24 +03:00
|
|
|
case <-time.After(time.Millisecond):
|
2016-03-17 14:48:04 +03:00
|
|
|
// ok
|
|
|
|
}
|
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Remove(ctx, cn, nil)
|
2016-03-17 14:48:04 +03:00
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
// Check that Get is unblocked.
|
2016-03-17 14:48:04 +03:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
// ok
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
Fail("Get is not unblocked")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cn := range cns {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cn)
|
2016-03-17 14:48:04 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
var _ = Describe("MinIdleConns", func() {
|
|
|
|
const poolSize = 100
|
2020-08-15 15:36:02 +03:00
|
|
|
ctx := context.Background()
|
2018-05-28 17:27:24 +03:00
|
|
|
var minIdleConns int
|
|
|
|
var connPool *pool.ConnPool
|
|
|
|
|
|
|
|
newConnPool := func() *pool.ConnPool {
|
|
|
|
connPool := pool.NewConnPool(&pool.Options{
|
|
|
|
Dialer: dummyDialer,
|
|
|
|
PoolSize: poolSize,
|
|
|
|
MinIdleConns: minIdleConns,
|
|
|
|
PoolTimeout: 100 * time.Millisecond,
|
|
|
|
IdleTimeout: -1,
|
|
|
|
IdleCheckFrequency: -1,
|
|
|
|
})
|
|
|
|
Eventually(func() int {
|
|
|
|
return connPool.Len()
|
|
|
|
}).Should(Equal(minIdleConns))
|
|
|
|
return connPool
|
|
|
|
}
|
|
|
|
|
|
|
|
assert := func() {
|
|
|
|
It("has idle connections when created", func() {
|
|
|
|
Expect(connPool.Len()).To(Equal(minIdleConns))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(minIdleConns))
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("after Get", func() {
|
|
|
|
var cn *pool.Conn
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err = connPool.Get(ctx)
|
2018-05-28 17:27:24 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return connPool.Len()
|
|
|
|
}).Should(Equal(minIdleConns + 1))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("has idle connections", func() {
|
|
|
|
Expect(connPool.Len()).To(Equal(minIdleConns + 1))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(minIdleConns))
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("after Remove", func() {
|
|
|
|
BeforeEach(func() {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Remove(ctx, cn, nil)
|
2018-05-28 17:27:24 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("has idle connections", func() {
|
|
|
|
Expect(connPool.Len()).To(Equal(minIdleConns))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(minIdleConns))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Get does not exceed pool size", func() {
|
|
|
|
var mu sync.RWMutex
|
|
|
|
var cns []*pool.Conn
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
cns = make([]*pool.Conn, 0)
|
|
|
|
|
|
|
|
perform(poolSize, func(_ int) {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2018-05-28 17:27:24 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
mu.Lock()
|
|
|
|
cns = append(cns, cn)
|
|
|
|
mu.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return connPool.Len()
|
|
|
|
}).Should(BeNumerically(">=", poolSize))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Get is blocked", func() {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Get(ctx)
|
2018-05-28 17:27:24 +03:00
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
Fail("Get is not blocked")
|
|
|
|
case <-time.After(time.Millisecond):
|
|
|
|
// ok
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
// ok
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
Fail("Get is not unblocked")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("after Put", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
perform(len(cns), func(i int) {
|
|
|
|
mu.RLock()
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cns[i])
|
2018-05-28 17:27:24 +03:00
|
|
|
mu.RUnlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return connPool.Len()
|
|
|
|
}).Should(Equal(poolSize))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("pool.Len is back to normal", func() {
|
|
|
|
Expect(connPool.Len()).To(Equal(poolSize))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(poolSize))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("after Remove", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
perform(len(cns), func(i int) {
|
|
|
|
mu.RLock()
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Remove(ctx, cns[i], nil)
|
2018-05-28 17:27:24 +03:00
|
|
|
mu.RUnlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
Eventually(func() int {
|
|
|
|
return connPool.Len()
|
|
|
|
}).Should(Equal(minIdleConns))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("has idle connections", func() {
|
|
|
|
Expect(connPool.Len()).To(Equal(minIdleConns))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(minIdleConns))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Context("minIdleConns = 1", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
minIdleConns = 1
|
|
|
|
connPool = newConnPool()
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
connPool.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
assert()
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("minIdleConns = 32", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
minIdleConns = 32
|
|
|
|
connPool = newConnPool()
|
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
connPool.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
assert()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
var _ = Describe("conns reaper", func() {
|
2016-09-10 16:54:49 +03:00
|
|
|
const idleTimeout = time.Minute
|
2018-08-12 10:08:21 +03:00
|
|
|
const maxAge = time.Hour
|
2016-09-10 16:54:49 +03:00
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
ctx := context.Background()
|
2016-03-12 15:42:12 +03:00
|
|
|
var connPool *pool.ConnPool
|
2018-08-12 10:08:21 +03:00
|
|
|
var conns, staleConns, closedConns []*pool.Conn
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
assert := func(typ string) {
|
|
|
|
BeforeEach(func() {
|
|
|
|
closedConns = nil
|
|
|
|
connPool = pool.NewConnPool(&pool.Options{
|
|
|
|
Dialer: dummyDialer,
|
|
|
|
PoolSize: 10,
|
|
|
|
IdleTimeout: idleTimeout,
|
|
|
|
MaxConnAge: maxAge,
|
|
|
|
PoolTimeout: time.Second,
|
|
|
|
IdleCheckFrequency: time.Hour,
|
|
|
|
OnClose: func(cn *pool.Conn) error {
|
|
|
|
closedConns = append(closedConns, cn)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
conns = nil
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
// add stale connections
|
|
|
|
staleConns = nil
|
|
|
|
for i := 0; i < 3; i++ {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
switch typ {
|
|
|
|
case "idle":
|
|
|
|
cn.SetUsedAt(time.Now().Add(-2 * idleTimeout))
|
|
|
|
case "aged":
|
2019-03-25 14:02:31 +03:00
|
|
|
cn.SetCreatedAt(time.Now().Add(-2 * maxAge))
|
2021-07-15 17:18:31 +03:00
|
|
|
case "conncheck":
|
|
|
|
cn.Close()
|
2018-08-12 10:08:21 +03:00
|
|
|
}
|
|
|
|
conns = append(conns, cn)
|
|
|
|
staleConns = append(staleConns, cn)
|
|
|
|
}
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
// add fresh connections
|
|
|
|
for i := 0; i < 3; i++ {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
conns = append(conns, cn)
|
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
for _, cn := range conns {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cn)
|
2018-08-12 10:08:21 +03:00
|
|
|
}
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(connPool.Len()).To(Equal(6))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(6))
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
n, err := connPool.ReapStaleConns()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(3))
|
|
|
|
})
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
AfterEach(func() {
|
|
|
|
_ = connPool.Close()
|
|
|
|
Expect(connPool.Len()).To(Equal(0))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(0))
|
|
|
|
Expect(len(closedConns)).To(Equal(len(conns)))
|
|
|
|
Expect(closedConns).To(ConsistOf(conns))
|
|
|
|
})
|
2016-03-17 14:48:04 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
It("reaps stale connections", func() {
|
|
|
|
Expect(connPool.Len()).To(Equal(3))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(3))
|
|
|
|
})
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
It("does not reap fresh connections", func() {
|
|
|
|
n, err := connPool.ReapStaleConns()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(0))
|
|
|
|
})
|
2016-09-10 16:54:49 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
It("stale connections are closed", func() {
|
|
|
|
Expect(len(closedConns)).To(Equal(len(staleConns)))
|
|
|
|
Expect(closedConns).To(ConsistOf(staleConns))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("pool is functional", func() {
|
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
var freeCns []*pool.Conn
|
|
|
|
for i := 0; i < 3; i++ {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cn).NotTo(BeNil())
|
|
|
|
freeCns = append(freeCns, cn)
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(connPool.Len()).To(Equal(3))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(0))
|
2016-09-10 16:54:49 +03:00
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2016-03-17 19:00:47 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-12 15:42:12 +03:00
|
|
|
Expect(cn).NotTo(BeNil())
|
2018-08-12 10:08:21 +03:00
|
|
|
conns = append(conns, cn)
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(connPool.Len()).To(Equal(4))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(0))
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Remove(ctx, cn, nil)
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(connPool.Len()).To(Equal(3))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(0))
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
for _, cn := range freeCns {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cn)
|
2018-08-12 10:08:21 +03:00
|
|
|
}
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
Expect(connPool.Len()).To(Equal(3))
|
|
|
|
Expect(connPool.IdleLen()).To(Equal(3))
|
2016-03-12 15:42:12 +03:00
|
|
|
}
|
2018-08-12 10:08:21 +03:00
|
|
|
})
|
|
|
|
}
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
assert("idle")
|
|
|
|
assert("aged")
|
2021-07-15 17:18:31 +03:00
|
|
|
assert("conncheck")
|
2016-03-12 15:42:12 +03:00
|
|
|
})
|
2016-03-17 14:48:04 +03:00
|
|
|
|
|
|
|
var _ = Describe("race", func() {
|
2020-08-15 15:36:02 +03:00
|
|
|
ctx := context.Background()
|
2016-03-17 14:48:04 +03:00
|
|
|
var connPool *pool.ConnPool
|
2016-03-17 19:00:47 +03:00
|
|
|
var C, N int
|
2016-03-17 14:48:04 +03:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2016-03-17 19:00:47 +03:00
|
|
|
C, N = 10, 1000
|
|
|
|
if testing.Short() {
|
|
|
|
C = 4
|
|
|
|
N = 100
|
|
|
|
}
|
2016-03-17 14:48:04 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
connPool.Close()
|
|
|
|
})
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
It("does not happen on Get, Put, and Remove", func() {
|
2017-06-29 14:26:52 +03:00
|
|
|
connPool = pool.NewConnPool(&pool.Options{
|
|
|
|
Dialer: dummyDialer,
|
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: time.Minute,
|
|
|
|
IdleTimeout: time.Millisecond,
|
|
|
|
IdleCheckFrequency: time.Millisecond,
|
|
|
|
})
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2016-03-17 14:48:04 +03:00
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2016-03-17 19:00:47 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-17 14:48:04 +03:00
|
|
|
if err == nil {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Put(ctx, cn)
|
2016-03-17 14:48:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
2020-08-15 15:36:02 +03:00
|
|
|
cn, err := connPool.Get(ctx)
|
2016-03-17 19:00:47 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-03-17 14:48:04 +03:00
|
|
|
if err == nil {
|
2020-08-15 15:36:02 +03:00
|
|
|
connPool.Remove(ctx, cn, nil)
|
2016-03-17 14:48:04 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
})
|
|
|
|
})
|
2016-03-17 14:48:04 +03:00
|
|
|
})
|