2020-09-09 21:51:51 +00:00
|
|
|
package grpc
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2021-02-16 15:17:54 +00:00
|
|
|
"time"
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2021-02-16 15:17:54 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
|
|
|
)
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// ClientConnPool creates and stores a connection for each datacenter.
|
|
|
|
type ClientConnPool struct {
|
|
|
|
dialer dialer
|
|
|
|
servers ServerLocator
|
|
|
|
conns map[string]*grpc.ClientConn
|
|
|
|
connsLock sync.Mutex
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
type ServerLocator interface {
|
|
|
|
// ServerForAddr is used to look up server metadata from an address.
|
|
|
|
ServerForAddr(addr string) (*metadata.Server, error)
|
|
|
|
// Scheme returns the url scheme to use to dial the server. This is primarily
|
|
|
|
// needed for testing multiple agents in parallel, because gRPC requires the
|
|
|
|
// resolver to be registered globally.
|
|
|
|
Scheme() string
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// TLSWrapper wraps a non-TLS connection and returns a connection with TLS
|
|
|
|
// enabled.
|
|
|
|
type TLSWrapper func(dc string, conn net.Conn) (net.Conn, error)
|
|
|
|
|
|
|
|
type dialer func(context.Context, string) (net.Conn, error)
|
|
|
|
|
2021-01-06 16:21:22 +00:00
|
|
|
// NewClientConnPool create new GRPC client pool to connect to servers using GRPC over RPC
|
|
|
|
func NewClientConnPool(servers ServerLocator, tls TLSWrapper, useTLSForDC func(dc string) bool) *ClientConnPool {
|
2020-09-09 22:46:58 +00:00
|
|
|
return &ClientConnPool{
|
2021-01-06 16:21:22 +00:00
|
|
|
dialer: newDialer(servers, tls, useTLSForDC),
|
2020-09-09 22:46:58 +00:00
|
|
|
servers: servers,
|
|
|
|
conns: make(map[string]*grpc.ClientConn),
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// ClientConn returns a grpc.ClientConn for the datacenter. If there are no
|
|
|
|
// existing connections in the pool, a new one will be created, stored in the pool,
|
|
|
|
// then returned.
|
|
|
|
func (c *ClientConnPool) ClientConn(datacenter string) (*grpc.ClientConn, error) {
|
|
|
|
c.connsLock.Lock()
|
|
|
|
defer c.connsLock.Unlock()
|
2020-09-09 20:37:43 +00:00
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
if conn, ok := c.conns[datacenter]; ok {
|
2020-09-09 20:37:43 +00:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
conn, err := grpc.Dial(
|
|
|
|
fmt.Sprintf("%s:///server.%s", c.servers.Scheme(), datacenter),
|
2020-09-09 20:37:43 +00:00
|
|
|
// use WithInsecure mode here because we handle the TLS wrapping in the
|
|
|
|
// custom dialer based on logic around whether the server has TLS enabled.
|
|
|
|
grpc.WithInsecure(),
|
2020-09-09 22:46:58 +00:00
|
|
|
grpc.WithContextDialer(c.dialer),
|
2020-09-09 20:37:43 +00:00
|
|
|
grpc.WithDisableRetry(),
|
2020-11-11 19:27:07 +00:00
|
|
|
grpc.WithStatsHandler(newStatsHandler(defaultMetrics())),
|
2020-09-09 22:46:58 +00:00
|
|
|
// nolint:staticcheck // there is no other supported alternative to WithBalancerName
|
2021-02-16 15:17:54 +00:00
|
|
|
grpc.WithBalancerName("pick_first"),
|
|
|
|
// Keep alive parameters are based on the same default ones we used for
|
|
|
|
// Yamux. These are somewhat arbitrary but we did observe in scale testing
|
|
|
|
// that the gRPC defaults (servers send keepalives only every 2 hours,
|
|
|
|
// clients never) seemed to result in TCP drops going undetected until
|
|
|
|
// actual updates needed to be sent which caused unnecessary delays for
|
|
|
|
// deliveries. These settings should be no more work for servers than
|
|
|
|
// existing yamux clients but hopefully allow TCP drops to be detected
|
|
|
|
// earlier and so have a smaller chance of going unnoticed until there are
|
|
|
|
// actual updates to send out from the servers. The servers have a policy to
|
|
|
|
// not accept pings any faster than once every 15 seconds to protect against
|
|
|
|
// abuse.
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: 30 * time.Second,
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}))
|
2020-09-09 20:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
c.conns[datacenter] = conn
|
2020-09-09 20:37:43 +00:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDialer returns a gRPC dialer function that conditionally wraps the connection
|
2020-09-09 22:46:58 +00:00
|
|
|
// with TLS based on the Server.useTLS value.
|
2021-01-06 16:21:22 +00:00
|
|
|
func newDialer(servers ServerLocator, wrapper TLSWrapper, useTLSForDC func(dc string) bool) func(context.Context, string) (net.Conn, error) {
|
2020-09-09 20:37:43 +00:00
|
|
|
return func(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
d := net.Dialer{}
|
|
|
|
conn, err := d.DialContext(ctx, "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
server, err := servers.ServerForAddr(addr)
|
|
|
|
if err != nil {
|
2020-09-15 18:11:48 +00:00
|
|
|
conn.Close()
|
2020-09-09 22:46:58 +00:00
|
|
|
return nil, err
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 16:21:22 +00:00
|
|
|
if server.UseTLS && useTLSForDC(server.Datacenter) {
|
2020-09-09 20:37:43 +00:00
|
|
|
if wrapper == nil {
|
2020-09-15 18:11:48 +00:00
|
|
|
conn.Close()
|
2020-09-09 20:37:43 +00:00
|
|
|
return nil, fmt.Errorf("TLS enabled but got nil TLS wrapper")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch the connection into TLS mode
|
|
|
|
if _, err := conn.Write([]byte{byte(pool.RPCTLS)}); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the connection in a TLS client
|
|
|
|
tlsConn, err := wrapper(server.Datacenter, conn)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conn = tlsConn
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte{pool.RPCGRPC})
|
|
|
|
if err != nil {
|
2020-09-15 18:11:48 +00:00
|
|
|
conn.Close()
|
2020-09-09 20:37:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|