2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
package pool
|
2013-12-09 20:09:57 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-27 21:33:09 +00:00
|
|
|
"container/list"
|
2021-08-24 21:28:44 +00:00
|
|
|
"context"
|
2019-08-12 16:47:02 +00:00
|
|
|
"crypto/tls"
|
2013-12-24 20:22:42 +00:00
|
|
|
"fmt"
|
2020-07-29 18:43:33 +00:00
|
|
|
"log"
|
2013-12-09 20:09:57 +00:00
|
|
|
"net"
|
|
|
|
"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
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
"github.com/hashicorp/yamux"
|
|
|
|
|
2022-02-14 17:45:45 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/consul-net-rpc/net-rpc-msgpackrpc"
|
|
|
|
"github.com/hashicorp/consul-net-rpc/net/rpc"
|
2021-08-24 21:28:44 +00:00
|
|
|
|
2020-02-07 21:50:24 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-10-10 22:19:50 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbcommon"
|
2014-11-18 16:03:36 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2013-12-09 20:09:57 +00:00
|
|
|
)
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
const DefaultDialTimeout = 10 * time.Second
|
2017-03-15 15:02:14 +00:00
|
|
|
|
2016-08-10 01:10:04 +00:00
|
|
|
// muxSession is used to provide an interface for a stream multiplexer.
|
2014-05-27 21:33:09 +00:00
|
|
|
type muxSession interface {
|
|
|
|
Open() (net.Conn, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// streamClient is used to wrap a stream with an RPC client
|
|
|
|
type StreamClient struct {
|
2022-10-18 19:05:09 +00:00
|
|
|
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
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
nodeName string
|
2017-03-15 16:06:21 +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
|
|
|
|
|
|
|
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
|
2022-10-18 19:05:09 +00:00
|
|
|
codec := msgpackrpc.NewCodecFromHandle(true, true, stream, structs.MsgpackHandle)
|
2014-05-27 21:33:09 +00:00
|
|
|
|
|
|
|
// Return a new stream client
|
|
|
|
sc := &StreamClient{
|
2022-10-18 19:05:09 +00:00
|
|
|
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()
|
2017-06-15 13:16:16 +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
|
2022-10-18 19:05:09 +00:00
|
|
|
if ys, ok := client.stream.(*yamux.Stream); ok {
|
2015-11-28 01:20:57 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-06-15 13:16:16 +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. Maintain at most one
|
|
|
|
// connection per host, for up to MaxTime. When MaxTime connection
|
|
|
|
// reaping is disabled. MaxStreams is used to control the number of idle
|
|
|
|
// streams allowed. If TLS settings are provided outgoing connections
|
|
|
|
// use TLS.
|
2013-12-09 20:09:57 +00:00
|
|
|
type ConnPool struct {
|
2022-10-18 19:05:09 +00:00
|
|
|
// clientTimeoutMs is the default timeout for client RPC requests
|
|
|
|
// in milliseconds. Stored as an atomic uint32 value to allow for
|
|
|
|
// reloading.
|
|
|
|
// TODO: once we move to go1.19, change to atomic.Uint32.
|
|
|
|
clientTimeoutMs uint32
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
// SrcAddr is the source address for outgoing connections.
|
|
|
|
SrcAddr *net.TCPAddr
|
2017-05-03 10:57:11 +00:00
|
|
|
|
2020-07-29 18:43:33 +00:00
|
|
|
// Logger passed to yamux
|
|
|
|
// TODO: consider refactoring to accept a full yamux.Config instead of a logger
|
|
|
|
Logger *log.Logger
|
2014-05-28 23:32:10 +00:00
|
|
|
|
2022-11-24 15:13:02 +00:00
|
|
|
// RPCHoldTimeout is used as a buffer when calculating timeouts to
|
|
|
|
// allow for leader rotation.
|
|
|
|
RPCHoldTimeout time.Duration
|
2022-10-18 19:05:09 +00:00
|
|
|
// MaxQueryTime is used for calculating timeouts on blocking queries.
|
|
|
|
MaxQueryTime time.Duration
|
|
|
|
// DefaultQueryTime is used for calculating timeouts on blocking queries.
|
2022-04-21 20:21:35 +00:00
|
|
|
DefaultQueryTime time.Duration
|
|
|
|
|
2013-12-19 23:42:17 +00:00
|
|
|
// The maximum time to keep a connection open
|
2017-06-15 13:16:16 +00:00
|
|
|
MaxTime time.Duration
|
2013-12-19 23:42:17 +00:00
|
|
|
|
2014-05-27 21:33:09 +00:00
|
|
|
// The maximum number of open streams to keep
|
2017-06-15 13:16:16 +00:00
|
|
|
MaxStreams int
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// TLSConfigurator
|
|
|
|
TLSConfigurator *tlsutil.Configurator
|
2017-06-15 13:16:16 +00:00
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// GatewayResolver is a function that returns a suitable random mesh
|
|
|
|
// gateway address for dialing servers in a given DC. This is only
|
|
|
|
// needed if wan federation via mesh gateways is enabled.
|
|
|
|
GatewayResolver func(string) string
|
|
|
|
|
|
|
|
// Datacenter is the datacenter of the current agent.
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
// Server should be set to true if this connection pool is configured in a
|
|
|
|
// server instead of a client.
|
|
|
|
Server bool
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
sync.Mutex
|
2014-05-27 21:33:09 +00:00
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// pool maps a nodeName+address to a open connection
|
2014-02-06 00:19:05 +00:00
|
|
|
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
|
|
|
|
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{}
|
2017-06-15 13:16:16 +00:00
|
|
|
|
|
|
|
// once initializes the internal data structures and connection
|
|
|
|
// reaping on first use.
|
|
|
|
once sync.Once
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
// init configures the initial data structures. It should be called
|
|
|
|
// by p.once.Do(p.init) in all public methods.
|
|
|
|
func (p *ConnPool) init() {
|
|
|
|
p.pool = make(map[string]*Conn)
|
|
|
|
p.limiter = make(map[string]chan struct{})
|
|
|
|
p.shutdownCh = make(chan struct{})
|
|
|
|
if p.MaxTime > 0 {
|
|
|
|
go p.reap()
|
2013-12-19 23:42:17 +00:00
|
|
|
}
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown is used to close the connection pool
|
|
|
|
func (p *ConnPool) Shutdown() error {
|
2017-06-15 13:16:16 +00:00
|
|
|
p.once.Do(p.init)
|
|
|
|
|
2013-12-09 20:09:57 +00:00
|
|
|
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.
|
2020-05-28 08:18:30 +00:00
|
|
|
func (p *ConnPool) acquire(dc string, nodeName string, addr net.Addr) (*Conn, error) {
|
2020-03-09 20:59:02 +00:00
|
|
|
if nodeName == "" {
|
|
|
|
return nil, fmt.Errorf("pool: ConnPool.acquire requires a node name")
|
|
|
|
}
|
|
|
|
|
2017-03-15 16:06:21 +00:00
|
|
|
addrStr := addr.String()
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
poolKey := nodeName + ":" + addrStr
|
|
|
|
|
2015-08-13 01:48:15 +00:00
|
|
|
// Check to see if there's a pooled connection available. This is up
|
2019-06-27 20:22:07 +00:00
|
|
|
// here since it should the vastly more common case than the rest
|
2015-08-13 01:48:15 +00:00
|
|
|
// of the code here.
|
|
|
|
p.Lock()
|
2020-03-09 20:59:02 +00:00
|
|
|
c := p.pool[poolKey]
|
2015-08-13 01:48:15 +00:00
|
|
|
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
|
2017-03-15 16:06:21 +00:00
|
|
|
if wait, ok = p.limiter[addrStr]; !ok {
|
2015-08-13 18:38:02 +00:00
|
|
|
wait = make(chan struct{})
|
2017-03-15 16:06:21 +00:00
|
|
|
p.limiter[addrStr] = wait
|
2015-08-13 01:48:15 +00:00
|
|
|
}
|
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 {
|
2020-05-28 08:18:30 +00:00
|
|
|
c, err := p.getNewConn(dc, nodeName, addr)
|
2015-08-13 17:01:05 +00:00
|
|
|
p.Lock()
|
2017-03-15 16:06:21 +00:00
|
|
|
delete(p.limiter, addrStr)
|
2015-08-13 17:01:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
p.pool[poolKey] = 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()
|
2020-03-09 20:59:02 +00:00
|
|
|
if c := p.pool[poolKey]; 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
|
|
|
}
|
|
|
|
|
2019-08-12 16:47:02 +00:00
|
|
|
// HalfCloser is an interface that exposes a TCP half-close without exposing
|
|
|
|
// the underlying TLS or raw TCP connection.
|
2016-10-26 02:20:24 +00:00
|
|
|
type HalfCloser interface {
|
|
|
|
CloseWrite() error
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// DialTimeout is used to establish a raw connection to the given server, with
|
|
|
|
// given connection timeout. It also writes RPCTLS as the first byte.
|
2020-03-09 20:59:02 +00:00
|
|
|
func (p *ConnPool) DialTimeout(
|
|
|
|
dc string,
|
|
|
|
nodeName string,
|
|
|
|
addr net.Addr,
|
|
|
|
actualRPCType RPCType,
|
|
|
|
) (net.Conn, HalfCloser, error) {
|
2017-06-15 13:16:16 +00:00
|
|
|
p.once.Do(p.init)
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
if p.Server &&
|
|
|
|
p.GatewayResolver != nil &&
|
|
|
|
p.TLSConfigurator != nil &&
|
|
|
|
dc != p.Datacenter {
|
2020-03-09 20:59:02 +00:00
|
|
|
// NOTE: TLS is required on this branch.
|
2021-08-24 21:28:44 +00:00
|
|
|
nextProto := actualRPCType.ALPNString()
|
|
|
|
if nextProto == "" {
|
|
|
|
return nil, nil, fmt.Errorf("rpc type %d cannot be routed through a mesh gateway", actualRPCType)
|
|
|
|
}
|
|
|
|
return DialRPCViaMeshGateway(
|
|
|
|
context.Background(),
|
2020-03-09 20:59:02 +00:00
|
|
|
dc,
|
|
|
|
nodeName,
|
|
|
|
p.SrcAddr,
|
|
|
|
p.TLSConfigurator.OutgoingALPNRPCWrapper(),
|
2021-08-24 21:28:44 +00:00
|
|
|
nextProto,
|
2020-03-09 20:59:02 +00:00
|
|
|
p.Server,
|
|
|
|
p.GatewayResolver,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-23 17:18:22 +00:00
|
|
|
return p.dial(dc, addr, actualRPCType, RPCTLS)
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:18:30 +00:00
|
|
|
func (p *ConnPool) dial(
|
2020-03-09 20:59:02 +00:00
|
|
|
dc string,
|
|
|
|
addr net.Addr,
|
|
|
|
actualRPCType RPCType,
|
|
|
|
tlsRPCType RPCType,
|
|
|
|
) (net.Conn, HalfCloser, error) {
|
2013-12-09 20:09:57 +00:00
|
|
|
// Try to dial the conn
|
2022-10-18 19:05:09 +00:00
|
|
|
d := &net.Dialer{LocalAddr: p.SrcAddr, Timeout: DefaultDialTimeout}
|
2017-05-03 10:57:11 +00:00
|
|
|
conn, err := d.Dial("tcp", addr.String())
|
2013-12-09 20:09:57 +00:00
|
|
|
if err != nil {
|
2016-10-26 02:20:24 +00:00
|
|
|
return nil, nil, err
|
2013-12-09 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
var hc HalfCloser
|
2019-08-12 16:47:02 +00:00
|
|
|
|
2014-04-04 23:27:56 +00:00
|
|
|
if tcp, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcp.SetKeepAlive(true)
|
|
|
|
tcp.SetNoDelay(true)
|
2019-08-12 16:47:02 +00:00
|
|
|
|
|
|
|
// Expose TCPConn CloseWrite method on HalfCloser
|
2016-10-26 02:20:24 +00:00
|
|
|
hc = tcp
|
2014-04-04 23:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if TLS is enabled
|
2020-05-28 08:18:30 +00:00
|
|
|
if p.TLSConfigurator.UseTLS(dc) {
|
|
|
|
wrapper := p.TLSConfigurator.OutgoingRPCWrapper()
|
2014-04-04 23:27:56 +00:00
|
|
|
// Switch the connection into TLS mode
|
2020-03-09 20:59:02 +00:00
|
|
|
if _, err := conn.Write([]byte{byte(tlsRPCType)}); err != nil {
|
2014-04-04 23:27:56 +00:00
|
|
|
conn.Close()
|
2016-10-26 02:20:24 +00:00
|
|
|
return nil, nil, err
|
2014-04-04 23:27:56 +00:00
|
|
|
}
|
2013-12-09 20:09:57 +00:00
|
|
|
|
2014-04-04 23:27:56 +00:00
|
|
|
// Wrap the connection in a TLS client
|
2019-06-27 20:22:07 +00:00
|
|
|
tlsConn, err := wrapper(dc, conn)
|
2014-06-22 19:49:51 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2016-10-26 02:20:24 +00:00
|
|
|
return nil, nil, err
|
2014-06-22 19:49:51 +00:00
|
|
|
}
|
2014-10-02 17:26:25 +00:00
|
|
|
conn = tlsConn
|
2019-08-12 16:47:02 +00:00
|
|
|
|
|
|
|
// If this is a tls.Conn, expose HalfCloser to caller
|
|
|
|
if tlsConn, ok := conn.(*tls.Conn); ok {
|
|
|
|
hc = tlsConn
|
|
|
|
}
|
2014-04-04 23:27:56 +00:00
|
|
|
}
|
2013-12-09 20:09:57 +00:00
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// Send the type-byte for the protocol if one is required.
|
|
|
|
//
|
|
|
|
// When using insecure TLS there is no inner type-byte as these connections
|
|
|
|
// aren't wrapped like the standard TLS ones are.
|
|
|
|
if tlsRPCType != RPCTLSInsecure {
|
|
|
|
if _, err := conn.Write([]byte{byte(actualRPCType)}); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
return conn, hc, nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:05:09 +00:00
|
|
|
func (p *ConnPool) RPCClientTimeout() time.Duration {
|
|
|
|
return time.Duration(atomic.LoadUint32(&p.clientTimeoutMs)) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) SetRPCClientTimeout(timeout time.Duration) {
|
|
|
|
if timeout > time.Hour {
|
|
|
|
// Prevent unreasonably large timeouts that might overflow a uint32
|
|
|
|
timeout = time.Hour
|
|
|
|
}
|
|
|
|
atomic.StoreUint32(&p.clientTimeoutMs, uint32(timeout.Milliseconds()))
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
// DialRPCViaMeshGateway dials the destination node and sets up the connection
|
|
|
|
// to be the correct RPC type using ALPN. This currently is exclusively used to
|
|
|
|
// dial other servers in foreign datacenters via mesh gateways.
|
|
|
|
func DialRPCViaMeshGateway(
|
|
|
|
ctx context.Context,
|
|
|
|
dc string, // (metadata.Server).Datacenter
|
|
|
|
nodeName string, // (metadata.Server).ShortName
|
|
|
|
srcAddr *net.TCPAddr,
|
|
|
|
alpnWrapper tlsutil.ALPNWrapper,
|
|
|
|
nextProto string,
|
2020-03-09 20:59:02 +00:00
|
|
|
dialingFromServer bool,
|
|
|
|
gatewayResolver func(string) string,
|
|
|
|
) (net.Conn, HalfCloser, error) {
|
|
|
|
if !dialingFromServer {
|
|
|
|
return nil, nil, fmt.Errorf("must dial via mesh gateways from a server agent")
|
|
|
|
} else if gatewayResolver == nil {
|
|
|
|
return nil, nil, fmt.Errorf("gatewayResolver is nil")
|
2021-08-24 21:28:44 +00:00
|
|
|
} else if alpnWrapper == nil {
|
2020-03-09 20:59:02 +00:00
|
|
|
return nil, nil, fmt.Errorf("cannot dial via a mesh gateway when outgoing TLS is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
gwAddr := gatewayResolver(dc)
|
|
|
|
if gwAddr == "" {
|
|
|
|
return nil, nil, structs.ErrDCNotAvailable
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
dialer := &net.Dialer{LocalAddr: srcAddr, Timeout: DefaultDialTimeout}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
rawConn, err := dialer.DialContext(ctx, "tcp", gwAddr)
|
2016-10-26 02:20:24 +00:00
|
|
|
if err != nil {
|
2020-03-09 20:59:02 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
if nextProto != ALPN_RPCGRPC {
|
2022-07-13 15:33:48 +00:00
|
|
|
// agent/grpc-internal/client.go:dial() handles this in another way for gRPC
|
2021-08-24 21:28:44 +00:00
|
|
|
if tcp, ok := rawConn.(*net.TCPConn); ok {
|
|
|
|
_ = tcp.SetKeepAlive(true)
|
|
|
|
_ = tcp.SetNoDelay(true)
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: now we wrap the connection in a TLS client.
|
2021-08-24 21:28:44 +00:00
|
|
|
tlsConn, err := alpnWrapper(dc, nodeName, nextProto, rawConn)
|
2020-03-09 20:59:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn net.Conn = tlsConn
|
|
|
|
|
|
|
|
var hc HalfCloser
|
|
|
|
if tlsConn, ok := conn.(*tls.Conn); ok {
|
|
|
|
// Expose *tls.Conn CloseWrite method on HalfCloser
|
|
|
|
hc = tlsConn
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, hc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNewConn is used to return a new connection
|
2020-05-28 08:18:30 +00:00
|
|
|
func (p *ConnPool) getNewConn(dc string, nodeName string, addr net.Addr) (*Conn, error) {
|
2020-03-09 20:59:02 +00:00
|
|
|
if nodeName == "" {
|
|
|
|
return nil, fmt.Errorf("pool: ConnPool.getNewConn requires a node name")
|
2016-10-26 02:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// Get a new, raw connection and write the Consul multiplex byte to set the mode
|
2020-05-28 08:56:10 +00:00
|
|
|
conn, _, err := p.DialTimeout(dc, nodeName, addr, RPCMultiplexV2)
|
2020-03-09 20:59:02 +00:00
|
|
|
if err != nil {
|
2017-04-21 01:59:42 +00:00
|
|
|
return nil, err
|
2014-05-27 21:33:09 +00:00
|
|
|
}
|
2013-12-09 22:52:22 +00:00
|
|
|
|
2017-04-21 01:59:42 +00:00
|
|
|
conf := yamux.DefaultConfig()
|
2020-07-29 18:43:33 +00:00
|
|
|
// override the default because LogOutput conflicts with Logger.
|
|
|
|
conf.LogOutput = nil
|
|
|
|
conf.Logger = p.Logger
|
2017-04-21 01:59:42 +00:00
|
|
|
|
|
|
|
// Create a multiplexed session
|
2020-06-10 20:15:32 +00:00
|
|
|
session, err := yamux.Client(conn, conf)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, fmt.Errorf("Failed to create yamux client: %w", err)
|
|
|
|
}
|
2017-04-21 01:59:42 +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,
|
2020-03-09 20:59:02 +00:00
|
|
|
nodeName: nodeName,
|
2015-02-16 22:04:47 +00:00
|
|
|
addr: addr,
|
|
|
|
session: session,
|
|
|
|
clients: list.New(),
|
|
|
|
lastUsed: time.Now(),
|
|
|
|
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) {
|
2020-03-09 20:59:02 +00:00
|
|
|
if conn.nodeName == "" {
|
|
|
|
panic("pool: ConnPool.acquire requires a node name")
|
|
|
|
}
|
|
|
|
|
2014-05-28 23:55:39 +00:00
|
|
|
// Ensure returned streams are closed
|
|
|
|
atomic.StoreInt32(&conn.shouldClose, 1)
|
|
|
|
|
|
|
|
// Clear from the cache
|
2017-03-15 16:06:21 +00:00
|
|
|
addrStr := conn.addr.String()
|
2020-03-09 20:59:02 +00:00
|
|
|
poolKey := conn.nodeName + ":" + addrStr
|
2014-02-06 00:19:05 +00:00
|
|
|
p.Lock()
|
2020-03-09 20:59:02 +00:00
|
|
|
if c, ok := p.pool[poolKey]; ok && c == conn {
|
|
|
|
delete(p.pool, poolKey)
|
2014-05-28 23:55:39 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2020-05-28 07:48:34 +00:00
|
|
|
// getClient is used to get a usable client for an address
|
2020-05-28 08:18:30 +00:00
|
|
|
func (p *ConnPool) getClient(dc string, nodeName string, addr net.Addr) (*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
|
2020-05-28 08:18:30 +00:00
|
|
|
conn, err := p.acquire(dc, nodeName, addr)
|
2013-12-09 22:58:49 +00:00
|
|
|
if err != nil {
|
2020-11-10 21:50:09 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to get conn: %w", 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
|
|
|
|
}
|
2020-11-10 21:50:09 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to start stream: %w", 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
|
2020-03-09 20:59:02 +00:00
|
|
|
func (p *ConnPool) RPC(
|
|
|
|
dc string,
|
|
|
|
nodeName string,
|
|
|
|
addr net.Addr,
|
|
|
|
method string,
|
|
|
|
args interface{},
|
|
|
|
reply interface{},
|
|
|
|
) error {
|
|
|
|
if nodeName == "" {
|
|
|
|
return fmt.Errorf("pool: ConnPool.RPC requires a node name")
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
// TODO (autoconf) probably will want to have a way to invoke the
|
|
|
|
// secure or insecure variant depending on whether its an ongoing
|
|
|
|
// or first time config request. For now though this is fine until
|
|
|
|
// those ongoing requests are implemented.
|
2020-07-07 19:39:04 +00:00
|
|
|
if method == "AutoEncrypt.Sign" || method == "AutoConfig.InitialConfiguration" {
|
2020-06-23 17:18:22 +00:00
|
|
|
return p.rpcInsecure(dc, addr, method, args, reply)
|
2019-06-27 20:22:07 +00:00
|
|
|
} else {
|
2020-05-28 07:48:34 +00:00
|
|
|
return p.rpc(dc, nodeName, addr, method, args, reply)
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rpcInsecure is used to make an RPC call to a remote host.
|
|
|
|
// It doesn't actually use any of the pooling, it is here so that it is
|
|
|
|
// transparent for the consumer. The pool cannot be used because
|
|
|
|
// AutoEncrypt.Sign is a one-off call and it doesn't make sense to pool that
|
|
|
|
// connection if it is not being reused.
|
2020-06-23 17:18:22 +00:00
|
|
|
func (p *ConnPool) rpcInsecure(dc string, addr net.Addr, method string, args interface{}, reply interface{}) error {
|
2020-05-28 08:18:30 +00:00
|
|
|
if dc != p.Datacenter {
|
|
|
|
return fmt.Errorf("insecure dialing prohibited between datacenters")
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
var codec rpc.ClientCodec
|
2020-06-23 17:18:22 +00:00
|
|
|
conn, _, err := p.dial(dc, addr, 0, RPCTLSInsecure)
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
2022-10-25 17:13:41 +00:00
|
|
|
return fmt.Errorf("rpcinsecure: error establishing connection: %w", err)
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
2020-02-07 21:50:24 +00:00
|
|
|
codec = msgpackrpc.NewCodecFromHandle(true, true, conn, structs.MsgpackHandle)
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
// Make the RPC call
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, method, args, reply)
|
|
|
|
if err != nil {
|
2022-10-25 17:13:41 +00:00
|
|
|
return fmt.Errorf("rpcinsecure: error making call: %w", err)
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 19:05:09 +00:00
|
|
|
// BlockableQuery represents a read query which can be blocking or non-blocking.
|
|
|
|
// This interface is used to override the rpc_client_timeout for blocking queries.
|
|
|
|
type BlockableQuery interface {
|
|
|
|
// BlockingTimeout returns duration > 0 if the query is blocking.
|
|
|
|
// Otherwise returns 0 for non-blocking queries.
|
|
|
|
BlockingTimeout(maxQueryTime, defaultQueryTime time.Duration) time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ BlockableQuery = (*structs.QueryOptions)(nil)
|
|
|
|
var _ BlockableQuery = (*pbcommon.QueryOptions)(nil)
|
|
|
|
|
2020-05-28 07:48:34 +00:00
|
|
|
func (p *ConnPool) rpc(dc string, nodeName string, addr net.Addr, method string, args interface{}, reply interface{}) error {
|
2017-06-15 13:16:16 +00:00
|
|
|
p.once.Do(p.init)
|
|
|
|
|
2014-05-28 23:55:39 +00:00
|
|
|
// Get a usable client
|
2020-05-28 08:18:30 +00:00
|
|
|
conn, sc, err := p.getClient(dc, nodeName, addr)
|
2014-05-28 23:55:39 +00:00
|
|
|
if err != nil {
|
2020-10-15 21:40:51 +00:00
|
|
|
return fmt.Errorf("rpc error getting client: %w", err)
|
2014-05-28 23:55:39 +00:00
|
|
|
}
|
2013-12-09 22:58:49 +00:00
|
|
|
|
2022-10-18 19:05:09 +00:00
|
|
|
var deadline time.Time
|
|
|
|
timeout := p.RPCClientTimeout()
|
|
|
|
if bq, ok := args.(BlockableQuery); ok {
|
|
|
|
blockingTimeout := bq.BlockingTimeout(p.MaxQueryTime, p.DefaultQueryTime)
|
|
|
|
if blockingTimeout > 0 {
|
2022-11-24 15:13:02 +00:00
|
|
|
// Override the default client timeout but add RPCHoldTimeout
|
|
|
|
// as a buffer for retries during leadership changes.
|
|
|
|
timeout = blockingTimeout + p.RPCHoldTimeout
|
2022-10-18 19:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if timeout > 0 {
|
|
|
|
deadline = time.Now().Add(timeout)
|
|
|
|
}
|
|
|
|
if err := sc.stream.SetReadDeadline(deadline); err != nil {
|
|
|
|
return fmt.Errorf("rpc error setting read deadline: %w", err)
|
2022-04-21 20:21:35 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2017-09-25 22:27:04 +00:00
|
|
|
|
|
|
|
// See the comment in leader_test.go TestLeader_ChangeServerID
|
|
|
|
// about how we found this. The tldr is that if we see this
|
|
|
|
// error, we know this connection is toast, so we should clear
|
|
|
|
// it and make a new one on the next attempt.
|
2017-10-10 22:19:50 +00:00
|
|
|
if lib.IsErrEOF(err) {
|
2017-09-25 22:27:04 +00:00
|
|
|
p.clearConn(conn)
|
|
|
|
}
|
|
|
|
|
2014-05-28 23:55:39 +00:00
|
|
|
p.releaseConn(conn)
|
2020-10-15 21:40:51 +00:00
|
|
|
return fmt.Errorf("rpc error making call: %w", 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
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
// Ping sends a Status.Ping message to the specified server and
|
2016-03-27 02:28:13 +00:00
|
|
|
// returns true if healthy, false if an error occurred
|
2020-05-28 07:48:34 +00:00
|
|
|
func (p *ConnPool) Ping(dc string, nodeName string, addr net.Addr) (bool, error) {
|
2016-03-27 02:28:13 +00:00
|
|
|
var out struct{}
|
2020-05-28 07:48:34 +00:00
|
|
|
err := p.RPC(dc, nodeName, addr, "Status.Ping", struct{}{}, &out)
|
2017-06-15 13:16:16 +00:00
|
|
|
return err == nil, err
|
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
|
2017-06-15 13:16:16 +00:00
|
|
|
if now.Sub(conn.lastUsed) < p.MaxTime {
|
2014-02-06 00:19:05 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|