mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
- Add new public gRPC methods `DescribeActivityExecution` and `GetActivityExecutionOutcome`. See https://github.com/temporalio/api/pull/673 - These replace the previous `PollActivityExecution` - Respond to additional API changes: inlining of `ActivityOptions` - Configure quota for the new methods in their blocking and non-blocking forms - Update test suite - Implements agreed Standalone Activity design - [x] built - [x] covered by existing tests - [x] added new functional test(s)
118 lines
3.4 KiB
Go
118 lines
3.4 KiB
Go
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
"go.temporal.io/api/workflowservice/v1"
|
|
"go.temporal.io/server/common/dynamicconfig"
|
|
"go.temporal.io/server/common/headers"
|
|
"go.temporal.io/server/common/namespace"
|
|
"go.temporal.io/server/common/quotas"
|
|
"go.temporal.io/server/service/frontend/configs"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
const (
|
|
NamespaceRateLimitDefaultToken = 1
|
|
)
|
|
|
|
var (
|
|
ErrNamespaceRateLimitServerBusy = &serviceerror.ResourceExhausted{
|
|
Cause: enumspb.RESOURCE_EXHAUSTED_CAUSE_RPS_LIMIT,
|
|
Scope: enumspb.RESOURCE_EXHAUSTED_SCOPE_NAMESPACE,
|
|
Message: "namespace rate limit exceeded",
|
|
}
|
|
)
|
|
|
|
type (
|
|
NamespaceRateLimitInterceptor interface {
|
|
Intercept(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error)
|
|
Allow(namespaceName namespace.Name, methodName string, headerGetter headers.HeaderGetter) error
|
|
}
|
|
|
|
NamespaceRateLimitInterceptorImpl struct {
|
|
namespaceRegistry namespace.Registry
|
|
rateLimiter quotas.RequestRateLimiter
|
|
tokens map[string]int
|
|
reducePollWorkflowHistoryPriority dynamicconfig.BoolPropertyFn
|
|
}
|
|
)
|
|
|
|
var _ grpc.UnaryServerInterceptor = (*NamespaceRateLimitInterceptorImpl)(nil).Intercept
|
|
var _ NamespaceRateLimitInterceptor = (*NamespaceRateLimitInterceptorImpl)(nil)
|
|
|
|
func NewNamespaceRateLimitInterceptor(
|
|
namespaceRegistry namespace.Registry,
|
|
rateLimiter quotas.RequestRateLimiter,
|
|
tokens map[string]int,
|
|
) NamespaceRateLimitInterceptor {
|
|
return &NamespaceRateLimitInterceptorImpl{
|
|
namespaceRegistry: namespaceRegistry,
|
|
rateLimiter: rateLimiter,
|
|
tokens: tokens,
|
|
}
|
|
}
|
|
|
|
func (ni *NamespaceRateLimitInterceptorImpl) Intercept(
|
|
ctx context.Context,
|
|
req interface{},
|
|
info *grpc.UnaryServerInfo,
|
|
handler grpc.UnaryHandler,
|
|
) (interface{}, error) {
|
|
if ns := MustGetNamespaceName(ni.namespaceRegistry, req); ns != namespace.EmptyName {
|
|
method := info.FullMethod
|
|
if IsLongPollGetWorkflowExecutionHistoryRequest(req) {
|
|
method = configs.PollWorkflowHistoryAPIName
|
|
} else if IsLongPollDescribeActivityExecutionRequest(req) {
|
|
method = configs.PollActivityExecutionAPIName
|
|
}
|
|
if err := ni.Allow(ns, method, headers.NewGRPCHeaderGetter(ctx)); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return handler(ctx, req)
|
|
}
|
|
|
|
func (ni *NamespaceRateLimitInterceptorImpl) Allow(namespaceName namespace.Name, methodName string, headerGetter headers.HeaderGetter) error {
|
|
token, ok := ni.tokens[methodName]
|
|
if !ok {
|
|
token = NamespaceRateLimitDefaultToken
|
|
}
|
|
|
|
if !ni.rateLimiter.Allow(time.Now().UTC(), quotas.NewRequest(
|
|
methodName,
|
|
token,
|
|
namespaceName.String(),
|
|
headerGetter.Get(headers.CallerTypeHeaderName),
|
|
0, // this interceptor layer does not throttle based on caller segment
|
|
"", // this interceptor layer does not throttle based on call initiation
|
|
)) {
|
|
return ErrNamespaceRateLimitServerBusy
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func IsLongPollGetWorkflowExecutionHistoryRequest(
|
|
req interface{},
|
|
) bool {
|
|
switch request := req.(type) {
|
|
case *workflowservice.GetWorkflowExecutionHistoryRequest:
|
|
return request.GetWaitNewEvent()
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsLongPollDescribeActivityExecutionRequest(
|
|
req interface{},
|
|
) bool {
|
|
switch request := req.(type) {
|
|
case *workflowservice.DescribeActivityExecutionRequest:
|
|
return len(request.GetLongPollToken()) > 0
|
|
}
|
|
return false
|
|
}
|