go.temporal.io/server/components/callbacks/request.go

146 LOC · 61 covered · 85 uncovered · 21 ranges · 17 concepts · 17 introducers · 9 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filego.temporal.io/server/common/nexus/callback_token.go · 127 LOCnexus/callback_token.gorequest.go ×1 · 4 introduced LOCrequest.go ×1request.go ×1 · 5 introduced LOCrequest.go ×1callback_token.go ×2 · 10 introduced LOCcallback_token.go ×2request.go ×1 · 7 introduced LOCrequest.go ×1request.go ×2 · 4 introduced LOCrequest.go ×2request.go ×1 · 3 introduced LOCrequest.go ×1request.go ×3 · 9 introduced LOCrequest.go ×3request.go ×1 · 2 introduced LOCrequest.go ×1request.go ×1 · 3 introduced LOCrequest.go ×1request.go ×1 · 2 introduced LOCrequest.go ×1request.go ×1 · 2 introduced LOCrequest.go ×1request.go ×1 · 3 introduced LOCrequest.go ×1request.go ×1 · 1 introduced LOCrequest.go ×1request.go ×1 · 2 introduced LOCrequest.go ×1request.go ×1 · 5 introduced LOCrequest.go ×1request.go ×1 · 2 introduced LOCrequest.go ×1request.go ×1 · 3 introduced LOCrequest.go ×1TestRouteRequest_ExternalTarget · introduced test · go.temporal.io/server/components/callbacks/TestRouteRequest_ExternalTargetTestRouteRequest_Externa…TestRouteRequest_SourceHeaderLocal · introduced test · go.temporal.io/server/components/callbacks/TestRouteRequest_SourceHeaderLocalTestRouteRequest_SourceH…TestRouteRequest_SourceHeaderUnknownCluster · introduced test · go.temporal.io/server/components/callbacks/TestRouteRequest_SourceHeaderUnknownClusterTestRouteRequest_SourceH…TestRouteRequest_SystemCallback · introduced test · go.temporal.io/server/components/callbacks/TestRouteRequest_SystemCallbackTestRouteRequest_SystemC…TestRouteSystemCallbackRequest_InvalidToken · introduced test · go.temporal.io/server/components/callbacks/TestRouteSystemCallbackRequest_InvalidTokenTestRouteSystemCallbackR…TestRouteSystemCallbackRequest_InvalidTokenData · introduced test · go.temporal.io/server/components/callbacks/TestRouteSystemCallbackRequest_InvalidTokenDataTestRouteSystemCallbackR…TestRouteSystemCallbackRequest_NamespaceNotFound · introduced test · go.temporal.io/server/components/callbacks/TestRouteSystemCallbackRequest_NamespaceNotFoundTestRouteSystemCallbackR…TestRouteSystemCallbackRequest_NilHeaders · introduced test · go.temporal.io/server/components/callbacks/TestRouteSystemCallbackRequest_NilHeadersTestRouteSystemCallbackR…TestRouteSystemCallbackRequest_Success · introduced test · go.temporal.io/server/components/callbacks/TestRouteSystemCallbackRequest_SuccessTestRouteSystemCallbackR…Focused file · go.temporal.io/server/components/callbacks/request.go · 146 LOCcallbacks/request.go

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package callbacks
2
3 import (
4 "errors"
5 "net/http"
6
7 "github.com/nexus-rpc/sdk-go/nexus"
8 "go.temporal.io/api/serviceerror"
9 "go.temporal.io/server/common"
10 "go.temporal.io/server/common/cluster"
11 "go.temporal.io/server/common/log"
12 "go.temporal.io/server/common/log/tag"
13 "go.temporal.io/server/common/namespace"
14 commonnexus "go.temporal.io/server/common/nexus"
15 )
16
17 // Header key used to identify callbacks that originate from and target the same cluster.
18 // Note: this is the nexusoperations.NexusCallbackSourceHeader stripped of Nexus-Callback-
19 const callbackSourceHeader = "source"
20
21 // routeSystemCallbackRequest routes a system callback request to the appropriate frontend client
22 // based on the callback token's namespace and active cluster.
23 func routeSystemCallbackRequest(
24 r *http.Request,
25 clusterMetadata cluster.Metadata,
26 namespaceRegistry namespace.Registry,
27 httpClientCache *cluster.FrontendHTTPClientCache,
28 callbackTokenGenerator *commonnexus.CallbackTokenGenerator,
29 localClient *common.FrontendHTTPClient,
30 logger log.Logger,
31 > ) (*http.Response, error) { request.go ×1
32 > var frontendClient *common.FrontendHTTPClient
33 > if r.Header != nil {
34 > token, err := commonnexus.DecodeCallbackToken(r.Header.Get(commonnexus.CallbackTokenHeader)) request.go ×1
35 > if err != nil {
36 > logger.Error("failed to decode callback token", tag.Error(err)) request.go ×1
37 > return nil, nexus.NewHandlerErrorf(nexus.HandlerErrorTypeBadRequest, "invalid callback token")
38 > }
39
40 > completion, err := callbackTokenGenerator.DecodeCompletion(token) request.go ×1
41 > if err != nil {
42 > logger.Error("failed to decode completion from token", tag.Error(err)) request.go ×1
43 > return nil, nexus.NewHandlerErrorf(nexus.HandlerErrorTypeBadRequest, "invalid callback token")
44 > }
45 // Resolve the target namespace/workflow from the token. For CHASM completions (e.g.
46 // standalone Nexus operations) the namespace lives inside the ComponentRef, not the
47 // top-level field, so use the shared helper rather than reading completion.NamespaceId
48 // directly.
49 > namespaceID, businessID, _, err := commonnexus.CompletionTarget(completion) callback_token.go ×2
50 > if err != nil {
51 logger.Error("failed to extract target from nexus completion token", tag.Error(err))
52 return nil, nexus.NewHandlerErrorf(nexus.HandlerErrorTypeBadRequest, "invalid callback token")
53 }
54 > ns, err := namespaceRegistry.GetNamespaceByID(namespace.ID(namespaceID)) callback_token.go ×2
55 > if err != nil {
56 > logger.Error("failed to get namespace for nexus completion request", tag.WorkflowNamespaceID(namespaceID), tag.Error(err)) request.go ×1
57 > var nfe *serviceerror.NamespaceNotFound
58 > if errors.As(err, &nfe) {
59 > return nil, nexus.NewHandlerErrorf(nexus.HandlerErrorTypeNotFound, "namespace %q not found", namespaceID)
60 > }
61 return nil, commonnexus.ConvertGRPCError(err, false)
62 }
63 > clusterName := ns.ActiveClusterName(namespace.RoutingKey{ID: businessID}) request.go ×1
64 > if clusterMetadata.GetCurrentClusterName() == clusterName {
65 > frontendClient = localClient
66 > } else {
67 fec, err := httpClientCache.Get(clusterName)
68 if err != nil {
69 logger.Warn(
70 "HTTPCallerProvider unable to get FrontendHTTPClient for callback target cluster. Using local HTTP Client.",
71 tag.SourceCluster(clusterMetadata.GetCurrentClusterName()),
72 tag.TargetCluster(clusterName),
73 tag.Error(err),
74 )
75 frontendClient = localClient
76 } else {
77 frontendClient = fec
78 }
79 }
80 > } else { request.go ×1
81 > frontendClient = localClient
82 > }
83 > r.URL.Path = commonnexus.PathCompletionCallbackNoIdentifier request.go ×1
84 > r.URL.Scheme = frontendClient.Scheme
85 > r.URL.Host = frontendClient.Address
86 > r.Host = frontendClient.Address
87 > return frontendClient.Do(r)
88 }
89
90 func routeRequest(
91 r *http.Request,
92 clusterMetadata cluster.Metadata,
93 namespaceRegistry namespace.Registry,
94 httpClientCache *cluster.FrontendHTTPClientCache,
95 callbackTokenGenerator *commonnexus.CallbackTokenGenerator,
96 defaultClient *http.Client,
97 localClient *common.FrontendHTTPClient,
98 logger log.Logger,
99 > ) (*http.Response, error) { request.go ×1
100 > if r.URL.String() == commonnexus.SystemCallbackURL {
101 > return routeSystemCallbackRequest(r, clusterMetadata, namespaceRegistry, httpClientCache, callbackTokenGenerator, localClient, logger) request.go ×1
102 > }
103 // This source header is populated in nexusoperations/executors (via the ClientProvider) for worker targets
104 // if this header is not populated then we assume it's an external target.
105 > if r.Header == nil || r.Header.Get(callbackSourceHeader) == "" { request.go ×1
106 > return defaultClient.Do(r) request.go ×1
107 > }
108 // If we got here, we assume that the endpoint in the original call was a worker target, and we should route
109 // internally, either to a local frontend, or one of the other connected clusters' frontends.
110 > var frontendClient *common.FrontendHTTPClient request.go ×3
111 > callbackSource := r.Header.Get(callbackSourceHeader)
112 > for clusterName, clusterInfo := range clusterMetadata.GetAllClusterInfo() {
113 > if callbackSource == clusterInfo.ClusterID {
114 > if clusterMetadata.GetCurrentClusterName() == clusterName { request.go ×2
115 > frontendClient = localClient
116 > } else {
117 fec, err := httpClientCache.Get(clusterName)
118 if err != nil {
119 logger.Warn(
120 "HTTPCallerProvider unable to get FrontendHTTPClient for callback target cluster. Using local HTTP Client.",
121 tag.SourceCluster(clusterMetadata.GetCurrentClusterName()),
122 tag.TargetCluster(clusterName),
123 tag.Error(err),
124 )
125 frontendClient = localClient
126 } else {
127 frontendClient = fec
128 }
129 }
130 > break request.go ×2
131 }
132 }
133 > if frontendClient == nil { request.go ×3
134 > // This can happen when a cluster is disconnected. request.go ×1
135 > logger.Warn(
136 > "HTTPCallerProvider unable to find the target cluster. Using local HTTP Client.",
137 > tag.SourceCluster(clusterMetadata.GetCurrentClusterName()),
138 > )
139 > frontendClient = localClient
140 > }
141
142 > r.URL.Scheme = frontendClient.Scheme request.go ×3
143 > r.URL.Host = frontendClient.Address
144 > r.Host = frontendClient.Address
145 > return frontendClient.Do(r)
146 }