Documentation
¶
Overview ¶
Package grpc provides a production-grade gRPC client wrapper with support for TLS, retry, backoff, interceptors, load balancing, and testability.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BackoffConfig ¶
type BackoffConfig struct {
BaseDelay time.Duration // BaseDelay is the amount of time to backoff after the first failure.
Multiplier float64 // Multiplier is the factor for multiplying backoffs after failed retry. Should be greater than 1.
Jitter float64 // Jitter is the factor with which backoffs are randomized.
MaxDelay time.Duration // MaxDelay is the upper bound of backoff delay.
}
BackoffConfig holds the configuration for exponential backoff strategy. It is used to control the retry behavior of the gRPC client. The backoff strategy is used when the client fails to connect to the server.
type Client ¶
type Client interface {
Conn() *grpc.ClientConn
HealthCheck(ctx context.Context) error
Close() error
}
Client defines the interface for GRPCClient, making it testable.
func NewGRPCClient ¶
func NewGRPCClient(opts *ClientOptions) (Client, error)
NewGRPCClient creates a new GRPCClient instance.
type ClientOptions ¶
type ClientOptions struct {
Target string // gRPC server address
TLSConfig *tls.Config // TLS configuration (optional)
Insecure bool // Allow insecure connection
MinConnectTimeout time.Duration // Minimum timeout for connection
MaxRetries int // Retry attempts on failure
EnableLoadBalancing bool // Enable round robin DNS LB
BackoffConfig *BackoffConfig // Backoff configuration for retrying connections
Logger logger.Logger // Logger for logging events during interceptors
DialOptions []grpc.DialOption
ContextDialer func(context.Context, string) (net.Conn, error)
}
ClientOptions holds the configuration required to initialize a GRPCClient.
type GRPCClient ¶
type GRPCClient struct {
// contains filtered or unexported fields
}
GRPCClient is a production-grade wrapper over grpc.ClientConn.
func (*GRPCClient) Close ¶
func (c *GRPCClient) Close() error
Close closes the gRPC client connection.
func (*GRPCClient) Conn ¶
func (c *GRPCClient) Conn() *grpc.ClientConn
Conn returns the underlying gRPC connection.
func (*GRPCClient) HealthCheck ¶
func (c *GRPCClient) HealthCheck(ctx context.Context) error
HealthCheck checks if the gRPC server is healthy.