mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
Cleanup pool
This commit is contained in:
@ -29,13 +29,13 @@ var _ = Describe("ConnPool", func() {
|
||||
|
||||
It("should unblock client when conn is removed", func() {
|
||||
// Reserve one connection.
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// Reserve all other connections.
|
||||
var cns []*pool.Conn
|
||||
for i := 0; i < 9; i++ {
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
cns = append(cns, cn)
|
||||
}
|
||||
@ -46,12 +46,11 @@ var _ = Describe("ConnPool", func() {
|
||||
defer GinkgoRecover()
|
||||
|
||||
started <- true
|
||||
_, _, err := connPool.Get()
|
||||
_, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
done <- true
|
||||
|
||||
err = connPool.Put(cn)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
connPool.Put(cn)
|
||||
}()
|
||||
<-started
|
||||
|
||||
@ -59,14 +58,13 @@ var _ = Describe("ConnPool", func() {
|
||||
select {
|
||||
case <-done:
|
||||
Fail("Get is not blocked")
|
||||
default:
|
||||
case <-time.After(time.Millisecond):
|
||||
// ok
|
||||
}
|
||||
|
||||
err = connPool.Remove(cn)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
connPool.Remove(cn)
|
||||
|
||||
// Check that Ping is unblocked.
|
||||
// Check that Get is unblocked.
|
||||
select {
|
||||
case <-done:
|
||||
// ok
|
||||
@ -75,8 +73,7 @@ var _ = Describe("ConnPool", func() {
|
||||
}
|
||||
|
||||
for _, cn := range cns {
|
||||
err = connPool.Put(cn)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
connPool.Put(cn)
|
||||
}
|
||||
})
|
||||
})
|
||||
@ -107,7 +104,7 @@ var _ = Describe("conns reaper", func() {
|
||||
// add stale connections
|
||||
idleConns = nil
|
||||
for i := 0; i < 3; i++ {
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
cn.SetUsedAt(time.Now().Add(-2 * idleTimeout))
|
||||
conns = append(conns, cn)
|
||||
@ -116,17 +113,17 @@ var _ = Describe("conns reaper", func() {
|
||||
|
||||
// add fresh connections
|
||||
for i := 0; i < 3; i++ {
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
conns = append(conns, cn)
|
||||
}
|
||||
|
||||
for _, cn := range conns {
|
||||
Expect(connPool.Put(cn)).NotTo(HaveOccurred())
|
||||
connPool.Put(cn)
|
||||
}
|
||||
|
||||
Expect(connPool.Len()).To(Equal(6))
|
||||
Expect(connPool.FreeLen()).To(Equal(6))
|
||||
Expect(connPool.IdleLen()).To(Equal(6))
|
||||
|
||||
n, err := connPool.ReapStaleConns()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
@ -136,14 +133,14 @@ var _ = Describe("conns reaper", func() {
|
||||
AfterEach(func() {
|
||||
_ = connPool.Close()
|
||||
Expect(connPool.Len()).To(Equal(0))
|
||||
Expect(connPool.FreeLen()).To(Equal(0))
|
||||
Expect(connPool.IdleLen()).To(Equal(0))
|
||||
Expect(len(closedConns)).To(Equal(len(conns)))
|
||||
Expect(closedConns).To(ConsistOf(conns))
|
||||
})
|
||||
|
||||
It("reaps stale connections", func() {
|
||||
Expect(connPool.Len()).To(Equal(3))
|
||||
Expect(connPool.FreeLen()).To(Equal(3))
|
||||
Expect(connPool.IdleLen()).To(Equal(3))
|
||||
})
|
||||
|
||||
It("does not reap fresh connections", func() {
|
||||
@ -161,36 +158,34 @@ var _ = Describe("conns reaper", func() {
|
||||
for j := 0; j < 3; j++ {
|
||||
var freeCns []*pool.Conn
|
||||
for i := 0; i < 3; i++ {
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cn).NotTo(BeNil())
|
||||
freeCns = append(freeCns, cn)
|
||||
}
|
||||
|
||||
Expect(connPool.Len()).To(Equal(3))
|
||||
Expect(connPool.FreeLen()).To(Equal(0))
|
||||
Expect(connPool.IdleLen()).To(Equal(0))
|
||||
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cn).NotTo(BeNil())
|
||||
conns = append(conns, cn)
|
||||
|
||||
Expect(connPool.Len()).To(Equal(4))
|
||||
Expect(connPool.FreeLen()).To(Equal(0))
|
||||
Expect(connPool.IdleLen()).To(Equal(0))
|
||||
|
||||
err = connPool.Remove(cn)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
connPool.Remove(cn)
|
||||
|
||||
Expect(connPool.Len()).To(Equal(3))
|
||||
Expect(connPool.FreeLen()).To(Equal(0))
|
||||
Expect(connPool.IdleLen()).To(Equal(0))
|
||||
|
||||
for _, cn := range freeCns {
|
||||
err := connPool.Put(cn)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
connPool.Put(cn)
|
||||
}
|
||||
|
||||
Expect(connPool.Len()).To(Equal(3))
|
||||
Expect(connPool.FreeLen()).To(Equal(3))
|
||||
Expect(connPool.IdleLen()).To(Equal(3))
|
||||
}
|
||||
})
|
||||
})
|
||||
@ -222,18 +217,18 @@ var _ = Describe("race", func() {
|
||||
|
||||
perform(C, func(id int) {
|
||||
for i := 0; i < N; i++ {
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if err == nil {
|
||||
Expect(connPool.Put(cn)).NotTo(HaveOccurred())
|
||||
connPool.Put(cn)
|
||||
}
|
||||
}
|
||||
}, func(id int) {
|
||||
for i := 0; i < N; i++ {
|
||||
cn, _, err := connPool.Get()
|
||||
cn, err := connPool.Get()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if err == nil {
|
||||
Expect(connPool.Remove(cn)).NotTo(HaveOccurred())
|
||||
connPool.Remove(cn)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user