go.temporal.io/server@v1.23.0/common/rpc/interceptor/namespace_rate_limit.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package interceptor
    26  
    27  import (
    28  	"context"
    29  	"time"
    30  
    31  	enumspb "go.temporal.io/api/enums/v1"
    32  	"go.temporal.io/api/serviceerror"
    33  	"go.temporal.io/server/common/headers"
    34  	"google.golang.org/grpc"
    35  
    36  	"go.temporal.io/server/common/namespace"
    37  	"go.temporal.io/server/common/quotas"
    38  )
    39  
    40  const (
    41  	NamespaceRateLimitDefaultToken = 1
    42  )
    43  
    44  var (
    45  	ErrNamespaceRateLimitServerBusy = serviceerror.NewResourceExhausted(enumspb.RESOURCE_EXHAUSTED_CAUSE_RPS_LIMIT, "namespace rate limit exceeded")
    46  )
    47  
    48  type (
    49  	NamespaceRateLimitInterceptor struct {
    50  		namespaceRegistry namespace.Registry
    51  		rateLimiter       quotas.RequestRateLimiter
    52  		tokens            map[string]int
    53  	}
    54  )
    55  
    56  var _ grpc.UnaryServerInterceptor = (*NamespaceRateLimitInterceptor)(nil).Intercept
    57  
    58  func NewNamespaceRateLimitInterceptor(
    59  	namespaceRegistry namespace.Registry,
    60  	rateLimiter quotas.RequestRateLimiter,
    61  	tokens map[string]int,
    62  ) *NamespaceRateLimitInterceptor {
    63  	return &NamespaceRateLimitInterceptor{
    64  		namespaceRegistry: namespaceRegistry,
    65  		rateLimiter:       rateLimiter,
    66  		tokens:            tokens,
    67  	}
    68  }
    69  
    70  func (ni *NamespaceRateLimitInterceptor) Intercept(
    71  	ctx context.Context,
    72  	req interface{},
    73  	info *grpc.UnaryServerInfo,
    74  	handler grpc.UnaryHandler,
    75  ) (interface{}, error) {
    76  	_, methodName := SplitMethodName(info.FullMethod)
    77  	token, ok := ni.tokens[methodName]
    78  	if !ok {
    79  		token = NamespaceRateLimitDefaultToken
    80  	}
    81  
    82  	namespace := MustGetNamespaceName(ni.namespaceRegistry, req)
    83  	if !ni.rateLimiter.Allow(time.Now().UTC(), quotas.NewRequest(
    84  		methodName,
    85  		token,
    86  		namespace.String(),
    87  		headers.GetValues(ctx, headers.CallerTypeHeaderName)[0],
    88  		0,  // this interceptor layer does not throttle based on caller segment
    89  		"", // this interceptor layer does not throttle based on call initiation
    90  	)) {
    91  		return nil, ErrNamespaceRateLimitServerBusy
    92  	}
    93  	return handler(ctx, req)
    94  }