2022-07-13 15:33:48 +00:00
|
|
|
package external
|
2022-03-22 12:40:24 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-24 16:31:38 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-22 12:40:24 +00:00
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
|
|
|
"google.golang.org/grpc"
|
2022-07-18 23:12:03 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2022-03-22 12:40:24 +00:00
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
agentmiddleware "github.com/hashicorp/consul/agent/grpc-middleware"
|
2022-03-22 12:40:24 +00:00
|
|
|
)
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
// NewServer constructs a gRPC server for the external gRPC port, to which
|
2022-03-22 12:40:24 +00:00
|
|
|
// handlers can be registered.
|
2022-09-01 17:32:11 +00:00
|
|
|
func NewServer(logger agentmiddleware.Logger) *grpc.Server {
|
2022-03-22 12:40:24 +00:00
|
|
|
recoveryOpts := agentmiddleware.PanicHandlerMiddlewareOpts(logger)
|
|
|
|
|
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
grpc.MaxConcurrentStreams(2048),
|
|
|
|
middleware.WithUnaryServerChain(
|
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.UnaryServerInterceptor(recoveryOpts...),
|
|
|
|
),
|
|
|
|
middleware.WithStreamServerChain(
|
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.StreamServerInterceptor(recoveryOpts...),
|
|
|
|
),
|
2022-07-18 23:12:03 +00:00
|
|
|
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
|
|
|
// This must be less than the keealive.ClientParameters Time setting, otherwise
|
|
|
|
// the server will disconnect the client for sending too many keepalive pings.
|
|
|
|
// Currently the client param is set to 30s.
|
|
|
|
MinTime: 15 * time.Second,
|
|
|
|
}),
|
2022-03-22 12:40:24 +00:00
|
|
|
}
|
|
|
|
return grpc.NewServer(opts...)
|
|
|
|
}
|