client

package
v0.0.0-...-3d19f0e Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a gRPC client with built-in observability, retry logic, and circuit breaker support.

func NewClient

func NewClient(opts ...Option) (*Client, error)

NewClient creates a new gRPC client with observability.

Example:

c, err := client.NewClient(
    client.WithAddress("localhost:50051"),
    client.WithServiceName("order-service"),
    client.WithTracing(tracingClient),
)

func (*Client) Close

func (c *Client) Close() error

Close closes the gRPC connection.

func (*Client) Connection

func (c *Client) Connection() *grpc.ClientConn

Connection returns the underlying gRPC connection.

func (*Client) Invoke

func (c *Client) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error

Invoke performs a unary gRPC call with observability and optional retry.

func (*Client) NewStream

func (c *Client) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)

NewStream creates a new streaming gRPC call with observability.

type Config

type Config struct {
	// Address is the gRPC server address (e.g., "localhost:50051")
	Address string

	// ServiceName is the name of the service for metrics/tracing.
	ServiceName string

	// UseTLS enables TLS for the connection.
	UseTLS bool

	// TLSConfig holds TLS configuration.
	TLSConfig *TLSConfig

	// Timeout is the default timeout for each request.
	Timeout time.Duration

	// MaxRetries is the maximum number of retry attempts.
	MaxRetries int

	// EnableCompression enables gzip compression for gRPC messages.
	// Reduces bandwidth usage at low CPU cost.
	EnableCompression bool
}

Config holds the configuration for the gRPC client.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default gRPC configuration.

type Option

type Option func(*Client) error

Option configures the gRPC client. All options are applied during NewClient construction.

Example:

c, err := client.NewClient(
    client.WithAddress("localhost:50051"),
    client.WithServiceName("order-service"),
    client.WithTracing(tracingClient),
    client.WithMetrics(metricsClient),
)

func WithAddress

func WithAddress(addr string) Option

WithAddress sets the gRPC server address.

func WithCircuitBreaker

func WithCircuitBreaker(cbCfg *resilience.CircuitBreakerConfig) Option

WithCircuitBreaker adds circuit breaker support.

func WithLogger

func WithLogger(log *logger.Logger) Option

WithLogger sets a custom logger for the gRPC client.

func WithMetrics

func WithMetrics(metricsClient metrics.Client) Option

WithMetrics adds metrics support.

func WithRetry

func WithRetry(retryCfg *resilience.RetryConfig) Option

WithRetry adds retry support.

func WithServiceName

func WithServiceName(name string) Option

WithServiceName sets the service name for metrics.

func WithTLS

func WithTLS(cfg *TLSConfig) Option

WithTLS enables TLS with the given configuration.

func WithTracing

func WithTracing(tracingClient tracing.Client) Option

WithTracing adds distributed tracing support.

type TLSConfig

type TLSConfig struct {
	// CertFile is the path to the client TLS certificate.
	CertFile string
	// KeyFile is the path to the client TLS key.
	KeyFile string
	// CAFile is the path to the CA certificate for server verification.
	CAFile string
	// ServerNameOverride is used to verify server hostname.
	ServerNameOverride string
}

TLSConfig holds TLS certificate paths for mutual TLS authentication.

Jump to

Keyboard shortcuts

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