go.temporal.io/server@v1.23.0/common/rpc/interceptor/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/quotas"
    37  )
    38  
    39  const (
    40  	RateLimitDefaultToken = 1
    41  )
    42  
    43  var (
    44  	RateLimitServerBusy = serviceerror.NewResourceExhausted(enumspb.RESOURCE_EXHAUSTED_CAUSE_RPS_LIMIT, "service rate limit exceeded")
    45  )
    46  
    47  type (
    48  	RateLimitInterceptor struct {
    49  		rateLimiter quotas.RequestRateLimiter
    50  		tokens      map[string]int
    51  	}
    52  )
    53  
    54  var _ grpc.UnaryServerInterceptor = (*RateLimitInterceptor)(nil).Intercept
    55  
    56  func NewRateLimitInterceptor(
    57  	rateLimiter quotas.RequestRateLimiter,
    58  	tokens map[string]int,
    59  ) *RateLimitInterceptor {
    60  	return &RateLimitInterceptor{
    61  		rateLimiter: rateLimiter,
    62  		tokens:      tokens,
    63  	}
    64  }
    65  
    66  func (i *RateLimitInterceptor) Intercept(
    67  	ctx context.Context,
    68  	req interface{},
    69  	info *grpc.UnaryServerInfo,
    70  	handler grpc.UnaryHandler,
    71  ) (interface{}, error) {
    72  	_, methodName := SplitMethodName(info.FullMethod)
    73  	token, ok := i.tokens[methodName]
    74  	if !ok {
    75  		token = RateLimitDefaultToken
    76  	}
    77  
    78  	if !i.rateLimiter.Allow(time.Now().UTC(), quotas.NewRequest(
    79  		methodName,
    80  		token,
    81  		"", // this interceptor layer does not throttle based on caller name
    82  		headers.GetValues(ctx, headers.CallerTypeHeaderName)[0],
    83  		0,  // this interceptor layer does not throttle based on caller segment
    84  		"", // this interceptor layer does not throttle based on call initiation
    85  	)) {
    86  		return nil, RateLimitServerBusy
    87  	}
    88  	return handler(ctx, req)
    89  }