Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Exclude gRPC health check requests from rate limiting in history & matching service #6036

Merged
merged 13 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common/authorization/default_authorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
healthpb "google.golang.org/grpc/health/grpc_health_v1"

"go.temporal.io/server/common/config"
)

Expand Down Expand Up @@ -79,7 +81,7 @@ var (
Namespace: testNamespace,
}
targetGrpcHealthCheck = CallTarget{
APIName: "/grpc.health.v1.Health/Check",
APIName: healthpb.Health_Check_FullMethodName,
Namespace: "",
}
targetGetSystemInfo = CallTarget{
Expand Down
8 changes: 6 additions & 2 deletions common/authorization/frontend_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@

package authorization

import "go.temporal.io/server/common/api"
import (
healthpb "google.golang.org/grpc/health/grpc_health_v1"

"go.temporal.io/server/common/api"
)

var healthCheckAPI = map[string]struct{}{
"/grpc.health.v1.Health/Check": {},
healthpb.Health_Check_FullMethodName: {},
"/temporal.api.workflowservice.v1.WorkflowService/GetSystemInfo": {},
}

Expand Down
5 changes: 5 additions & 0 deletions common/rpc/interceptor/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (i *RateLimitInterceptor) Allow(
token = RateLimitDefaultToken
}

// we don't want to apply rate limiter if a method is configured with 0 tokens.
if token < 1 {
return nil
}

if !i.rateLimiter.Allow(time.Now().UTC(), quotas.NewRequest(
methodName,
token,
Expand Down
105 changes: 105 additions & 0 deletions common/rpc/interceptor/rate_limit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package interceptor

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"

"go.temporal.io/server/common/quotas"
)

type (
// rateLimitInterceptorSuite struct {
rateLimitInterceptorSuite struct {
suite.Suite
*require.Assertions

controller *gomock.Controller
mockRateLimiter *quotas.MockRequestRateLimiter
}
)

func TestRateLimitInterceptorSuite(t *testing.T) {
suite.Run(t, &rateLimitInterceptorSuite{})
}

func (s *rateLimitInterceptorSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
s.mockRateLimiter = quotas.NewMockRequestRateLimiter(s.controller)
}

func (s *rateLimitInterceptorSuite) TestInterceptWithTokenConfig() {
methodName := "TEST/METHOD"
interceptor := NewRateLimitInterceptor(s.mockRateLimiter, map[string]int{methodName: 0})
// mock rate limiter should not be called.
s.mockRateLimiter.EXPECT().Allow(gomock.Any(), gomock.Any()).MaxTimes(0).Return(false)

handlerCalled := false
handler := func(ctx context.Context, req any) (any, error) {
handlerCalled = true
return nil, nil
}
_, err := interceptor.Intercept(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: methodName}, handler)
s.NoError(err)
s.True(handlerCalled)
}

func (s *rateLimitInterceptorSuite) TestInterceptWithNoTokenConfig() {
interceptor := NewRateLimitInterceptor(s.mockRateLimiter, nil)
// mock rate limiter is set to blocking.
s.mockRateLimiter.EXPECT().Allow(gomock.Any(), gomock.Any()).MaxTimes(1).Return(false)

handlerCalled := false
handler := func(ctx context.Context, req any) (any, error) {
handlerCalled = true
return nil, nil
}
_, err := interceptor.Intercept(context.Background(), nil, &grpc.UnaryServerInfo{}, handler)
s.Error(err)
s.False(handlerCalled)
}

func (s *rateLimitInterceptorSuite) TestInterceptWithNonZeroTokenConfig() {
methodName := "TEST/METHOD"
interceptor := NewRateLimitInterceptor(s.mockRateLimiter, map[string]int{methodName: 100})
// mock rate limiter is set to non-blocking.
s.mockRateLimiter.EXPECT().Allow(gomock.Any(), gomock.Any()).MaxTimes(1).Return(true)

handlerCalled := false
handler := func(ctx context.Context, req any) (any, error) {
handlerCalled = true
return nil, nil
}
_, err := interceptor.Intercept(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: methodName}, handler)
s.NoError(err)
s.True(handlerCalled)
}
5 changes: 4 additions & 1 deletion service/frontend/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
"go.temporal.io/server/service/frontend/configs"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/worker/scheduler"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

type (
Expand Down Expand Up @@ -395,7 +396,9 @@ func RateLimitInterceptorProvider(
quotas.NewDefaultIncomingRateBurst(namespaceReplicationInducingRateFn),
serviceConfig.OperatorRPSRatio,
),
map[string]int{},
map[string]int{
healthpb.Health_Check_FullMethodName: 0, // exclude health check requests from rate limiting.
},
)
}

Expand Down
5 changes: 4 additions & 1 deletion service/history/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import (
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/workflow"
"go.temporal.io/server/service/history/workflow/cache"
healthpb "google.golang.org/grpc/health/grpc_health_v1"

"go.temporal.io/server/components/callbacks"
"go.temporal.io/server/components/nexusoperations"
Expand Down Expand Up @@ -211,7 +212,9 @@ func RateLimitInterceptorProvider(
) *interceptor.RateLimitInterceptor {
return interceptor.NewRateLimitInterceptor(
configs.NewPriorityRateLimiter(func() float64 { return float64(serviceConfig.RPS()) }, serviceConfig.OperatorRPSRatio),
map[string]int{},
map[string]int{
healthpb.Health_Check_FullMethodName: 0, // exclude health check requests from rate limiting.
},
)
}

Expand Down
5 changes: 4 additions & 1 deletion service/matching/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"go.temporal.io/server/common/searchattribute"
"go.temporal.io/server/service"
"go.temporal.io/server/service/matching/configs"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

var Module = fx.Options(
Expand Down Expand Up @@ -101,7 +102,9 @@ func RateLimitInterceptorProvider(
) *interceptor.RateLimitInterceptor {
return interceptor.NewRateLimitInterceptor(
configs.NewPriorityRateLimiter(func() float64 { return float64(serviceConfig.RPS()) }, serviceConfig.OperatorRPSRatio),
map[string]int{},
map[string]int{
healthpb.Health_Check_FullMethodName: 0, // exclude health check requests from rate limiting.
},
)
}

Expand Down
Loading