Skip to content

Commit

Permalink
Tweak pool stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jan 25, 2016
1 parent d2b13f5 commit 6c7b789
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +34,7 @@ type pool interface {
Len() int
FreeLen() int
Close() error
Stats() PoolStats
Stats() *PoolStats
}

type connList struct {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 6c7b789

Please sign in to comment.