Skip to content

Commit

Permalink
Export helper for setting Nexus failure source on context (temporalio…
Browse files Browse the repository at this point in the history
…#7286)

## What changed?
<!-- Describe what has changed in this PR -->

## Why?
<!-- Tell your future self why have you made these changes -->

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
  • Loading branch information
pdoerner authored Feb 7, 2025
1 parent ea6a2e6 commit 1e99d45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
22 changes: 22 additions & 0 deletions common/nexus/failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
package nexus

import (
"context"
"encoding/json"
"errors"
"net/http"
"sync/atomic"

"github.com/nexus-rpc/sdk-go/nexus"
commonpb "go.temporal.io/api/common/v1"
Expand All @@ -48,6 +50,26 @@ type failureSourceContextKeyType struct{}

var FailureSourceContextKey = failureSourceContextKeyType{}

func SetFailureSourceOnContext(ctx context.Context, response *http.Response) {
if response == nil || response.Header == nil {
return
}

failureSourceHeader := response.Header.Get(FailureSourceHeaderName)
if failureSourceHeader == "" {
return
}

failureSourceContext := ctx.Value(FailureSourceContextKey)
if failureSourceContext == nil {
return
}

if val, ok := failureSourceContext.(*atomic.Value); ok {
val.Store(failureSourceHeader)
}
}

var failureTypeString = string((&failurepb.Failure{}).ProtoReflect().Descriptor().FullName())

// ProtoFailureToNexusFailure converts a proto Nexus Failure to a Nexus SDK Failure.
Expand Down
23 changes: 1 addition & 22 deletions components/nexusoperations/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"context"
"fmt"
"net/http"
"sync/atomic"

"github.com/nexus-rpc/sdk-go/nexus"
"go.temporal.io/api/serviceerror"
Expand Down Expand Up @@ -140,7 +139,7 @@ func ClientProviderFactory(
httpCaller = func(r *http.Request) (*http.Response, error) {
r.Header.Set(NexusCallbackSourceHeader, clusterInfo.ClusterID)
resp, callErr := httpClient.Do(r)
setFailureSourceOnContext(ctx, resp)
commonnexus.SetFailureSourceOnContext(ctx, resp)
return resp, callErr
}
}
Expand All @@ -158,23 +157,3 @@ func ClientProviderFactory(
func CallbackTokenGeneratorProvider() *commonnexus.CallbackTokenGenerator {
return commonnexus.NewCallbackTokenGenerator()
}

func setFailureSourceOnContext(ctx context.Context, response *http.Response) {
if response == nil || response.Header == nil {
return
}

failureSourceHeader := response.Header.Get(commonnexus.FailureSourceHeaderName)
if failureSourceHeader == "" {
return
}

failureSourceContext := ctx.Value(commonnexus.FailureSourceContextKey)
if failureSourceContext == nil {
return
}

if val, ok := failureSourceContext.(*atomic.Value); ok {
val.Store(failureSourceHeader)
}
}

0 comments on commit 1e99d45

Please sign in to comment.