Merge pull request #248 from go-redis/fix/pool-stats-tweaks

Tweak pool stats.
This commit is contained in:
Vladimir Mihailenco 2016-01-25 16:10:42 +02:00
commit 538069c33f
3 changed files with 18 additions and 15 deletions

View File

@ -56,17 +56,17 @@ func (c *ClusterClient) Watch(keys ...string) (*Multi, error) {
return client.Watch(keys...) return client.Watch(keys...)
} }
// PoolStats returns accumulated connection pool stats // PoolStats returns accumulated connection pool stats.
func (c *ClusterClient) PoolStats() *PoolStats { func (c *ClusterClient) PoolStats() *PoolStats {
acc := PoolStats{} acc := PoolStats{}
c.clientsMx.RLock() c.clientsMx.RLock()
for _, client := range c.clients { for _, client := range c.clients {
m := client.PoolStats() m := client.PoolStats()
acc.TotalConns += m.TotalConns
acc.FreeConns += m.FreeConns
acc.Requests += m.Requests acc.Requests += m.Requests
acc.Waits += m.Waits acc.Waits += m.Waits
acc.Timeouts += m.Timeouts acc.Timeouts += m.Timeouts
acc.TotalConns += m.TotalConns
acc.FreeConns += m.FreeConns
} }
c.clientsMx.RUnlock() c.clientsMx.RUnlock()
return &acc return &acc
@ -322,7 +322,7 @@ type ClusterOptions struct {
ReadTimeout time.Duration ReadTimeout time.Duration
WriteTimeout time.Duration WriteTimeout time.Duration
// PoolSize applies per redis node and not for the whole cluster. // PoolSize applies per cluster node and not for the whole cluster.
PoolSize int PoolSize int
PoolTimeout time.Duration PoolTimeout time.Duration
IdleTimeout time.Duration IdleTimeout time.Duration

22
pool.go
View File

@ -16,10 +16,10 @@ var (
errPoolTimeout = errors.New("redis: connection pool timeout") errPoolTimeout = errors.New("redis: connection pool timeout")
) )
// PoolStats contains pool state information and accumulated stats // PoolStats contains pool state information and accumulated stats.
type PoolStats struct { type PoolStats struct {
Requests uint64 // number of times a connection was requested by the pool Requests uint64 // number of times a connection was requested by the pool
Waits uint64 // number of times our pool had to wait for a connection to avail Waits uint64 // number of times our pool had to wait for a connection
Timeouts uint64 // number of times a wait timeout occurred Timeouts uint64 // number of times a wait timeout occurred
TotalConns uint64 // the number of total connections in the pool TotalConns uint64 // the number of total connections in the pool
@ -34,7 +34,7 @@ type pool interface {
Len() int Len() int
FreeLen() int FreeLen() int
Close() error Close() error
Stats() PoolStats Stats() *PoolStats
} }
type connList struct { type connList struct {
@ -314,10 +314,14 @@ func (p *connPool) FreeLen() int {
return len(p.freeConns) return len(p.freeConns)
} }
func (p *connPool) Stats() PoolStats { func (p *connPool) Stats() *PoolStats {
p.stats.TotalConns = uint64(p.Len()) stats := p.stats
p.stats.FreeConns = uint64(p.FreeLen()) stats.Requests = atomic.LoadUint64(&p.stats.Requests)
return p.stats stats.Waits = atomic.LoadUint64(&p.stats.Waits)
stats.Timeouts = atomic.LoadUint64(&p.stats.Timeouts)
stats.TotalConns = uint64(p.Len())
stats.FreeConns = uint64(p.FreeLen())
return &stats
} }
func (p *connPool) Close() (retErr error) { func (p *connPool) Close() (retErr error) {
@ -408,7 +412,7 @@ func (p *singleConnPool) FreeLen() int {
return 0 return 0
} }
func (p *singleConnPool) Stats() PoolStats { return PoolStats{} } func (p *singleConnPool) Stats() *PoolStats { return nil }
func (p *singleConnPool) Close() error { func (p *singleConnPool) Close() error {
return nil return nil
@ -517,7 +521,7 @@ func (p *stickyConnPool) FreeLen() int {
return 0 return 0
} }
func (p *stickyConnPool) Stats() PoolStats { return PoolStats{} } func (p *stickyConnPool) Stats() *PoolStats { return nil }
func (p *stickyConnPool) Reset(reason error) (err error) { func (p *stickyConnPool) Reset(reason error) (err error) {
p.mx.Lock() p.mx.Lock()

View File

@ -195,6 +195,5 @@ func NewClient(opt *Options) *Client {
// PoolStats returns connection pool stats // PoolStats returns connection pool stats
func (c *Client) PoolStats() *PoolStats { func (c *Client) PoolStats() *PoolStats {
stats := c.baseClient.connPool.Stats() return c.connPool.Stats()
return &stats
} }