open-consul/consul/pool.go

401 lines
8.7 KiB
Go
Raw Normal View History

2013-12-09 20:09:57 +00:00
package consul
import (
"container/list"
"crypto/tls"
2013-12-24 20:22:42 +00:00
"fmt"
2014-05-28 23:32:10 +00:00
"io"
2013-12-09 20:09:57 +00:00
"net"
2013-12-09 22:52:22 +00:00
"net/rpc"
2013-12-09 20:09:57 +00:00
"sync"
"sync/atomic"
2013-12-09 20:09:57 +00:00
"time"
2014-10-02 17:26:25 +00:00
"github.com/hashicorp/consul/tlsutil"
2014-10-20 17:21:31 +00:00
"github.com/hashicorp/go-msgpack/codec"
2014-10-02 17:26:25 +00:00
"github.com/hashicorp/yamux"
"github.com/inconshreveable/muxado"
2013-12-09 20:09:57 +00:00
)
2014-06-08 21:02:42 +00:00
// msgpackHandle is a shared handle for encoding/decoding of RPC messages
var msgpackHandle = &codec.MsgpackHandle{}
2014-06-07 07:59:27 +00:00
// muxSession is used to provide an interface for either muxado or yamux
type muxSession interface {
Open() (net.Conn, error)
Close() error
}
type muxadoWrapper struct {
m muxado.Session
}
func (w *muxadoWrapper) Open() (net.Conn, error) {
return w.m.Open()
}
func (w *muxadoWrapper) Close() error {
return w.m.Close()
}
// streamClient is used to wrap a stream with an RPC client
type StreamClient struct {
stream net.Conn
client *rpc.Client
}
func (sc *StreamClient) Close() {
sc.stream.Close()
sc.client.Close()
}
2013-12-09 20:09:57 +00:00
// Conn is a pooled connection to a Consul server
type Conn struct {
refCount int32
shouldClose int32
2013-12-19 23:42:17 +00:00
addr net.Addr
session muxSession
2013-12-19 23:42:17 +00:00
lastUsed time.Time
version int
pool *ConnPool
clients *list.List
clientLock sync.Mutex
2013-12-09 20:09:57 +00:00
}
2013-12-09 22:58:49 +00:00
func (c *Conn) Close() error {
return c.session.Close()
2013-12-09 22:58:49 +00:00
}
// getClient is used to get a cached or new client
func (c *Conn) getClient() (*StreamClient, error) {
// Check for cached client
c.clientLock.Lock()
front := c.clients.Front()
if front != nil {
c.clients.Remove(front)
}
c.clientLock.Unlock()
if front != nil {
return front.Value.(*StreamClient), nil
}
// Open a new session
stream, err := c.session.Open()
if err != nil {
return nil, err
}
// Create the RPC client
2014-06-08 21:02:42 +00:00
cc := codec.GoRpc.ClientCodec(stream, msgpackHandle)
client := rpc.NewClientWithCodec(cc)
// Return a new stream client
sc := &StreamClient{
stream: stream,
client: client,
}
return sc, nil
}
// returnStream is used when done with a stream
// to allow re-use by a future RPC
func (c *Conn) returnClient(client *StreamClient) {
didSave := false
c.clientLock.Lock()
if c.clients.Len() < c.pool.maxStreams && atomic.LoadInt32(&c.shouldClose) == 0 {
c.clients.PushFront(client)
didSave = true
}
c.clientLock.Unlock()
if !didSave {
client.Close()
}
}
2013-12-09 20:09:57 +00:00
// ConnPool is used to maintain a connection pool to other
// Consul servers. This is used to reduce the latency of
// RPC requests between servers. It is only used to pool
// connections in the rpcConsul mode. Raft connections
// are pooled separately.
2013-12-09 20:09:57 +00:00
type ConnPool struct {
sync.Mutex
2014-05-28 23:32:10 +00:00
// LogOutput is used to control logging
logOutput io.Writer
2013-12-19 23:42:17 +00:00
// The maximum time to keep a connection open
maxTime time.Duration
// The maximum number of open streams to keep
maxStreams int
// Pool maps an address to a open connection
pool map[string]*Conn
2013-12-09 20:09:57 +00:00
// TLS settings
tlsConfig *tls.Config
2013-12-09 20:09:57 +00:00
// Used to indicate the pool is shutdown
shutdown bool
shutdownCh chan struct{}
2013-12-09 20:09:57 +00:00
}
// NewPool is used to make a new connection pool
// Maintain at most one connection per host, for up to maxTime.
// Set maxTime to 0 to disable reaping. maxStreams is used to control
// the number of idle streams allowed.
// If TLS settings are provided outgoing connections use TLS.
2014-05-28 23:32:10 +00:00
func NewPool(logOutput io.Writer, maxTime time.Duration, maxStreams int, tlsConfig *tls.Config) *ConnPool {
2013-12-09 20:09:57 +00:00
pool := &ConnPool{
2014-05-28 23:32:10 +00:00
logOutput: logOutput,
maxTime: maxTime,
maxStreams: maxStreams,
pool: make(map[string]*Conn),
tlsConfig: tlsConfig,
shutdownCh: make(chan struct{}),
2013-12-09 20:09:57 +00:00
}
2013-12-19 23:42:17 +00:00
if maxTime > 0 {
go pool.reap()
}
2013-12-09 20:09:57 +00:00
return pool
}
// Shutdown is used to close the connection pool
func (p *ConnPool) Shutdown() error {
p.Lock()
defer p.Unlock()
for _, conn := range p.pool {
conn.Close()
2013-12-09 20:09:57 +00:00
}
p.pool = make(map[string]*Conn)
2013-12-09 20:09:57 +00:00
if p.shutdown {
return nil
}
p.shutdown = true
close(p.shutdownCh)
2013-12-09 20:09:57 +00:00
return nil
}
// Acquire is used to get a connection that is
// pooled or to return a new connection
func (p *ConnPool) acquire(addr net.Addr, version int) (*Conn, error) {
2013-12-09 20:09:57 +00:00
// Check for a pooled ocnn
if conn := p.getPooled(addr, version); conn != nil {
2013-12-09 20:09:57 +00:00
return conn, nil
}
// Create a new connection
return p.getNewConn(addr, version)
2013-12-09 20:09:57 +00:00
}
// getPooled is used to return a pooled connection
func (p *ConnPool) getPooled(addr net.Addr, version int) *Conn {
2013-12-09 20:09:57 +00:00
p.Lock()
c := p.pool[addr.String()]
if c != nil {
c.lastUsed = time.Now()
atomic.AddInt32(&c.refCount, 1)
2013-12-09 20:09:57 +00:00
}
p.Unlock()
return c
2013-12-09 20:09:57 +00:00
}
// getNewConn is used to return a new connection
func (p *ConnPool) getNewConn(addr net.Addr, version int) (*Conn, error) {
2013-12-09 20:09:57 +00:00
// Try to dial the conn
conn, err := net.DialTimeout("tcp", addr.String(), 10*time.Second)
2013-12-09 20:09:57 +00:00
if err != nil {
return nil, err
}
// Cast to TCPConn
if tcp, ok := conn.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
tcp.SetNoDelay(true)
}
// Check if TLS is enabled
if p.tlsConfig != nil {
// Switch the connection into TLS mode
if _, err := conn.Write([]byte{byte(rpcTLS)}); err != nil {
conn.Close()
return nil, err
}
2013-12-09 20:09:57 +00:00
// Wrap the connection in a TLS client
tlsConn, err := tlsutil.WrapTLSClient(conn, p.tlsConfig)
if err != nil {
conn.Close()
return nil, err
}
2014-10-02 17:26:25 +00:00
conn = tlsConn
}
2013-12-09 20:09:57 +00:00
// Switch the multiplexing based on version
var session muxSession
if version < 2 {
// Write the Consul multiplex byte to set the mode
if _, err := conn.Write([]byte{byte(rpcMultiplex)}); err != nil {
conn.Close()
return nil, err
}
// Create a multiplexed session
session = &muxadoWrapper{muxado.Client(conn)}
} else {
// Write the Consul multiplex byte to set the mode
if _, err := conn.Write([]byte{byte(rpcMultiplexV2)}); err != nil {
conn.Close()
return nil, err
}
2014-05-28 23:32:10 +00:00
// Setup the logger
conf := yamux.DefaultConfig()
conf.LogOutput = p.logOutput
2014-05-28 23:32:10 +00:00
// Create a multiplexed session
2014-05-28 23:32:10 +00:00
session, _ = yamux.Client(conn, conf)
}
2013-12-09 22:52:22 +00:00
2013-12-09 20:09:57 +00:00
// Wrap the connection
c := &Conn{
refCount: 1,
addr: addr,
session: session,
clients: list.New(),
lastUsed: time.Now(),
version: version,
pool: p,
}
2013-12-09 20:09:57 +00:00
// Track this connection, handle potential race condition
2013-12-09 20:09:57 +00:00
p.Lock()
if existing := p.pool[addr.String()]; existing != nil {
c.Close()
p.Unlock()
return existing, nil
} else {
p.pool[addr.String()] = c
p.Unlock()
return c, nil
2013-12-09 20:09:57 +00:00
}
}
// clearConn is used to clear any cached connection, potentially in response to an erro
func (p *ConnPool) clearConn(conn *Conn) {
// Ensure returned streams are closed
atomic.StoreInt32(&conn.shouldClose, 1)
// Clear from the cache
p.Lock()
if c, ok := p.pool[conn.addr.String()]; ok && c == conn {
delete(p.pool, conn.addr.String())
}
p.Unlock()
// Close down immediately if idle
if refCount := atomic.LoadInt32(&conn.refCount); refCount == 0 {
conn.Close()
}
}
2013-12-09 20:09:57 +00:00
// releaseConn is invoked when we are done with a conn to reduce the ref count
func (p *ConnPool) releaseConn(conn *Conn) {
refCount := atomic.AddInt32(&conn.refCount, -1)
if refCount == 0 && atomic.LoadInt32(&conn.shouldClose) == 1 {
conn.Close()
}
2013-12-09 20:09:57 +00:00
}
2013-12-09 22:58:49 +00:00
// getClient is used to get a usable client for an address and protocol version
func (p *ConnPool) getClient(addr net.Addr, version int) (*Conn, *StreamClient, error) {
retries := 0
START:
2013-12-09 22:58:49 +00:00
// Try to get a conn first
conn, err := p.acquire(addr, version)
2013-12-09 22:58:49 +00:00
if err != nil {
return nil, nil, fmt.Errorf("failed to get conn: %v", err)
2013-12-09 22:58:49 +00:00
}
// Get a client
client, err := conn.getClient()
if err != nil {
p.clearConn(conn)
p.releaseConn(conn)
// Try to redial, possible that the TCP session closed due to timeout
if retries == 0 {
retries++
goto START
}
return nil, nil, fmt.Errorf("failed to start stream: %v", err)
}
return conn, client, nil
}
// RPC is used to make an RPC call to a remote host
func (p *ConnPool) RPC(addr net.Addr, version int, method string, args interface{}, reply interface{}) error {
// Get a usable client
conn, sc, err := p.getClient(addr, version)
if err != nil {
return fmt.Errorf("rpc error: %v", err)
}
2013-12-09 22:58:49 +00:00
// Make the RPC call
err = sc.client.Call(method, args, reply)
if err != nil {
sc.Close()
p.releaseConn(conn)
return fmt.Errorf("rpc error: %v", err)
2013-12-09 22:58:49 +00:00
}
// Done with the connection
conn.returnClient(sc)
p.releaseConn(conn)
return nil
2013-12-09 22:58:49 +00:00
}
2013-12-19 23:42:17 +00:00
// Reap is used to close conns open over maxTime
func (p *ConnPool) reap() {
for {
2013-12-19 23:42:17 +00:00
// Sleep for a while
select {
case <-p.shutdownCh:
return
case <-time.After(time.Second):
}
2013-12-19 23:42:17 +00:00
// Reap all old conns
p.Lock()
var removed []string
2013-12-19 23:42:17 +00:00
now := time.Now()
for host, conn := range p.pool {
// Skip recently used connections
if now.Sub(conn.lastUsed) < p.maxTime {
continue
2013-12-19 23:42:17 +00:00
}
// Skip connections with active streams
if atomic.LoadInt32(&conn.refCount) > 0 {
continue
}
// Close the conn
conn.Close()
// Remove from pool
removed = append(removed, host)
}
for _, host := range removed {
delete(p.pool, host)
2013-12-19 23:42:17 +00:00
}
p.Unlock()
}
}