1d95609fb7
Adds automation for generating the map of `gRPC Method Name → Rate Limit Type` used by the middleware introduced in #15550, and will ensure we don't forget to add new endpoints. Engineers must annotate their RPCs in the proto file like so: ``` rpc Foo(FooRequest) returns (FooResponse) { option (consul.internal.ratelimit.spec) = { operation_type: READ, }; } ``` When they run `make proto` a protoc plugin `protoc-gen-consul-rate-limit` will be installed that writes rate-limit specs as a JSON array to a file called `.ratelimit.tmp` (one per protobuf package/directory). After running Buf, `make proto` will execute a post-process script that will ingest all of the `.ratelimit.tmp` files and generate a Go file containing the mappings in the `agent/grpc-middleware` package. In the enterprise repository, it will write an additional file with the enterprise-only endpoints. If an engineer forgets to add the annotation to a new RPC, the plugin will return an error like so: ``` RPC Foo is missing rate-limit specification, fix it with: import "proto-public/annotations/ratelimit/ratelimit.proto"; service Bar { rpc Foo(...) returns (...) { option (hashicorp.consul.internal.ratelimit.spec) = { operation_type: OPERATION_READ | OPERATION_WRITE | OPERATION_EXEMPT, }; } } ``` In the future, this annotation can be extended to support rate-limit category (e.g. KV vs Catalog) and to determine the retry policy.
73 lines
2.6 KiB
Go
73 lines
2.6 KiB
Go
package external
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/armon/go-metrics"
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/keepalive"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/rate"
|
|
agentmiddleware "github.com/hashicorp/consul/agent/grpc-middleware"
|
|
"github.com/hashicorp/consul/tlsutil"
|
|
)
|
|
|
|
var (
|
|
metricsLabels = []metrics.Label{{
|
|
Name: "server_type",
|
|
Value: "external",
|
|
}}
|
|
)
|
|
|
|
// NewServer constructs a gRPC server for the external gRPC port, to which
|
|
// handlers can be registered.
|
|
func NewServer(logger agentmiddleware.Logger, metricsObj *metrics.Metrics, tls *tlsutil.Configurator, limiter rate.RequestLimitsHandler) *grpc.Server {
|
|
if metricsObj == nil {
|
|
metricsObj = metrics.Default()
|
|
}
|
|
recoveryOpts := agentmiddleware.PanicHandlerMiddlewareOpts(logger)
|
|
|
|
unaryInterceptors := []grpc.UnaryServerInterceptor{
|
|
// Add middlware interceptors to recover in case of panics.
|
|
recovery.UnaryServerInterceptor(recoveryOpts...),
|
|
}
|
|
streamInterceptors := []grpc.StreamServerInterceptor{
|
|
// Add middlware interceptors to recover in case of panics.
|
|
recovery.StreamServerInterceptor(recoveryOpts...),
|
|
agentmiddleware.NewActiveStreamCounter(metricsObj, metricsLabels).Intercept,
|
|
}
|
|
|
|
if tls != nil {
|
|
// Attach TLS middleware if TLS is provided.
|
|
authInterceptor := agentmiddleware.AuthInterceptor{TLS: tls, Logger: logger}
|
|
unaryInterceptors = append(unaryInterceptors, authInterceptor.InterceptUnary)
|
|
streamInterceptors = append(streamInterceptors, authInterceptor.InterceptStream)
|
|
}
|
|
opts := []grpc.ServerOption{
|
|
grpc.MaxConcurrentStreams(2048),
|
|
grpc.MaxRecvMsgSize(50 * 1024 * 1024),
|
|
grpc.InTapHandle(agentmiddleware.ServerRateLimiterMiddleware(limiter, agentmiddleware.NewPanicHandler(logger), logger)),
|
|
grpc.StatsHandler(agentmiddleware.NewStatsHandler(metricsObj, metricsLabels)),
|
|
middleware.WithUnaryServerChain(unaryInterceptors...),
|
|
middleware.WithStreamServerChain(streamInterceptors...),
|
|
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,
|
|
}),
|
|
}
|
|
|
|
if tls != nil {
|
|
// Attach TLS credentials, if provided.
|
|
tlsCreds := agentmiddleware.NewOptionalTransportCredentials(
|
|
credentials.NewTLS(tls.IncomingGRPCConfig()),
|
|
logger)
|
|
opts = append(opts, grpc.Creds(tlsCreds))
|
|
}
|
|
return grpc.NewServer(opts...)
|
|
}
|