rpc

package
v1.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTimeout = 10 * time.Second

Variables

This section is empty.

Functions

func CallRPC

func CallRPC[T any](
	ctx context.Context,
	tracer trace.Tracer,
	spanName string,
	serviceName string,
	call func(ctx context.Context, opts ...grpc.CallOption) (T, error),
	opts ...Option,
) (T, *apierror.APIError)

CallRPC executes a gRPC call with standard boilerplate:

  1. Starts a tracing span named spanName on tracer, deferred End.
  2. Applies a timeout (default DefaultTimeout, overridable via WithTimeout).
  3. Captures response header metadata.
  4. Invokes call with the prepared context and grpc.Header option.
  5. Converts any gRPC error to an apierror.APIError using serviceName.
  6. Checks the response headers for the idempotent-replayed marker and invokes the WithOnReplayed callback when present.

func PrepareRPCCtx

func PrepareRPCCtx(ctx context.Context, opts ...MetadataOption) context.Context

PrepareRPCCtx returns a new context with outgoing gRPC metadata configured by the supplied options. Existing outgoing metadata on ctx is preserved.

func PrepareServiceCallCtx

func PrepareServiceCallCtx(ctx context.Context, opts ...ServiceCallOption) context.Context

PrepareServiceCallCtx builds outgoing gRPC metadata for a service-to-service call. It always forwards identity, the idempotency key, the request ID, and the propagated client IP from the incoming context. Callers can supply ServiceCallOption values to customise the defaults (e.g. override the idempotency key).

Use this instead of assembling metadata manually in each gRPC client.

Types

type MetadataOption

type MetadataOption func(metadata.MD)

MetadataOption configures the outgoing gRPC metadata built by PrepareRPCCtx.

func WithIdentity

func WithIdentity(ctx context.Context) MetadataOption

WithIdentity adds the caller's identity from the context to the outgoing gRPC metadata. If the context does not carry an identity the option is a no-op.

func WithMetadata

func WithMetadata(key string, values ...string) MetadataOption

WithMetadata sets arbitrary key/value pairs on the outgoing gRPC metadata.

type Option

type Option func(*config)

Option configures the behavior of CallRPC.

func WithOnReplayed

func WithOnReplayed(fn func()) Option

WithOnReplayed registers a callback that is invoked when the response headers indicate the server replayed a cached idempotent response.

func WithTimeout

func WithTimeout(t time.Duration) Option

WithTimeout overrides the default RPC deadline (DefaultTimeout).

type ServiceCallOption

type ServiceCallOption func(*serviceCallConfig)

ServiceCallOption configures the behavior of PrepareServiceCallCtx.

func WithIdempotencyKeyOverride

func WithIdempotencyKeyOverride(key string) ServiceCallOption

WithIdempotencyKeyOverride sets an explicit idempotency key on the outgoing metadata, overriding whatever the context carries. Use this when a caller needs to pass a derived key (e.g. appending a suffix) instead of forwarding the original.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL