2013-12-09 20:09:57 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-05-27 21:33:09 +00:00
|
|
|
"container/list"
|
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"
|
2014-02-06 00:19:05 +00:00
|
|
|
"sync/atomic"
|
2013-12-09 20:09:57 +00:00
|
|
|
"time"
|
2014-10-02 17:26:25 +00:00
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
"github.com/hashicorp/consul/consul/agent"
|
2014-11-18 16:03:36 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2015-10-13 23:43:52 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
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-05-27 21:33:09 +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
|
2015-10-13 23:43:52 +00:00
|
|
|
codec rpc.ClientCodec
|
2014-05-27 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 21:17:20 +00:00
|
|
|
func (sc *StreamClient) Close() {
|
|
|
|
sc.stream.Close()
|
2015-10-13 23:43:52 +00:00
|
|
|
sc.codec.Close()
|
2015-04-07 21:17:20 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 20:09:57 +00:00
|
|
|
// Conn is a pooled connection to a Consul server
|
|
|
|
type Conn struct {
|
2014-05-28 23:55:39 +00:00
|
|
|
refCount int32
|
|
|
|
shouldClose int32
|
|
|
|
|
2013-12-19 23:42:17 +00:00
|
|
|
addr net.Addr
|
2014-05-27 21:33:09 +00:00
|
|
|
session muxSession
|
2013-12-19 23:42:17 +00:00
|
|
|
lastUsed time.Time
|
2014-05-27 21:33:09 +00:00
|
|
|
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 {
|
2014-02-06 00:19:05 +00:00
|
|
|
return c.session.Close()
|
2013-12-09 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 21:33:09 +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
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := msgpackrpc.NewClientCodec(stream)
|
2014-05-27 21:33:09 +00:00
|
|
|
|
|
|
|
// Return a new stream client
|
|
|
|
sc := &StreamClient{
|
|
|
|
stream: stream,
|
2015-10-13 23:43:52 +00:00
|
|
|
codec: codec,
|
2014-05-27 21:33:09 +00:00
|
|
|
}
|
|
|
|
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()
|
2014-05-28 23:55:39 +00:00
|
|
|
if c.clients.Len() < c.pool.maxStreams && atomic.LoadInt32(&c.shouldClose) == 0 {
|
2014-05-27 21:33:09 +00:00
|
|
|
c.clients.PushFront(client)
|
|
|
|
didSave = true
|
2015-11-28 01:20:57 +00:00
|
|
|
|
|
|
|
// If this is a Yamux stream, shrink the internal buffers so that
|
|
|
|
// we can GC the idle memory
|
|
|
|
if ys, ok := client.stream.(*yamux.Stream); ok {
|
|
|
|
ys.Shrink()
|
|
|
|
}
|
2014-05-27 21:33:09 +00:00
|
|
|
}
|
|
|
|
c.clientLock.Unlock()
|
|
|
|
if !didSave {
|
2015-04-07 21:17:20 +00:00
|
|
|
client.Close()
|
2014-05-27 21:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 17:01:05 +00:00
|
|
|
// markForUse does all the bookkeeping required to ready a connection for use.
|
|
|
|
func (c *Conn) markForUse() {
|
|
|
|
c.lastUsed = time.Now()
|
|
|
|
atomic.AddInt32(&c.refCount, 1)
|
|
|
|
}
|
|
|
|
|
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
|
2013-12-09 22:29:20 +00:00
|
|
|
// RPC requests between servers. It is only used to pool
|
|
|
|
// connections in the rpcConsul mode. Raft connections
|
2014-12-04 23:25:06 +00:00
|
|
|
// 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
|
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// The maximum number of open streams to keep
|
|
|
|
maxStreams int
|
|
|
|
|
2014-02-06 00:19:05 +00:00
|
|
|
// Pool maps an address to a open connection
|
|
|
|
pool map[string]*Conn
|
2013-12-09 20:09:57 +00:00
|
|
|
|
2015-08-13 01:48:15 +00:00
|
|
|
// limiter is used to throttle the number of connect attempts
|
2015-08-13 03:14:48 +00:00
|
|
|
// to a given address. The first thread will attempt a connection
|
|
|
|
// and put a channel in here, which all other threads will wait
|
|
|
|
// on to close.
|
|
|
|
limiter map[string]chan struct{}
|
2015-08-13 01:48:15 +00:00
|
|
|
|
2015-05-08 22:57:37 +00:00
|
|
|
// TLS wrapper
|
2015-05-11 22:15:36 +00:00
|
|
|
tlsWrap tlsutil.DCWrapper
|
2014-04-04 23:27:56 +00:00
|
|
|
|
2013-12-09 20:09:57 +00:00
|
|
|
// Used to indicate the pool is shutdown
|
2014-02-05 22:20:18 +00:00
|
|
|
shutdown bool
|
|
|
|
shutdownCh chan struct{}
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPool is used to make a new connection pool
|
2014-02-06 00:19:05 +00:00
|
|
|
// Maintain at most one connection per host, for up to maxTime.
|
2014-05-27 21:33:09 +00:00
|
|
|
// 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.
|
2015-05-11 22:15:36 +00:00
|
|
|
func NewPool(logOutput io.Writer, maxTime time.Duration, maxStreams int, tlsWrap tlsutil.DCWrapper) *ConnPool {
|
2013-12-09 20:09:57 +00:00
|
|
|
pool := &ConnPool{
|
2014-05-28 23:32:10 +00:00
|
|
|
logOutput: logOutput,
|
2014-02-05 22:20:18 +00:00
|
|
|
maxTime: maxTime,
|
2014-05-27 21:33:09 +00:00
|
|
|
maxStreams: maxStreams,
|
2014-02-06 00:19:05 +00:00
|
|
|
pool: make(map[string]*Conn),
|
2015-08-13 03:14:48 +00:00
|
|
|
limiter: make(map[string]chan struct{}),
|
2015-05-08 22:57:37 +00:00
|
|
|
tlsWrap: tlsWrap,
|
2014-02-05 22:20:18 +00:00
|
|
|
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()
|
|
|
|
|
2014-02-06 00:19:05 +00:00
|
|
|
for _, conn := range p.pool {
|
|
|
|
conn.Close()
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
2014-02-06 00:19:05 +00:00
|
|
|
p.pool = make(map[string]*Conn)
|
2013-12-09 20:09:57 +00:00
|
|
|
|
2014-02-05 22:20:18 +00:00
|
|
|
if p.shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p.shutdown = true
|
|
|
|
close(p.shutdownCh)
|
2013-12-09 20:09:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-13 01:48:15 +00:00
|
|
|
// acquire will return a pooled connection, if available. Otherwise it will
|
|
|
|
// wait for an existing connection attempt to finish, if one if in progress,
|
|
|
|
// and will return that one if it succeeds. If all else fails, it will return a
|
|
|
|
// newly-created connection and add it to the pool.
|
2015-05-09 01:42:19 +00:00
|
|
|
func (p *ConnPool) acquire(dc string, addr net.Addr, version int) (*Conn, error) {
|
2015-08-13 01:48:15 +00:00
|
|
|
// Check to see if there's a pooled connection available. This is up
|
|
|
|
// here since it should the the vastly more common case than the rest
|
|
|
|
// of the code here.
|
|
|
|
p.Lock()
|
|
|
|
c := p.pool[addr.String()]
|
|
|
|
if c != nil {
|
2015-08-13 17:01:05 +00:00
|
|
|
c.markForUse()
|
2015-08-13 01:48:15 +00:00
|
|
|
p.Unlock()
|
|
|
|
return c, nil
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 01:48:15 +00:00
|
|
|
// If not (while we are still locked), set up the throttling structure
|
2015-08-13 03:14:48 +00:00
|
|
|
// for this address, which will make everyone else wait until our
|
|
|
|
// attempt is done.
|
|
|
|
var wait chan struct{}
|
2015-08-13 01:48:15 +00:00
|
|
|
var ok bool
|
|
|
|
if wait, ok = p.limiter[addr.String()]; !ok {
|
2015-08-13 18:38:02 +00:00
|
|
|
wait = make(chan struct{})
|
2015-08-13 01:48:15 +00:00
|
|
|
p.limiter[addr.String()] = wait
|
|
|
|
}
|
2015-08-13 03:14:48 +00:00
|
|
|
isLeadThread := !ok
|
2015-08-13 01:48:15 +00:00
|
|
|
p.Unlock()
|
|
|
|
|
2015-08-13 03:14:48 +00:00
|
|
|
// If we are the lead thread, make the new connection and then wake
|
|
|
|
// everybody else up to see if we got it.
|
|
|
|
if isLeadThread {
|
|
|
|
c, err := p.getNewConn(dc, addr, version)
|
2015-08-13 17:01:05 +00:00
|
|
|
p.Lock()
|
|
|
|
delete(p.limiter, addr.String())
|
|
|
|
close(wait)
|
2015-08-13 03:14:48 +00:00
|
|
|
if err != nil {
|
2015-08-13 17:01:05 +00:00
|
|
|
p.Unlock()
|
2015-08-13 03:14:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.pool[addr.String()] = c
|
2015-08-13 01:48:15 +00:00
|
|
|
p.Unlock()
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-08-13 03:14:48 +00:00
|
|
|
// Otherwise, wait for the lead thread to attempt the connection
|
|
|
|
// and use what's in the pool at that point.
|
|
|
|
select {
|
|
|
|
case <-p.shutdownCh:
|
|
|
|
return nil, fmt.Errorf("rpc error: shutdown")
|
|
|
|
case <-wait:
|
2015-08-13 01:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 03:14:48 +00:00
|
|
|
// See if the lead thread was able to get us a connection.
|
2015-08-13 01:48:15 +00:00
|
|
|
p.Lock()
|
2015-08-13 03:14:48 +00:00
|
|
|
if c := p.pool[addr.String()]; c != nil {
|
2015-08-13 17:01:05 +00:00
|
|
|
c.markForUse()
|
2015-08-13 01:48:15 +00:00
|
|
|
p.Unlock()
|
2015-08-13 03:14:48 +00:00
|
|
|
return c, nil
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
2015-08-13 01:48:15 +00:00
|
|
|
|
2014-05-28 23:55:39 +00:00
|
|
|
p.Unlock()
|
2015-08-13 03:14:48 +00:00
|
|
|
return nil, fmt.Errorf("rpc error: lead thread didn't get connection")
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNewConn is used to return a new connection
|
2015-05-09 01:42:19 +00:00
|
|
|
func (p *ConnPool) getNewConn(dc string, addr net.Addr, version int) (*Conn, error) {
|
2013-12-09 20:09:57 +00:00
|
|
|
// Try to dial the conn
|
2014-04-04 23:27:56 +00:00
|
|
|
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
|
2014-04-04 23:27:56 +00:00
|
|
|
if tcp, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcp.SetKeepAlive(true)
|
|
|
|
tcp.SetNoDelay(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if TLS is enabled
|
2015-05-08 22:57:37 +00:00
|
|
|
if p.tlsWrap != nil {
|
2014-04-04 23:27:56 +00:00
|
|
|
// 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
|
|
|
|
2014-04-04 23:27:56 +00:00
|
|
|
// Wrap the connection in a TLS client
|
2015-05-11 22:15:36 +00:00
|
|
|
tlsConn, err := p.tlsWrap(dc, conn)
|
2014-06-22 19:49:51 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-02 17:26:25 +00:00
|
|
|
conn = tlsConn
|
2014-04-04 23:27:56 +00:00
|
|
|
}
|
2013-12-09 20:09:57 +00:00
|
|
|
|
2014-05-27 21:33:09 +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
|
|
|
|
}
|
2013-12-09 22:29:20 +00:00
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// 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()
|
2014-06-09 18:16:53 +00:00
|
|
|
conf.LogOutput = p.logOutput
|
2014-05-28 23:32:10 +00:00
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// Create a multiplexed session
|
2014-05-28 23:32:10 +00:00
|
|
|
session, _ = yamux.Client(conn, conf)
|
2014-05-27 21:33:09 +00:00
|
|
|
}
|
2013-12-09 22:52:22 +00:00
|
|
|
|
2013-12-09 20:09:57 +00:00
|
|
|
// Wrap the connection
|
2015-02-16 22:04:47 +00:00
|
|
|
c := &Conn{
|
|
|
|
refCount: 1,
|
|
|
|
addr: addr,
|
|
|
|
session: session,
|
|
|
|
clients: list.New(),
|
|
|
|
lastUsed: time.Now(),
|
|
|
|
version: version,
|
|
|
|
pool: p,
|
|
|
|
}
|
2015-08-13 01:48:15 +00:00
|
|
|
return c, nil
|
2014-02-06 00:19:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 12:22:08 +00:00
|
|
|
// clearConn is used to clear any cached connection, potentially in response to an error
|
2014-05-28 23:55:39 +00:00
|
|
|
func (p *ConnPool) clearConn(conn *Conn) {
|
|
|
|
// Ensure returned streams are closed
|
|
|
|
atomic.StoreInt32(&conn.shouldClose, 1)
|
|
|
|
|
|
|
|
// Clear from the cache
|
2014-02-06 00:19:05 +00:00
|
|
|
p.Lock()
|
2014-05-28 23:55:39 +00:00
|
|
|
if c, ok := p.pool[conn.addr.String()]; ok && c == conn {
|
|
|
|
delete(p.pool, conn.addr.String())
|
|
|
|
}
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
// Close down immediately if idle
|
2014-06-13 16:25:01 +00:00
|
|
|
if refCount := atomic.LoadInt32(&conn.refCount); refCount == 0 {
|
2014-05-28 21:17:07 +00:00
|
|
|
conn.Close()
|
|
|
|
}
|
2014-02-06 00:19:05 +00:00
|
|
|
}
|
2013-12-09 20:09:57 +00:00
|
|
|
|
2014-02-06 00:19:05 +00:00
|
|
|
// releaseConn is invoked when we are done with a conn to reduce the ref count
|
|
|
|
func (p *ConnPool) releaseConn(conn *Conn) {
|
2014-05-28 23:55:39 +00:00
|
|
|
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
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// getClient is used to get a usable client for an address and protocol version
|
2015-05-09 01:42:19 +00:00
|
|
|
func (p *ConnPool) getClient(dc string, addr net.Addr, version int) (*Conn, *StreamClient, error) {
|
2014-02-06 00:19:05 +00:00
|
|
|
retries := 0
|
|
|
|
START:
|
2013-12-09 22:58:49 +00:00
|
|
|
// Try to get a conn first
|
2015-05-09 01:42:19 +00:00
|
|
|
conn, err := p.acquire(dc, addr, version)
|
2013-12-09 22:58:49 +00:00
|
|
|
if err != nil {
|
2014-05-27 21:33:09 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to get conn: %v", err)
|
2013-12-09 22:58:49 +00:00
|
|
|
}
|
2014-02-06 00:19:05 +00:00
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// Get a client
|
|
|
|
client, err := conn.getClient()
|
2014-02-06 00:19:05 +00:00
|
|
|
if err != nil {
|
2014-05-28 23:55:39 +00:00
|
|
|
p.clearConn(conn)
|
|
|
|
p.releaseConn(conn)
|
2014-02-06 00:19:05 +00:00
|
|
|
|
|
|
|
// Try to redial, possible that the TCP session closed due to timeout
|
|
|
|
if retries == 0 {
|
|
|
|
retries++
|
|
|
|
goto START
|
|
|
|
}
|
2014-05-27 21:33:09 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to start stream: %v", err)
|
2014-02-06 00:19:05 +00:00
|
|
|
}
|
2014-05-27 21:33:09 +00:00
|
|
|
return conn, client, nil
|
|
|
|
}
|
2014-02-06 00:19:05 +00:00
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// RPC is used to make an RPC call to a remote host
|
2015-05-09 01:42:19 +00:00
|
|
|
func (p *ConnPool) RPC(dc string, addr net.Addr, version int, method string, args interface{}, reply interface{}) error {
|
2014-05-28 23:55:39 +00:00
|
|
|
// Get a usable client
|
2015-05-09 01:42:19 +00:00
|
|
|
conn, sc, err := p.getClient(dc, addr, version)
|
2014-05-28 23:55:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %v", err)
|
|
|
|
}
|
2013-12-09 22:58:49 +00:00
|
|
|
|
|
|
|
// Make the RPC call
|
2015-10-13 23:43:52 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(sc.codec, method, args, reply)
|
2014-05-28 23:55:39 +00:00
|
|
|
if err != nil {
|
2015-04-07 21:17:20 +00:00
|
|
|
sc.Close()
|
2014-05-28 23:55:39 +00:00
|
|
|
p.releaseConn(conn)
|
|
|
|
return fmt.Errorf("rpc error: %v", err)
|
2013-12-09 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 23:55:39 +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
|
|
|
|
2016-03-27 02:28:13 +00:00
|
|
|
// PingConsulServer sends a Status.Ping message to the specified server and
|
|
|
|
// returns true if healthy, false if an error occurred
|
2016-03-30 00:39:19 +00:00
|
|
|
func (p *ConnPool) PingConsulServer(s *agent.Server) (bool, error) {
|
2016-03-27 02:28:13 +00:00
|
|
|
// Get a usable client
|
|
|
|
conn, sc, err := p.getClient(s.Datacenter, s.Addr, s.Version)
|
|
|
|
if err != nil {
|
2016-03-28 20:38:58 +00:00
|
|
|
return false, err
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC call
|
|
|
|
var out struct{}
|
|
|
|
err = msgpackrpc.CallWithCodec(sc.codec, "Status.Ping", struct{}{}, &out)
|
|
|
|
if err != nil {
|
|
|
|
sc.Close()
|
|
|
|
p.releaseConn(conn)
|
2016-03-28 20:38:58 +00:00
|
|
|
return false, err
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done with the connection
|
|
|
|
conn.returnClient(sc)
|
|
|
|
p.releaseConn(conn)
|
2016-03-28 20:38:58 +00:00
|
|
|
return true, nil
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 23:42:17 +00:00
|
|
|
// Reap is used to close conns open over maxTime
|
|
|
|
func (p *ConnPool) reap() {
|
2014-11-26 09:25:37 +00:00
|
|
|
for {
|
2013-12-19 23:42:17 +00:00
|
|
|
// Sleep for a while
|
2014-02-05 22:20:18 +00:00
|
|
|
select {
|
|
|
|
case <-p.shutdownCh:
|
|
|
|
return
|
2014-11-26 09:25:37 +00:00
|
|
|
case <-time.After(time.Second):
|
2014-02-05 22:20:18 +00:00
|
|
|
}
|
2013-12-19 23:42:17 +00:00
|
|
|
|
|
|
|
// Reap all old conns
|
|
|
|
p.Lock()
|
2014-02-06 00:19:05 +00:00
|
|
|
var removed []string
|
2013-12-19 23:42:17 +00:00
|
|
|
now := time.Now()
|
2014-02-06 00:19:05 +00:00
|
|
|
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
|
|
|
}
|
2014-02-06 00:19:05 +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()
|
|
|
|
}
|
|
|
|
}
|