Files
tasks/common/rpc/interceptor/namespace_rate_limit.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

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"
"github.com/hanzoai/tasks/common/dynamicconfig"
"github.com/hanzoai/tasks/common/headers"
"github.com/hanzoai/tasks/common/namespace"
"github.com/hanzoai/tasks/common/quotas"
"github.com/hanzoai/tasks/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 any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (any, 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 any,
) bool {
switch request := req.(type) {
case *workflowservice.GetWorkflowExecutionHistoryRequest:
return request.GetWaitNewEvent()
}
return false
}
func IsLongPollDescribeActivityExecutionRequest(
req any,
) bool {
switch request := req.(type) {
case *workflowservice.DescribeActivityExecutionRequest:
return len(request.GetLongPollToken()) > 0
}
return false
}