Documentation
¶
Overview ¶
Package nexusrpc provides client and server implementations of the Nexus HTTP API
Index ¶
- Constants
- func FormatDuration(d time.Duration) string
- func MarkAsWrapperError(failureConverter FailureConverter, opErr *nexus.OperationError) error
- func NewCompletionHTTPHandler(options CompletionHandlerOptions) http.Handler
- func NewHTTPHandler(options HandlerOptions) http.Handler
- func ParseDuration(value string) (time.Duration, error)
- type BaseHTTPHandler
- type ClientStartOperationResponse
- type CompleteOperationOptions
- type CompletionHTTPClient
- type CompletionHTTPClientOptions
- type CompletionHandler
- type CompletionHandlerOptions
- type CompletionRequest
- type FailureConverter
- type HTTPClient
- type HTTPClientOptions
- type HandlerOptions
- type OperationHandle
- type UnexpectedResponseError
Constants ¶
const ( // HeaderTemporalNexusFailureSupport is a Temporal specific header that toggles behavior of how failures are // serialized over the wire when set to the string "true". // NOTE: If / when porting this package back into the Nexus SDK, make sure not to copy this header over. // The header should be safe to remove by server version 1.32.0. HeaderTemporalNexusFailureSupport = "temporal-nexus-failure-support" )
Variables ¶
This section is empty.
Functions ¶
func FormatDuration ¶
FormatDuration converts a duration into a string representation in millisecond resolution.
func MarkAsWrapperError ¶
func MarkAsWrapperError(failureConverter FailureConverter, opErr *nexus.OperationError) error
MarkAsWrapperError adds the "unwrap-error" metadata to the original failure of the given OperationError, which signals the Temporal codebase to unwrap the underlying failure as the failure cause. This is used as a shim for Temporal->Temporal calls. Temporal already has a Failure type that represents an OperationError and does not need to record this wrapper error.
func NewCompletionHTTPHandler ¶
func NewCompletionHTTPHandler(options CompletionHandlerOptions) http.Handler
NewCompletionHTTPHandler constructs an http.Handler from given options for handling operation completion requests.
func NewHTTPHandler ¶
func NewHTTPHandler(options HandlerOptions) http.Handler
NewHTTPHandler constructs an http.Handler from given options for handling Nexus service requests.
Types ¶
type BaseHTTPHandler ¶
type BaseHTTPHandler struct {
Logger *slog.Logger
FailureConverter FailureConverter
}
func (*BaseHTTPHandler) WriteFailure ¶
func (h *BaseHTTPHandler) WriteFailure(writer http.ResponseWriter, r *http.Request, err error)
WriteFailure writes the given error to the response, converting it to a Failure if possible. It also sets the HTTP status code based on the type of error. nolint:revive // Keeping all of the logic together for readability, even if it means the function is long.
type ClientStartOperationResponse ¶
type ClientStartOperationResponse[T any] struct { // Set when start completes synchronously and successfully. // // If T is a [LazyValue], ensure that your consume it or read the underlying content in its entirety and close it to // free up the underlying connection. Successful T // Set when the handler indicates that it started an asynchronous operation. // The attached handle can be used to perform actions such as cancel the operation or get its result. Pending *OperationHandle[T] // Links contain information about the operations done by the handler. Links []nexus.Link }
ClientStartOperationResponse is the return type of HTTPClient.StartOperation. One and only one of Successful or Pending will be non-nil.
func StartOperation ¶
func StartOperation[I, O any](ctx context.Context, client *HTTPClient, operation nexus.OperationReference[I, O], input I, request nexus.StartOperationOptions) (*ClientStartOperationResponse[O], error)
StartOperation is the type safe version of HTTPClient.StartOperation. It accepts input of type I and returns a ClientStartOperationResponse of type O, removing the need to consume the [LazyValue] returned by the client method.
type CompleteOperationOptions ¶
type CompleteOperationOptions struct {
// Header to send in the completion request.
// Note that this is a Nexus header, not an HTTP header.
Header nexus.Header
// OperationToken is the unique token for this operation. Used when a completion callback is received before a
// started response.
OperationToken string
// StartTime is the time the operation started. Used when a completion callback is received before a started response.
StartTime time.Time
// CloseTime is the time the operation completed. This may be different from the time the completion callback is delivered.
CloseTime time.Time
// Links are used to link back to the operation when a completion callback is received before a started response.
Links []nexus.Link
// Error to send with the completion. If set, the completion is unsuccessful.
Error *nexus.OperationError
// Result to deliver with the completion. Uses the client's serializer to serialize the result into the request body.
// Only used for successful completions.
Result any
}
CompleteOperationOptions is input for CompleteOperation. If Error is set, the completion is unsuccessful. Otherwise, it is successful.
type CompletionHTTPClient ¶
type CompletionHTTPClient struct {
// contains filtered or unexported fields
}
CompletionHTTPClient is a client for sending Nexus operation completion callbacks via HTTP.
func NewCompletionHTTPClient ¶
func NewCompletionHTTPClient(options CompletionHTTPClientOptions) *CompletionHTTPClient
NewCompletionHTTPClient constructs a CompletionHTTPClient from given options for sending Nexus operation completion callbacks via HTTP.
func (*CompletionHTTPClient) CompleteOperation ¶
func (c *CompletionHTTPClient) CompleteOperation(ctx context.Context, url string, completion CompleteOperationOptions) error
CompleteOperation sends a completion callback for a Nexus operation to the given URL with the given completion details.
type CompletionHTTPClientOptions ¶
type CompletionHTTPClientOptions struct {
// A function for making HTTP requests.
// Defaults to [http.DefaultClient.Do].
HTTPCaller func(*http.Request) (*http.Response, error)
// A [serializer] to customize client serialization behavior.
// By default the client handles JSONables, byte slices, and nil.
Serializer nexus.Serializer
// A [failureConverter] to convert a [Failure] instance to and from an [error]. Defaults to
// [DefaultFailureConverter].
FailureConverter FailureConverter
}
CompletionHTTPClientOptions are options for NewCompletionHTTPClient.
type CompletionHandler ¶
type CompletionHandler interface {
CompleteOperation(context.Context, *CompletionRequest) error
}
A CompletionHandler can receive operation completion requests as delivered via the callback URL provided in start-operation requests.
type CompletionHandlerOptions ¶
type CompletionHandlerOptions struct {
// Handler for completion requests.
Handler CompletionHandler
// A stuctured logging handler.
// Defaults to slog.Default().
Logger *slog.Logger
// A [Serializer] to customize handler serialization behavior.
// By default the handler handles, JSONables, byte slices, and nil.
Serializer nexus.Serializer
// A [FailureConverter] to convert a [Failure] instance to and from an [error]. Defaults to
// [DefaultFailureConverter].
FailureConverter FailureConverter
}
CompletionHandlerOptions are options for NewCompletionHTTPHandler.
type CompletionRequest ¶
type CompletionRequest struct {
// The original HTTP request.
HTTPRequest *http.Request
// State of the operation.
State nexus.OperationState
// OperationToken is the unique token for this operation. Used when a completion callback is received before a
// started response.
OperationToken string
// StartTime is the time the operation started. Used when a completion callback is received before a started response.
StartTime time.Time
// CloseTime is the time the operation completed. This may be different from the time the completion callback is delivered.
CloseTime time.Time
// Links are used to link back to the operation when a completion callback is received before a started response.
Links []nexus.Link
// Parsed from request and set if State is failed or canceled.
Error *nexus.OperationError
// Extracted from request and set if State is succeeded.
Result *nexus.LazyValue
}
CompletionRequest is input for CompletionHandler.CompleteOperation.
type FailureConverter ¶
type FailureConverter interface {
// ErrorToFailure converts an [error] to a [Failure].
// Note that the provided error may be nil.
ErrorToFailure(error) (nexus.Failure, error)
// FailureToError converts a [Failure] to an [error].
FailureToError(nexus.Failure) (error, error)
}
FailureConverter is used by the framework to transform [error] instances to and from [Failure] instances. To customize conversion logic, implement this interface and provide your implementation to framework methods such as NewHTTPClient and NewHTTPHandler. By default the SDK translates [HandlerError], [OperationError] and [FailureError] to and from [Failure] objects maintaining their cause chain. Arbitrary errors are translated to a [Failure] object with its Message set to the Error() string, losing the cause chain.
func DefaultFailureConverter ¶
func DefaultFailureConverter() FailureConverter
DefaultFailureConverter returns the package's default FailureConverter implementation. Translates [HandlerError], [OperationError] and [FailureError] to and from [Failure] objects maintaining their cause chain. Arbitrary errors are translated to a [Failure] object with its Message set to the Error() string, losing the cause chain. [Failure] instances are converted to [FailureError] to allow access to the full failure metadata and details if available.
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
An HTTPClient makes Nexus service requests as defined in the Nexus HTTP API.
It can start a new operation and get an OperationHandle to an existing, asynchronous operation.
Use an OperationHandle to cancel, get the result of, and get information about asynchronous operations.
OperationHandles can be obtained either by starting new operations or by calling HTTPClient.NewOperationHandle for existing operations.
func NewHTTPClient ¶
func NewHTTPClient(options HTTPClientOptions) (*HTTPClient, error)
NewHTTPClient creates a new HTTPClient from provided HTTPClientOptions. BaseURL and Service are required.
func (*HTTPClient) NewOperationHandle ¶
func (c *HTTPClient) NewOperationHandle(operation string, token string) (*OperationHandle[*nexus.LazyValue], error)
NewOperationHandle gets a handle to an asynchronous operation by name and token. Does not incur a trip to the server. Fails if provided an empty operation or token.
func (*HTTPClient) StartOperation ¶
func (c *HTTPClient) StartOperation( ctx context.Context, operation string, input any, options nexus.StartOperationOptions, ) (*ClientStartOperationResponse[*nexus.LazyValue], error)
StartOperation calls the configured Nexus endpoint to start an operation.
This method has the following possible outcomes:
The operation completes successfully. The result of this call will be set as a [LazyValue] in ClientStartOperationResult.Successful and must be consumed to free up the underlying connection.
The operation was started and the handler has indicated that it will complete asynchronously. An OperationHandle will be returned as ClientStartOperationResult.Pending, which can be used to perform actions such as getting its result.
The operation was unsuccessful. The returned result will be nil and error will be an [OperationError].
Any other error.
nolint:revive // (cyclomatic complexity) Containing the entire implementation inline is clearer here.
type HTTPClientOptions ¶
type HTTPClientOptions struct {
// Base URL for all requests. Required.
BaseURL string
// Service name. Required.
Service string
// A function for making HTTP requests.
// Defaults to [http.DefaultClient.Do].
HTTPCaller func(*http.Request) (*http.Response, error)
// A [Serializer] to customize client serialization behavior.
// By default the client handles JSONables, byte slices, and nil.
Serializer nexus.Serializer
// A [FailureConverter] to convert a [Failure] instance to and from an [error]. Defaults to
// [DefaultFailureConverter].
FailureConverter FailureConverter
}
HTTPClientOptions are options for creating an HTTPClient.
type HandlerOptions ¶
type HandlerOptions struct {
// Handler for handling service requests.
Handler nexus.Handler
// A stuctured logger.
// Defaults to slog.Default().
Logger *slog.Logger
// Max duration to allow waiting for a single get result request.
// Enforced if provided for requests with the wait query parameter set.
//
// Defaults to one minute.
GetResultTimeout time.Duration
// A [Serializer] to customize handler serialization behavior.
// By default the handler handles JSONables, byte slices, and nil.
Serializer nexus.Serializer
// A [FailureConverter] to convert a [Failure] instance to and from an [error].
// Defaults to [DefaultFailureConverter].
FailureConverter FailureConverter
}
HandlerOptions are options for NewHTTPHandler.
type OperationHandle ¶
type OperationHandle[T any] struct { // Name of the Operation this handle represents. Operation string // Handler generated token for this handle's operation. Token string // contains filtered or unexported fields }
An OperationHandle is used to cancel operations and get their result and status.
func (*OperationHandle[T]) Cancel ¶
func (h *OperationHandle[T]) Cancel(ctx context.Context, options nexus.CancelOperationOptions) error
Cancel requests to cancel an asynchronous operation.
Cancelation is asynchronous and may be not be respected by the operation's implementation.
type UnexpectedResponseError ¶
type UnexpectedResponseError struct {
// Error message.
Message string
// Optional failure that may have been emedded in the response.
Failure *nexus.Failure
// Additional transport specific details.
// For HTTP, this would include the HTTP response. The response body will have already been read into memory and
// does not need to be closed.
Details any
}
UnexpectedResponseError indicates a client encountered something unexpected in the server's response.
func (*UnexpectedResponseError) Error ¶
func (e *UnexpectedResponseError) Error() string
Error implements the error interface.