From 6c7b789b3a9c21d9e2c805a04e0f2651ebefb5cd Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Mon, 25 Jan 2016 15:57:09 +0200 Subject: [PATCH] Tweak pool stats. --- cluster.go | 8 ++++---- pool.go | 22 +++++++++++++--------- redis.go | 3 +-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/cluster.go b/cluster.go index d3139fd7..7291cb30 100644 --- a/cluster.go +++ b/cluster.go @@ -56,17 +56,17 @@ func (c *ClusterClient) Watch(keys ...string) (*Multi, error) { return client.Watch(keys...) } -// PoolStats returns accumulated connection pool stats +// PoolStats returns accumulated connection pool stats. func (c *ClusterClient) PoolStats() *PoolStats { acc := PoolStats{} c.clientsMx.RLock() for _, client := range c.clients { m := client.PoolStats() - acc.TotalConns += m.TotalConns - acc.FreeConns += m.FreeConns acc.Requests += m.Requests acc.Waits += m.Waits acc.Timeouts += m.Timeouts + acc.TotalConns += m.TotalConns + acc.FreeConns += m.FreeConns } c.clientsMx.RUnlock() return &acc @@ -322,7 +322,7 @@ type ClusterOptions struct { ReadTimeout 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 PoolTimeout time.Duration IdleTimeout time.Duration diff --git a/pool.go b/pool.go index d007f1a7..76ba10df 100644 --- a/pool.go +++ b/pool.go @@ -16,10 +16,10 @@ var ( 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 { 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 TotalConns uint64 // the number of total connections in the pool @@ -34,7 +34,7 @@ type pool interface { Len() int FreeLen() int Close() error - Stats() PoolStats + Stats() *PoolStats } type connList struct { @@ -314,10 +314,14 @@ func (p *connPool) FreeLen() int { return len(p.freeConns) } -func (p *connPool) Stats() PoolStats { - p.stats.TotalConns = uint64(p.Len()) - p.stats.FreeConns = uint64(p.FreeLen()) - return p.stats +func (p *connPool) Stats() *PoolStats { + stats := p.stats + stats.Requests = atomic.LoadUint64(&p.stats.Requests) + 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) { @@ -408,7 +412,7 @@ func (p *singleConnPool) FreeLen() int { return 0 } -func (p *singleConnPool) Stats() PoolStats { return PoolStats{} } +func (p *singleConnPool) Stats() *PoolStats { return nil } func (p *singleConnPool) Close() error { return nil @@ -517,7 +521,7 @@ func (p *stickyConnPool) FreeLen() int { return 0 } -func (p *stickyConnPool) Stats() PoolStats { return PoolStats{} } +func (p *stickyConnPool) Stats() *PoolStats { return nil } func (p *stickyConnPool) Reset(reason error) (err error) { p.mx.Lock() diff --git a/redis.go b/redis.go index abfbc6bb..db22af6e 100644 --- a/redis.go +++ b/redis.go @@ -195,6 +195,5 @@ func NewClient(opt *Options) *Client { // PoolStats returns connection pool stats func (c *Client) PoolStats() *PoolStats { - stats := c.baseClient.connPool.Stats() - return &stats + return c.connPool.Stats() }