From 6c72dc807ed4f63b195758c8d86aa24b4912f6cf Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 31 May 2019 17:54:30 +0300 Subject: [PATCH] internal/pool: replace atomic.Value with int64 --- cluster_test.go | 12 ------------ commands_test.go | 12 ++++++------ internal/pool/conn.go | 12 ++++++------ main_test.go | 12 ++++++------ pool_test.go | 9 ++++++--- pubsub_test.go | 4 ++-- redis_test.go | 2 ++ 7 files changed, 28 insertions(+), 35 deletions(-) diff --git a/cluster_test.go b/cluster_test.go index 3c0b201..ad3a6be 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -543,18 +543,6 @@ var _ = Describe("ClusterClient", func() { Expect(stats).To(BeAssignableToTypeOf(&redis.PoolStats{})) }) - It("removes idle connections", func() { - stats := client.PoolStats() - Expect(stats.TotalConns).NotTo(BeZero()) - Expect(stats.IdleConns).NotTo(BeZero()) - - time.Sleep(2 * time.Second) - - stats = client.PoolStats() - Expect(stats.TotalConns).To(BeZero()) - Expect(stats.IdleConns).To(BeZero()) - }) - It("returns an error when there are no attempts left", func() { opt := redisClusterOptions() opt.MaxRedirects = -1 diff --git a/commands_test.go b/commands_test.go index b8d7fd2..2633094 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1502,8 +1502,8 @@ var _ = Describe("Commands", func() { Expect(client.Ping().Err()).NotTo(HaveOccurred()) stats := client.PoolStats() - Expect(stats.Hits).To(Equal(uint32(1))) - Expect(stats.Misses).To(Equal(uint32(2))) + Expect(stats.Hits).To(Equal(uint32(2))) + Expect(stats.Misses).To(Equal(uint32(1))) Expect(stats.Timeouts).To(Equal(uint32(0))) }) @@ -2215,8 +2215,8 @@ var _ = Describe("Commands", func() { Expect(client.Ping().Err()).NotTo(HaveOccurred()) stats := client.PoolStats() - Expect(stats.Hits).To(Equal(uint32(1))) - Expect(stats.Misses).To(Equal(uint32(2))) + Expect(stats.Hits).To(Equal(uint32(2))) + Expect(stats.Misses).To(Equal(uint32(1))) Expect(stats.Timeouts).To(Equal(uint32(0))) }) @@ -2297,8 +2297,8 @@ var _ = Describe("Commands", func() { Expect(client.Ping().Err()).NotTo(HaveOccurred()) stats := client.PoolStats() - Expect(stats.Hits).To(Equal(uint32(1))) - Expect(stats.Misses).To(Equal(uint32(2))) + Expect(stats.Hits).To(Equal(uint32(2))) + Expect(stats.Misses).To(Equal(uint32(1))) Expect(stats.Timeouts).To(Equal(uint32(0))) }) diff --git a/internal/pool/conn.go b/internal/pool/conn.go index ac48113..78b29f5 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -13,14 +13,13 @@ var noDeadline = time.Time{} type Conn struct { netConn net.Conn - rd *proto.Reader - rdLocked bool - wr *proto.Writer + rd *proto.Reader + wr *proto.Writer Inited bool pooled bool createdAt time.Time - usedAt atomic.Value + usedAt int64 // atomic } func NewConn(netConn net.Conn) *Conn { @@ -35,11 +34,12 @@ func NewConn(netConn net.Conn) *Conn { } func (cn *Conn) UsedAt() time.Time { - return cn.usedAt.Load().(time.Time) + unix := atomic.LoadInt64(&cn.usedAt) + return time.Unix(unix, 0) } func (cn *Conn) SetUsedAt(tm time.Time) { - cn.usedAt.Store(tm) + atomic.StoreInt64(&cn.usedAt, tm.Unix()) } func (cn *Conn) SetNetConn(netConn net.Conn) { diff --git a/main_test.go b/main_test.go index e6a372b..7a7f0be 100644 --- a/main_test.go +++ b/main_test.go @@ -113,8 +113,8 @@ func redisOptions() *redis.Options { WriteTimeout: 30 * time.Second, PoolSize: 10, PoolTimeout: 30 * time.Second, - IdleTimeout: 500 * time.Millisecond, - IdleCheckFrequency: 500 * time.Millisecond, + IdleTimeout: time.Minute, + IdleCheckFrequency: 100 * time.Millisecond, } } @@ -125,8 +125,8 @@ func redisClusterOptions() *redis.ClusterOptions { WriteTimeout: 30 * time.Second, PoolSize: 10, PoolTimeout: 30 * time.Second, - IdleTimeout: 500 * time.Millisecond, - IdleCheckFrequency: 500 * time.Millisecond, + IdleTimeout: time.Minute, + IdleCheckFrequency: 100 * time.Millisecond, } } @@ -141,8 +141,8 @@ func redisRingOptions() *redis.RingOptions { WriteTimeout: 30 * time.Second, PoolSize: 10, PoolTimeout: 30 * time.Second, - IdleTimeout: 500 * time.Millisecond, - IdleCheckFrequency: 500 * time.Millisecond, + IdleTimeout: time.Minute, + IdleCheckFrequency: 100 * time.Millisecond, } } diff --git a/pool_test.go b/pool_test.go index 0e1580b..c8fa859 100644 --- a/pool_test.go +++ b/pool_test.go @@ -16,8 +16,8 @@ var _ = Describe("pool", func() { opt := redisOptions() opt.MinIdleConns = 0 opt.MaxConnAge = 0 + opt.IdleTimeout = time.Second client = redis.NewClient(opt) - Expect(client.FlushDB().Err()).NotTo(HaveOccurred()) }) AfterEach(func() { @@ -98,7 +98,7 @@ var _ = Describe("pool", func() { Expect(pool.IdleLen()).To(Equal(1)) stats := pool.Stats() - Expect(stats.Hits).To(Equal(uint32(2))) + Expect(stats.Hits).To(Equal(uint32(1))) Expect(stats.Misses).To(Equal(uint32(2))) Expect(stats.Timeouts).To(Equal(uint32(0))) }) @@ -115,12 +115,15 @@ var _ = Describe("pool", func() { Expect(pool.IdleLen()).To(Equal(1)) stats := pool.Stats() - Expect(stats.Hits).To(Equal(uint32(100))) + Expect(stats.Hits).To(Equal(uint32(99))) Expect(stats.Misses).To(Equal(uint32(1))) Expect(stats.Timeouts).To(Equal(uint32(0))) }) It("removes idle connections", func() { + err := client.Ping().Err() + Expect(err).NotTo(HaveOccurred()) + stats := client.PoolStats() Expect(stats).To(Equal(&redis.PoolStats{ Hits: 0, diff --git a/pubsub_test.go b/pubsub_test.go index 2d072fa..ea4b26e 100644 --- a/pubsub_test.go +++ b/pubsub_test.go @@ -78,7 +78,7 @@ var _ = Describe("PubSub", func() { } stats := client.PoolStats() - Expect(stats.Misses).To(Equal(uint32(2))) + Expect(stats.Misses).To(Equal(uint32(1))) }) It("should pub/sub channels", func() { @@ -201,7 +201,7 @@ var _ = Describe("PubSub", func() { } stats := client.PoolStats() - Expect(stats.Misses).To(Equal(uint32(2))) + Expect(stats.Misses).To(Equal(uint32(1))) }) It("should ping/pong", func() { diff --git a/redis_test.go b/redis_test.go index d600894..ceeb87e 100644 --- a/redis_test.go +++ b/redis_test.go @@ -191,6 +191,8 @@ var _ = Describe("Client", func() { client.Pool().Put(cn) Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue()) + time.Sleep(time.Second) + err = client.Ping().Err() Expect(err).NotTo(HaveOccurred())