Skip to content

Commit

Permalink
Added getting statistics of all connection pools
Browse files Browse the repository at this point in the history
  • Loading branch information
nvorobev committed Jul 12, 2022
1 parent bfd7635 commit 22a2c3c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.1.0 (2022-07-12)

- Added getting statistics of all connection pools

## v2.0.1 (2022-06-16)

- Fixed error return value for MGetWithGD
Expand Down
6 changes: 6 additions & 0 deletions ha_conn_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func NewHAConnFactory(cfg *HAConfig) (*HAConnFactory, error) {
return factory, nil
}

func (factory *HAConnFactory) stats() map[string]*redis.PoolStats {
return map[string]*redis.PoolStats{
factory.master.redisCli.Options().Addr: factory.master.redisCli.PoolStats(),
}
}

func (factory *HAConnFactory) close() {
factory.master.redisCli.Close()
factory.slaves.close()
Expand Down
9 changes: 3 additions & 6 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
type ConnFactory interface {
getSlaveConn(key ...string) (*redis.Client, error)
getMasterConn(key ...string) (*redis.Client, error)
stats() map[string]*redis.PoolStats
close()
}

Expand Down Expand Up @@ -160,12 +161,8 @@ func NewShard(cfg *ShardConfig) (*Pool, error) {
}, nil
}

func (p *Pool) Stats() (*redis.PoolStats, error) {
conn, err := p.connFactory.getMasterConn()
if err != nil {
return nil, err
}
return conn.PoolStats(), nil
func (p *Pool) Stats() map[string]*redis.PoolStats {
return p.connFactory.stats()
}

func (p *Pool) Close() {
Expand Down
13 changes: 13 additions & 0 deletions shard_conn_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ func NewShardConnFactory(cfg *ShardConfig) (*ShardConnFactory, error) {
return factory, nil
}

func (factory *ShardConnFactory) stats() map[string]*redis.PoolStats {
results := make(map[string]*redis.PoolStats, len(factory.shards))

for _, shard := range factory.shards {
result := shard.stats()
for addr, stats := range result {
results[addr] = stats
}
}

return results
}

func (factory *ShardConnFactory) close() {
for _, shard := range factory.shards {
shard.close()
Expand Down

0 comments on commit 22a2c3c

Please sign in to comment.