2020-07-22 23:57:29 +00:00
|
|
|
/*
|
|
|
|
Package grpc provides a Handler and client for agent gRPC connections.
|
|
|
|
*/
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-02-16 15:17:54 +00:00
|
|
|
"time"
|
2020-07-22 23:57:29 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2021-08-22 18:06:26 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2021-02-16 15:17:54 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2021-08-22 18:06:26 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2021-08-07 12:21:12 +00:00
|
|
|
|
2021-10-16 17:02:03 +00:00
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
2021-08-22 18:06:26 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-07-22 23:57:29 +00:00
|
|
|
)
|
|
|
|
|
2020-08-28 23:23:40 +00:00
|
|
|
// NewHandler returns a gRPC server that accepts connections from Handle(conn).
|
2020-09-08 21:31:47 +00:00
|
|
|
// The register function will be called with the grpc.Server to register
|
|
|
|
// gRPC services with the server.
|
2021-08-22 18:06:26 +00:00
|
|
|
func NewHandler(logger Logger, addr net.Addr, register func(server *grpc.Server)) *Handler {
|
2020-11-11 19:27:07 +00:00
|
|
|
metrics := defaultMetrics()
|
2021-10-16 17:02:03 +00:00
|
|
|
|
2021-10-16 17:05:32 +00:00
|
|
|
// We don't need to pass tls.Config to the server since it's multiplexed
|
|
|
|
// behind the RPC listener, which already has TLS configured.
|
2021-10-16 17:02:03 +00:00
|
|
|
recoveryOpts := PanicHandlerMiddlewareOpts(logger)
|
|
|
|
|
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
grpc.StatsHandler(newStatsHandler(metrics)),
|
2021-08-07 12:21:12 +00:00
|
|
|
middleware.WithUnaryServerChain(
|
2021-08-22 18:06:26 +00:00
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.UnaryServerInterceptor(recoveryOpts...),
|
2021-08-07 12:21:12 +00:00
|
|
|
),
|
|
|
|
middleware.WithStreamServerChain(
|
2021-08-22 18:06:26 +00:00
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.StreamServerInterceptor(recoveryOpts...),
|
2021-08-07 12:21:12 +00:00
|
|
|
(&activeStreamCounter{metrics: metrics}).Intercept,
|
|
|
|
),
|
2021-02-16 15:17:54 +00:00
|
|
|
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
|
|
|
MinTime: 15 * time.Second,
|
|
|
|
}),
|
2021-10-16 17:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to pass tls.Config to the server since it's multiplexed
|
|
|
|
// behind the RPC listener, which already has TLS configured.
|
|
|
|
srv := grpc.NewServer(opts...)
|
2020-09-08 21:31:47 +00:00
|
|
|
register(srv)
|
2020-07-22 23:57:29 +00:00
|
|
|
|
2020-10-13 23:21:02 +00:00
|
|
|
lis := &chanListener{addr: addr, conns: make(chan net.Conn), done: make(chan struct{})}
|
2020-09-11 18:15:02 +00:00
|
|
|
return &Handler{srv: srv, listener: lis}
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 17:02:03 +00:00
|
|
|
// PanicHandlerMiddlewareOpts returns the []recovery.Option containing
|
|
|
|
// recovery handler function.
|
|
|
|
func PanicHandlerMiddlewareOpts(logger Logger) []recovery.Option {
|
|
|
|
return []recovery.Option{
|
|
|
|
recovery.WithRecoveryHandler(NewPanicHandler(logger)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPanicHandler returns a recovery.RecoveryHandlerFunc closure function
|
2021-08-22 18:06:26 +00:00
|
|
|
// to handle panic in GRPC server's handlers.
|
2021-10-16 17:02:03 +00:00
|
|
|
func NewPanicHandler(logger Logger) recovery.RecoveryHandlerFunc {
|
|
|
|
return func(p interface{}) (err error) {
|
2021-08-22 18:06:26 +00:00
|
|
|
// Log the panic and the stack trace of the Goroutine that caused the panic.
|
|
|
|
stacktrace := hclog.Stacktrace()
|
|
|
|
logger.Error("panic serving grpc request",
|
|
|
|
"panic", p,
|
|
|
|
"stack", stacktrace,
|
|
|
|
)
|
|
|
|
|
2021-10-16 17:02:03 +00:00
|
|
|
return status.Errorf(codes.Internal, "grpc: panic serving request")
|
2021-08-22 18:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:57:29 +00:00
|
|
|
// Handler implements a handler for the rpc server listener, and the
|
|
|
|
// agent.Component interface for managing the lifecycle of the grpc.Server.
|
|
|
|
type Handler struct {
|
|
|
|
srv *grpc.Server
|
|
|
|
listener *chanListener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the connection by sending it to a channel for the grpc.Server to receive.
|
|
|
|
func (h *Handler) Handle(conn net.Conn) {
|
2020-08-28 23:23:40 +00:00
|
|
|
h.listener.conns <- conn
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Run() error {
|
|
|
|
return h.srv.Serve(h.listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Shutdown() error {
|
|
|
|
h.srv.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// chanListener implements net.Listener for grpc.Server.
|
|
|
|
type chanListener struct {
|
|
|
|
conns chan net.Conn
|
|
|
|
addr net.Addr
|
2020-10-13 23:21:02 +00:00
|
|
|
done chan struct{}
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accept blocks until a connection is received from Handle, and then returns the
|
|
|
|
// connection. Accept implements part of the net.Listener interface for grpc.Server.
|
|
|
|
func (l *chanListener) Accept() (net.Conn, error) {
|
2020-09-11 18:15:02 +00:00
|
|
|
select {
|
2020-10-13 23:21:02 +00:00
|
|
|
case c := <-l.conns:
|
2020-09-11 18:15:02 +00:00
|
|
|
return c, nil
|
2020-10-13 23:21:02 +00:00
|
|
|
case <-l.done:
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "accept",
|
|
|
|
Net: l.addr.Network(),
|
|
|
|
Addr: l.addr,
|
|
|
|
Err: fmt.Errorf("listener closed"),
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
2020-07-22 23:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *chanListener) Addr() net.Addr {
|
|
|
|
return l.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *chanListener) Close() error {
|
2020-10-13 23:21:02 +00:00
|
|
|
close(l.done)
|
2020-07-22 23:57:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoOpHandler implements the same methods as Handler, but performs no handling.
|
|
|
|
// It may be used in place of Handler to disable the grpc server.
|
|
|
|
type NoOpHandler struct {
|
|
|
|
Logger Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type Logger interface {
|
|
|
|
Error(string, ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h NoOpHandler) Handle(conn net.Conn) {
|
|
|
|
h.Logger.Error("gRPC conn opened but gRPC RPC is disabled, closing",
|
|
|
|
"conn", logConn(conn))
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h NoOpHandler) Run() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h NoOpHandler) Shutdown() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// logConn is a local copy of github.com/hashicorp/memberlist.LogConn, to avoid
|
|
|
|
// a large dependency for a minor formatting function.
|
|
|
|
// logConn is used to keep log formatting consistent.
|
|
|
|
func logConn(conn net.Conn) string {
|
|
|
|
if conn == nil {
|
|
|
|
return "from=<unknown address>"
|
|
|
|
}
|
|
|
|
addr := conn.RemoteAddr()
|
|
|
|
if addr == nil {
|
|
|
|
return "from=<unknown address>"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("from=%s", addr.String())
|
|
|
|
}
|