2015-05-27 16:02:38 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
2015-05-27 21:25:08 +00:00
|
|
|
"gopkg.in/fatih/pool.v2"
|
2015-05-27 16:02:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type clientPool struct {
|
|
|
|
mu sync.RWMutex
|
2015-05-27 19:18:03 +00:00
|
|
|
pool map[uint64]pool.Pool
|
2015-05-27 16:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newClientPool() *clientPool {
|
|
|
|
return &clientPool{
|
2015-05-27 19:18:03 +00:00
|
|
|
pool: make(map[uint64]pool.Pool),
|
2015-05-27 16:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:18:03 +00:00
|
|
|
func (c *clientPool) setPool(nodeID uint64, p pool.Pool) {
|
2015-05-27 16:02:38 +00:00
|
|
|
c.mu.Lock()
|
2015-05-27 19:18:03 +00:00
|
|
|
c.pool[nodeID] = p
|
2015-05-27 16:02:38 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:18:03 +00:00
|
|
|
func (c *clientPool) getPool(nodeID uint64) (pool.Pool, bool) {
|
2015-06-08 16:12:32 +00:00
|
|
|
c.mu.RLock()
|
2015-05-27 19:18:03 +00:00
|
|
|
p, ok := c.pool[nodeID]
|
2015-06-08 16:12:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:18:03 +00:00
|
|
|
func (c *clientPool) conn(nodeID uint64) (net.Conn, error) {
|
2015-06-08 16:12:32 +00:00
|
|
|
c.mu.RLock()
|
2015-05-27 19:18:03 +00:00
|
|
|
conn, err := c.pool[nodeID].Get()
|
2015-06-08 16:12:32 +00:00
|
|
|
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()
|
|
|
|
}
|