Documentation
¶
Overview ¶
Package clientx provides Connect client factory with retry, circuit breaker, and timeouts.
Overview:
- Responsibility: Create Connect HTTP clients with production-ready interceptors
- Key Types: Options for client configuration, interceptors for resilience
- Concurrency Model: Clients are safe for concurrent use
- Error Semantics: Retry only on transient/idempotent errors
- Performance Notes: Circuit breaker prevents cascade failures
Usage:
client := clientx.NewHTTPClient("https://api.example.com",
clientx.WithTimeout(5*time.Second),
clientx.WithRetry(3),
)
Package clientx provides Connect HTTP client construction with retry, circuit breaker, idempotency headers, and timeouts.
Overview ¶
clientx offers production-grade HTTP clients suitable for Connect-based services. It includes exponential backoff retry, optional circuit breaker, and request timeouts while keeping APIs minimal and composable.
Features ¶
- Exponential backoff retries for transient 5xx errors
- Optional circuit breaker to prevent cascade failures
- Request timeouts and idempotency key injection
- Generic helper for constructing typed Connect clients
Usage ¶
client := clientx.NewHTTPClient("https://api.example.com",
clientx.WithTimeout(5*time.Second),
clientx.WithRetry(3),
clientx.WithCircuitBreaker(true),
)
Layer ¶
clientx belongs to Layer 3 (L3) and depends on core/log, connectx (optionally).
Stability ¶
Stable since v0.1.0. Minor versions may introduce backward-compatible improvements.
Package clientx provides client-side metrics collection.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientMetricsInterceptor ¶
func ClientMetricsInterceptor(collector *ClientMetricsCollector) connect.UnaryInterceptorFunc
ClientMetricsInterceptor creates a Connect client interceptor that collects outbound RPC metrics. It records request count and duration for all outbound RPC calls.
Parameters:
- collector: client metrics collector instance
Returns:
- connect.UnaryInterceptorFunc: client interceptor function
Metrics collected:
- rpc_client_requests_total: counter of outbound requests by service, method, code
- rpc_client_request_duration_seconds: histogram of outbound request duration
Labels:
- rpc_service: target service name
- rpc_method: target method name
- rpc_code: Connect error code
Concurrency:
- Safe for concurrent use
func NewConnectClient ¶
func NewConnectClient[T any](baseURL, serviceName string, newClient func(connect.HTTPClient, string, ...connect.ClientOption) T, opts ...Option) T
NewConnectClient creates a Connect client with interceptors. This is a convenience wrapper for creating Connect clients with standard interceptors.
Types ¶
type ClientMetricsCollector ¶
type ClientMetricsCollector struct {
// contains filtered or unexported fields
}
ClientMetricsCollector holds OpenTelemetry metrics instruments for client-side RPC monitoring.
func NewClientMetricsCollector ¶
func NewClientMetricsCollector(otelProvider *obsx.Provider) (*ClientMetricsCollector, error)
NewClientMetricsCollector creates a new metrics collector for client-side RPC monitoring. If otelProvider is nil, metrics collection is disabled.
Parameters:
- otelProvider: OpenTelemetry provider (can be nil to disable metrics)
Returns:
- *ClientMetricsCollector: metrics collector instance
- error: initialization error if metrics setup fails
Concurrency:
- Safe for concurrent use after initialization
type Option ¶
type Option func(*Options)
Option is a functional option for configuring the client.
func WithCircuitBreaker ¶
WithCircuitBreaker enables or disables the circuit breaker.
func WithIdempotencyKey ¶
WithIdempotencyKey sets the idempotency key header name.
type Options ¶
type Options struct {
Timeout time.Duration // Request timeout (default: 30s)
MaxRetries int // Maximum retry attempts (default: 3)
RetryBackoff time.Duration // Initial backoff duration (default: 100ms)
EnableCircuit bool // Enable circuit breaker (default: true)
CircuitThreshold uint32 // Circuit breaker failure threshold (default: 5)
IdempotencyKey string // Custom idempotency key header name
}
Options configures the HTTP client behavior.