Files
tasks/components/nexusoperations/fx.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

147 lines
4.8 KiB
Go

package nexusoperations
import (
"context"
"fmt"
"net/http"
"go.temporal.io/api/serviceerror"
persistencespb "github.com/hanzoai/tasks/api/persistence/v1"
"github.com/hanzoai/tasks/common"
"github.com/hanzoai/tasks/common/cluster"
"github.com/hanzoai/tasks/common/collection"
"github.com/hanzoai/tasks/common/dynamicconfig"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/metrics"
commonnexus "github.com/hanzoai/tasks/common/nexus"
"github.com/hanzoai/tasks/common/nexus/nexusrpc"
"github.com/hanzoai/tasks/common/persistence"
"github.com/hanzoai/tasks/common/resource"
"go.uber.org/fx"
)
var Module = fx.Module(
"component.nexusoperations",
fx.Provide(ConfigProvider),
fx.Provide(ClientProviderFactory),
fx.Provide(DefaultNexusTransportProvider),
fx.Provide(CallbackTokenGeneratorProvider),
fx.Provide(EndpointRegistryProvider),
fx.Invoke(EndpointRegistryLifetimeHooks),
fx.Invoke(RegisterStateMachines),
fx.Invoke(RegisterTaskSerializers),
fx.Invoke(RegisterEventDefinitions),
fx.Invoke(RegisterExecutor),
)
const NexusCallbackSourceHeader = "Nexus-Callback-Source"
func EndpointRegistryProvider(
matchingClient resource.MatchingClient,
endpointManager persistence.NexusEndpointManager,
dc *dynamicconfig.Collection,
logger log.Logger,
metricsHandler metrics.Handler,
) commonnexus.EndpointRegistry {
registryConfig := commonnexus.NewEndpointRegistryConfig(dc)
return commonnexus.NewEndpointRegistry(
registryConfig,
matchingClient,
endpointManager,
logger,
metricsHandler,
)
}
func EndpointRegistryLifetimeHooks(lc fx.Lifecycle, registry commonnexus.EndpointRegistry) {
lc.Append(fx.StartStopHook(registry.StartLifecycle, registry.StopLifecycle))
}
// NexusTransportProvider type alias allows a provider to customize the default implementation specifically for Nexus.
type NexusTransportProvider func(namespaceID, serviceName string) http.RoundTripper
func DefaultNexusTransportProvider() NexusTransportProvider {
return func(namespaceID, serviceName string) http.RoundTripper {
// In the future, we'll want to inject headers and certs here.
// For now this is must be done externally via a custom transport provider.
return http.DefaultTransport
}
}
type clientProviderCacheKey struct {
namespaceID, endpointID string
// URL is part of the cache key in case the service configuration is modified to use a new URL after caching the
// client for the service.
url string
}
func ClientProviderFactory(
httpTransportProvider NexusTransportProvider,
clusterMetadata cluster.Metadata,
rpcFactory common.RPCFactory,
config *Config,
) (ClientProvider, error) {
cl, err := rpcFactory.CreateLocalFrontendHTTPClient()
if err != nil {
return nil, fmt.Errorf("cannot create local frontend HTTP client: %w", err)
}
var clusterID string
if clusterInfo, ok := clusterMetadata.GetAllClusterInfo()[clusterMetadata.GetCurrentClusterName()]; ok {
clusterID = clusterInfo.ClusterID
}
// TODO(bergundy): This should use an LRU or other form of cache that supports eviction.
m := collection.NewFallibleOnceMap(func(key clientProviderCacheKey) (*http.Client, error) {
transport := httpTransportProvider(key.namespaceID, key.endpointID)
return &http.Client{
Transport: ResponseSizeLimiter{transport},
}, nil
})
return func(ctx context.Context, namespaceID string, entry *persistencespb.NexusEndpointEntry, service string) (*nexusrpc.HTTPClient, error) {
var url string
var httpClient *http.Client
httpCaller := httpClient.Do
switch variant := entry.Endpoint.Spec.Target.Variant.(type) {
case *persistencespb.NexusEndpointTarget_External_:
url = variant.External.GetUrl()
var err error
httpClient, err = m.Get(clientProviderCacheKey{namespaceID, entry.Id, url})
if err != nil {
return nil, err
}
if clusterID != "" {
httpCaller = func(r *http.Request) (*http.Response, error) {
resp, callErr := httpClient.Do(r)
commonnexus.SetFailureSourceOnContext(ctx, resp)
return resp, callErr
}
}
case *persistencespb.NexusEndpointTarget_Worker_:
url = cl.BaseURL() + "/" + commonnexus.RouteDispatchNexusTaskByEndpoint.Path(entry.Id)
httpClient = &cl.Client
if clusterID != "" {
httpCaller = func(r *http.Request) (*http.Response, error) {
r.Header.Set(NexusCallbackSourceHeader, clusterID)
resp, callErr := httpClient.Do(r)
commonnexus.SetFailureSourceOnContext(ctx, resp)
return resp, callErr
}
}
default:
return nil, serviceerror.NewInternal("got unexpected endpoint target")
}
// still need to override the caller
return nexusrpc.NewHTTPClient(nexusrpc.HTTPClientOptions{
BaseURL: url,
Service: service,
HTTPCaller: httpCaller,
Serializer: commonnexus.PayloadSerializer,
})
}, nil
}
func CallbackTokenGeneratorProvider() *commonnexus.CallbackTokenGenerator {
return commonnexus.NewCallbackTokenGenerator()
}