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...)
}
// 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

22
pool.go
View File

@ -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()

View File

@ -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()
}