influxdb/cluster/client_pool.go

58 lines
883 B
Go
Raw Normal View History

2015-05-27 16:02:38 +00:00
package cluster
import (
"net"
"sync"
"gopkg.in/fatih/pool.v2"
2015-05-27 16:02:38 +00:00
)
type clientPool struct {
mu sync.RWMutex
pool map[uint64]pool.Pool
2015-05-27 16:02:38 +00:00
}
func newClientPool() *clientPool {
return &clientPool{
pool: make(map[uint64]pool.Pool),
2015-05-27 16:02:38 +00:00
}
}
func (c *clientPool) setPool(nodeID uint64, p pool.Pool) {
2015-05-27 16:02:38 +00:00
c.mu.Lock()
c.pool[nodeID] = p
2015-05-27 16:02:38 +00:00
c.mu.Unlock()
}
func (c *clientPool) getPool(nodeID uint64) (pool.Pool, bool) {
c.mu.RLock()
p, ok := c.pool[nodeID]
c.mu.RUnlock()
2015-05-27 16:02:38 +00:00
return p, ok
}
func (c *clientPool) size() int {
c.mu.RLock()
var size int
for _, p := range c.pool {
size += p.Len()
}
c.mu.RUnlock()
return size
}
func (c *clientPool) conn(nodeID uint64) (net.Conn, error) {
c.mu.RLock()
conn, err := c.pool[nodeID].Get()
c.mu.RUnlock()
2015-05-27 16:02:38 +00:00
return conn, err
}
2015-05-27 16:06:04 +00:00
func (c *clientPool) close() {
2015-05-27 16:02:38 +00:00
c.mu.Lock()
for _, p := range c.pool {
p.Close()
}
c.mu.Unlock()
}