Documentation
¶
Index ¶
- Constants
- func CallRPC[T any](ctx context.Context, tracer trace.Tracer, spanName string, serviceName string, ...) (T, *apierror.APIError)
- func PrepareRPCCtx(ctx context.Context, opts ...MetadataOption) context.Context
- func PrepareServiceCallCtx(ctx context.Context, opts ...ServiceCallOption) context.Context
- type MetadataOption
- type Option
- type ServiceCallOption
Constants ¶
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:
- Starts a tracing span named spanName on tracer, deferred End.
- Applies a timeout (default DefaultTimeout, overridable via WithTimeout).
- Captures response header metadata.
- Invokes call with the prepared context and grpc.Header option.
- Converts any gRPC error to an apierror.APIError using serviceName.
- 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 ¶
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 ¶
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.