Files
tasks/common/rpc/interceptor/caller_info.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

81 lines
1.9 KiB
Go

package interceptor
import (
"context"
"github.com/hanzoai/tasks/common/api"
"github.com/hanzoai/tasks/common/headers"
"github.com/hanzoai/tasks/common/namespace"
"google.golang.org/grpc"
)
type (
CallerInfoInterceptor struct {
namespaceRegistry namespace.Registry
}
)
var _ grpc.UnaryServerInterceptor = (*CallerInfoInterceptor)(nil).Intercept
func NewCallerInfoInterceptor(
namespaceRegistry namespace.Registry,
) *CallerInfoInterceptor {
return &CallerInfoInterceptor{
namespaceRegistry: namespaceRegistry,
}
}
func (i *CallerInfoInterceptor) Intercept(
ctx context.Context,
req any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (any, error) {
ctx = PopulateCallerInfo(
ctx,
func() string { return string(MustGetNamespaceName(i.namespaceRegistry, req)) },
func() string { return api.MethodName(info.FullMethod) },
)
return handler(ctx, req)
}
// PopulateCallerInfo gets current caller info value from the context and updates any that are missing.
// Namespace name and method are passed as functions to avoid expensive lookups if those values are already set.
func PopulateCallerInfo(
ctx context.Context,
nsNameGetter func() string,
methodGetter func() string,
) context.Context {
callerInfo := headers.GetCallerInfo(ctx)
infoUpdated := false
nsName := nsNameGetter()
if callerInfo.CallerName != nsName {
callerInfo.CallerName = nsName
infoUpdated = true
}
_, isValidCallerType := headers.ValidCallerTypes[callerInfo.CallerType]
if !isValidCallerType {
callerInfo.CallerType = headers.CallerTypeAPI
infoUpdated = true
}
if callerInfo.CallerType == headers.CallerTypeAPI ||
callerInfo.CallerType == headers.CallerTypeOperator {
methodName := methodGetter()
if callerInfo.CallOrigin != methodName {
callerInfo.CallOrigin = methodName
infoUpdated = true
}
}
if infoUpdated {
ctx = headers.SetCallerInfo(ctx, callerInfo)
}
return ctx
}