forked from mirror/redis
internal/pool: replace atomic.Value with int64
This commit is contained in:
parent
7a91ed0df5
commit
6c72dc807e
|
@ -543,18 +543,6 @@ var _ = Describe("ClusterClient", func() {
|
||||||
Expect(stats).To(BeAssignableToTypeOf(&redis.PoolStats{}))
|
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() {
|
It("returns an error when there are no attempts left", func() {
|
||||||
opt := redisClusterOptions()
|
opt := redisClusterOptions()
|
||||||
opt.MaxRedirects = -1
|
opt.MaxRedirects = -1
|
||||||
|
|
|
@ -1502,8 +1502,8 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
||||||
|
|
||||||
stats := client.PoolStats()
|
stats := client.PoolStats()
|
||||||
Expect(stats.Hits).To(Equal(uint32(1)))
|
Expect(stats.Hits).To(Equal(uint32(2)))
|
||||||
Expect(stats.Misses).To(Equal(uint32(2)))
|
Expect(stats.Misses).To(Equal(uint32(1)))
|
||||||
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2215,8 +2215,8 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
||||||
|
|
||||||
stats := client.PoolStats()
|
stats := client.PoolStats()
|
||||||
Expect(stats.Hits).To(Equal(uint32(1)))
|
Expect(stats.Hits).To(Equal(uint32(2)))
|
||||||
Expect(stats.Misses).To(Equal(uint32(2)))
|
Expect(stats.Misses).To(Equal(uint32(1)))
|
||||||
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2297,8 +2297,8 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
Expect(client.Ping().Err()).NotTo(HaveOccurred())
|
||||||
|
|
||||||
stats := client.PoolStats()
|
stats := client.PoolStats()
|
||||||
Expect(stats.Hits).To(Equal(uint32(1)))
|
Expect(stats.Hits).To(Equal(uint32(2)))
|
||||||
Expect(stats.Misses).To(Equal(uint32(2)))
|
Expect(stats.Misses).To(Equal(uint32(1)))
|
||||||
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -13,14 +13,13 @@ var noDeadline = time.Time{}
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
netConn net.Conn
|
netConn net.Conn
|
||||||
|
|
||||||
rd *proto.Reader
|
rd *proto.Reader
|
||||||
rdLocked bool
|
wr *proto.Writer
|
||||||
wr *proto.Writer
|
|
||||||
|
|
||||||
Inited bool
|
Inited bool
|
||||||
pooled bool
|
pooled bool
|
||||||
createdAt time.Time
|
createdAt time.Time
|
||||||
usedAt atomic.Value
|
usedAt int64 // atomic
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn(netConn net.Conn) *Conn {
|
func NewConn(netConn net.Conn) *Conn {
|
||||||
|
@ -35,11 +34,12 @@ func NewConn(netConn net.Conn) *Conn {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) UsedAt() time.Time {
|
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) {
|
func (cn *Conn) SetUsedAt(tm time.Time) {
|
||||||
cn.usedAt.Store(tm)
|
atomic.StoreInt64(&cn.usedAt, tm.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) SetNetConn(netConn net.Conn) {
|
func (cn *Conn) SetNetConn(netConn net.Conn) {
|
||||||
|
|
12
main_test.go
12
main_test.go
|
@ -113,8 +113,8 @@ func redisOptions() *redis.Options {
|
||||||
WriteTimeout: 30 * time.Second,
|
WriteTimeout: 30 * time.Second,
|
||||||
PoolSize: 10,
|
PoolSize: 10,
|
||||||
PoolTimeout: 30 * time.Second,
|
PoolTimeout: 30 * time.Second,
|
||||||
IdleTimeout: 500 * time.Millisecond,
|
IdleTimeout: time.Minute,
|
||||||
IdleCheckFrequency: 500 * time.Millisecond,
|
IdleCheckFrequency: 100 * time.Millisecond,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,8 +125,8 @@ func redisClusterOptions() *redis.ClusterOptions {
|
||||||
WriteTimeout: 30 * time.Second,
|
WriteTimeout: 30 * time.Second,
|
||||||
PoolSize: 10,
|
PoolSize: 10,
|
||||||
PoolTimeout: 30 * time.Second,
|
PoolTimeout: 30 * time.Second,
|
||||||
IdleTimeout: 500 * time.Millisecond,
|
IdleTimeout: time.Minute,
|
||||||
IdleCheckFrequency: 500 * time.Millisecond,
|
IdleCheckFrequency: 100 * time.Millisecond,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,8 +141,8 @@ func redisRingOptions() *redis.RingOptions {
|
||||||
WriteTimeout: 30 * time.Second,
|
WriteTimeout: 30 * time.Second,
|
||||||
PoolSize: 10,
|
PoolSize: 10,
|
||||||
PoolTimeout: 30 * time.Second,
|
PoolTimeout: 30 * time.Second,
|
||||||
IdleTimeout: 500 * time.Millisecond,
|
IdleTimeout: time.Minute,
|
||||||
IdleCheckFrequency: 500 * time.Millisecond,
|
IdleCheckFrequency: 100 * time.Millisecond,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ var _ = Describe("pool", func() {
|
||||||
opt := redisOptions()
|
opt := redisOptions()
|
||||||
opt.MinIdleConns = 0
|
opt.MinIdleConns = 0
|
||||||
opt.MaxConnAge = 0
|
opt.MaxConnAge = 0
|
||||||
|
opt.IdleTimeout = time.Second
|
||||||
client = redis.NewClient(opt)
|
client = redis.NewClient(opt)
|
||||||
Expect(client.FlushDB().Err()).NotTo(HaveOccurred())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
|
@ -98,7 +98,7 @@ var _ = Describe("pool", func() {
|
||||||
Expect(pool.IdleLen()).To(Equal(1))
|
Expect(pool.IdleLen()).To(Equal(1))
|
||||||
|
|
||||||
stats := pool.Stats()
|
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.Misses).To(Equal(uint32(2)))
|
||||||
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
||||||
})
|
})
|
||||||
|
@ -115,12 +115,15 @@ var _ = Describe("pool", func() {
|
||||||
Expect(pool.IdleLen()).To(Equal(1))
|
Expect(pool.IdleLen()).To(Equal(1))
|
||||||
|
|
||||||
stats := pool.Stats()
|
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.Misses).To(Equal(uint32(1)))
|
||||||
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
Expect(stats.Timeouts).To(Equal(uint32(0)))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("removes idle connections", func() {
|
It("removes idle connections", func() {
|
||||||
|
err := client.Ping().Err()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
stats := client.PoolStats()
|
stats := client.PoolStats()
|
||||||
Expect(stats).To(Equal(&redis.PoolStats{
|
Expect(stats).To(Equal(&redis.PoolStats{
|
||||||
Hits: 0,
|
Hits: 0,
|
||||||
|
|
|
@ -78,7 +78,7 @@ var _ = Describe("PubSub", func() {
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := client.PoolStats()
|
stats := client.PoolStats()
|
||||||
Expect(stats.Misses).To(Equal(uint32(2)))
|
Expect(stats.Misses).To(Equal(uint32(1)))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("should pub/sub channels", func() {
|
It("should pub/sub channels", func() {
|
||||||
|
@ -201,7 +201,7 @@ var _ = Describe("PubSub", func() {
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := client.PoolStats()
|
stats := client.PoolStats()
|
||||||
Expect(stats.Misses).To(Equal(uint32(2)))
|
Expect(stats.Misses).To(Equal(uint32(1)))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("should ping/pong", func() {
|
It("should ping/pong", func() {
|
||||||
|
|
|
@ -191,6 +191,8 @@ var _ = Describe("Client", func() {
|
||||||
client.Pool().Put(cn)
|
client.Pool().Put(cn)
|
||||||
Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue())
|
Expect(cn.UsedAt().Equal(createdAt)).To(BeTrue())
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
err = client.Ping().Err()
|
err = client.Ping().Err()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue