Skip to content

Commit 10a091a

Browse files
committed
internal/pool: replace atomic.Value with int64
1 parent 7a91ed0 commit 10a091a

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed

cluster_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -543,18 +543,6 @@ var _ = Describe("ClusterClient", func() {
543543
Expect(stats).To(BeAssignableToTypeOf(&redis.PoolStats{}))
544544
})
545545

546-
It("removes idle connections", func() {
547-
stats := client.PoolStats()
548-
Expect(stats.TotalConns).NotTo(BeZero())
549-
Expect(stats.IdleConns).NotTo(BeZero())
550-
551-
time.Sleep(2 * time.Second)
552-
553-
stats = client.PoolStats()
554-
Expect(stats.TotalConns).To(BeZero())
555-
Expect(stats.IdleConns).To(BeZero())
556-
})
557-
558546
It("returns an error when there are no attempts left", func() {
559547
opt := redisClusterOptions()
560548
opt.MaxRedirects = -1

commands_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,8 @@ var _ = Describe("Commands", func() {
15021502
Expect(client.Ping().Err()).NotTo(HaveOccurred())
15031503

15041504
stats := client.PoolStats()
1505-
Expect(stats.Hits).To(Equal(uint32(1)))
1506-
Expect(stats.Misses).To(Equal(uint32(2)))
1505+
Expect(stats.Hits).To(Equal(uint32(2)))
1506+
Expect(stats.Misses).To(Equal(uint32(1)))
15071507
Expect(stats.Timeouts).To(Equal(uint32(0)))
15081508
})
15091509

@@ -2215,8 +2215,8 @@ var _ = Describe("Commands", func() {
22152215
Expect(client.Ping().Err()).NotTo(HaveOccurred())
22162216

22172217
stats := client.PoolStats()
2218-
Expect(stats.Hits).To(Equal(uint32(1)))
2219-
Expect(stats.Misses).To(Equal(uint32(2)))
2218+
Expect(stats.Hits).To(Equal(uint32(2)))
2219+
Expect(stats.Misses).To(Equal(uint32(1)))
22202220
Expect(stats.Timeouts).To(Equal(uint32(0)))
22212221
})
22222222

@@ -2297,8 +2297,8 @@ var _ = Describe("Commands", func() {
22972297
Expect(client.Ping().Err()).NotTo(HaveOccurred())
22982298

22992299
stats := client.PoolStats()
2300-
Expect(stats.Hits).To(Equal(uint32(1)))
2301-
Expect(stats.Misses).To(Equal(uint32(2)))
2300+
Expect(stats.Hits).To(Equal(uint32(2)))
2301+
Expect(stats.Misses).To(Equal(uint32(1)))
23022302
Expect(stats.Timeouts).To(Equal(uint32(0)))
23032303
})
23042304

internal/pool/conn.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ var noDeadline = time.Time{}
1313
type Conn struct {
1414
netConn net.Conn
1515

16-
rd *proto.Reader
17-
rdLocked bool
18-
wr *proto.Writer
16+
rd *proto.Reader
17+
wr *proto.Writer
1918

2019
Inited bool
2120
pooled bool
2221
createdAt time.Time
23-
usedAt atomic.Value
22+
usedAt int64 // atomic
2423
}
2524

2625
func NewConn(netConn net.Conn) *Conn {
@@ -35,11 +34,12 @@ func NewConn(netConn net.Conn) *Conn {
3534
}
3635

3736
func (cn *Conn) UsedAt() time.Time {
38-
return cn.usedAt.Load().(time.Time)
37+
unix := atomic.LoadInt64(&cn.usedAt)
38+
return time.Unix(unix, 0)
3939
}
4040

4141
func (cn *Conn) SetUsedAt(tm time.Time) {
42-
cn.usedAt.Store(tm)
42+
atomic.StoreInt64(&cn.usedAt, tm.Unix())
4343
}
4444

4545
func (cn *Conn) SetNetConn(netConn net.Conn) {

main_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func redisOptions() *redis.Options {
113113
WriteTimeout: 30 * time.Second,
114114
PoolSize: 10,
115115
PoolTimeout: 30 * time.Second,
116-
IdleTimeout: 500 * time.Millisecond,
117-
IdleCheckFrequency: 500 * time.Millisecond,
116+
IdleTimeout: time.Minute,
117+
IdleCheckFrequency: 100 * time.Millisecond,
118118
}
119119
}
120120

@@ -125,8 +125,8 @@ func redisClusterOptions() *redis.ClusterOptions {
125125
WriteTimeout: 30 * time.Second,
126126
PoolSize: 10,
127127
PoolTimeout: 30 * time.Second,
128-
IdleTimeout: 500 * time.Millisecond,
129-
IdleCheckFrequency: 500 * time.Millisecond,
128+
IdleTimeout: time.Minute,
129+
IdleCheckFrequency: 100 * time.Millisecond,
130130
}
131131
}
132132

@@ -141,8 +141,8 @@ func redisRingOptions() *redis.RingOptions {
141141
WriteTimeout: 30 * time.Second,
142142
PoolSize: 10,
143143
PoolTimeout: 30 * time.Second,
144-
IdleTimeout: 500 * time.Millisecond,
145-
IdleCheckFrequency: 500 * time.Millisecond,
144+
IdleTimeout: time.Minute,
145+
IdleCheckFrequency: 100 * time.Millisecond,
146146
}
147147
}
148148

pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var _ = Describe("pool", func() {
1616
opt := redisOptions()
1717
opt.MinIdleConns = 0
1818
opt.MaxConnAge = 0
19+
opt.IdleTimeout = time.Second
1920
client = redis.NewClient(opt)
20-
Expect(client.FlushDB().Err()).NotTo(HaveOccurred())
2121
})
2222

2323
AfterEach(func() {

pubsub_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var _ = Describe("PubSub", func() {
7878
}
7979

8080
stats := client.PoolStats()
81-
Expect(stats.Misses).To(Equal(uint32(2)))
81+
Expect(stats.Misses).To(Equal(uint32(1)))
8282
})
8383

8484
It("should pub/sub channels", func() {
@@ -201,7 +201,7 @@ var _ = Describe("PubSub", func() {
201201
}
202202

203203
stats := client.PoolStats()
204-
Expect(stats.Misses).To(Equal(uint32(2)))
204+
Expect(stats.Misses).To(Equal(uint32(1)))
205205
})
206206

207207
It("should ping/pong", func() {

redis_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ var _ = Describe("Client", func() {
191191
client.Pool().Put(cn)
192192
Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue())
193193

194+
time.Sleep(time.Second)
195+
194196
err = client.Ping().Err()
195197
Expect(err).NotTo(HaveOccurred())
196198

0 commit comments

Comments
 (0)