1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-31 05:04:23 +03:00

feat: add connection waiting statistics (#2804)

Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
This commit is contained in:
Lev Zakharov
2025-05-07 15:54:26 +03:00
committed by GitHub
parent c0be87ec5b
commit 8ba559ca5d
2 changed files with 50 additions and 13 deletions

View File

@ -59,12 +59,14 @@ var _ = Describe("ConnPool", func() {
time.Sleep(time.Second)
Expect(connPool.Stats()).To(Equal(&pool.Stats{
Hits: 0,
Misses: 0,
Timeouts: 0,
TotalConns: 0,
IdleConns: 0,
StaleConns: 0,
Hits: 0,
Misses: 0,
Timeouts: 0,
WaitCount: 0,
WaitDurationNs: 0,
TotalConns: 0,
IdleConns: 0,
StaleConns: 0,
}))
})
@ -358,4 +360,31 @@ var _ = Describe("race", func() {
Expect(stats.IdleConns).To(Equal(uint32(0)))
Expect(stats.TotalConns).To(Equal(uint32(opt.PoolSize)))
})
It("wait", func() {
opt := &pool.Options{
Dialer: func(ctx context.Context) (net.Conn, error) {
return &net.TCPConn{}, nil
},
PoolSize: 1,
PoolTimeout: 3 * time.Second,
}
p := pool.NewConnPool(opt)
wait := make(chan struct{})
conn, _ := p.Get(ctx)
go func() {
_, _ = p.Get(ctx)
wait <- struct{}{}
}()
time.Sleep(time.Second)
p.Put(ctx, conn)
<-wait
stats := p.Stats()
Expect(stats.IdleConns).To(Equal(uint32(0)))
Expect(stats.TotalConns).To(Equal(uint32(1)))
Expect(stats.WaitCount).To(Equal(uint32(1)))
Expect(stats.WaitDurationNs).To(BeNumerically("~", time.Second.Nanoseconds(), 100*time.Millisecond.Nanoseconds()))
})
})