grpc

package
v0.5.0-beta4 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0, MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RepackGRPCErrorWithDetails

func RepackGRPCErrorWithDetails(err error) error

RepackGRPCErrorWithDetails extracts *status.Status and formats ErrorInfo details into a single error

Types

type Client

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

Client holds the gRPC connection and services

func NewClient

func NewClient(cfg *ClientConfig) (*Client, error)

NewClient initializes and returns a new gRPC client

func (*Client) Close

func (c *Client) Close() error

Close closes the gRPC connection

func (*Client) Conn

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

Conn returns the gRPC connection

type ClientConfig

type ClientConfig struct {
	// URL is the URL of the gRPC server
	URL string `mapstructure:"URL"`

	// MinConnectTimeout is the minimum time to wait for a connection to be established
	// This is used to prevent the client from hanging indefinitely if the server is unreachable.
	MinConnectTimeout types.Duration `mapstructure:"MinConnectTimeout"`

	// RequestTimeout is the timeout for individual requests
	RequestTimeout types.Duration `mapstructure:"RequestTimeout"`

	// UseTLS indicates whether to use TLS for the gRPC connection
	UseTLS bool `mapstructure:"UseTLS"`

	// Retry represents the retry configuration
	Retry *RetryConfig `mapstructure:"Retry"`
}

ClientConfig is the configuration for the gRPC client

func DefaultConfig

func DefaultConfig() *ClientConfig

DefaultConfig returns a default configuration for the gRPC client

func (*ClientConfig) String

func (c *ClientConfig) String() string

String returns a string representation of the gRPC client configuration

func (*ClientConfig) Validate

func (c *ClientConfig) Validate() error

Validate checks if the gRPC client configuration is valid. It returns an error if any of the required fields are missing or invalid.

type GRPCError

type GRPCError struct {
	Code    codes.Code
	Message string
	Details []string
}

GRPCError represents an error structure used in gRPC communication. It contains the error code, a descriptive message, and optional details providing additional context about the error.

func (GRPCError) Error

func (e GRPCError) Error() string

Error returns a formatted string representation of the GRPCError, including the error code, message, and details. The details are joined into a single string for readability.

func (GRPCError) Is

func (e GRPCError) Is(err error) bool

Is is an implementation of the error.Is interface for GRPCError. It checks if the provided error matches the GRPCError's code and message.

type Method

type Method struct {
	// ServiceName identifies gRPC service name (alongside package)
	ServiceName string `mapstructure:"Service"`

	// MethodName denotes gRPC function name
	MethodName string `mapstructure:"Method"` // optional
}

Method describes the gRPC service name and method name

type MethodConfig

type MethodConfig struct {
	// List of service/method pairs this config applies to.
	Name []MethodName `json:"name"`
	// Optional retry policy to apply to the specified methods.
	RetryPolicy *RetryPolicy `json:"retryPolicy"`
}

MethodConfig defines behavior overrides (e.g., retry policy) for specific RPC methods or services.

type MethodName

type MethodName struct {
	// Optional: Match all methods of a specific service if Method is empty.
	Service string `json:"service,omitempty"`
	// Optional: Match a specific method of the given service.
	Method string `json:"method,omitempty"`
}

MethodName identifies a gRPC method or service that the retry policy should apply to. Either Service or Method may be empty for wildcard matching.

type RetryConfig

type RetryConfig struct {
	// InitialBackoff is the initial delay before retrying a request
	InitialBackoff types.Duration `mapstructure:"InitialBackoff"`

	// MaxBackoff is the maximum backoff duration for retries
	MaxBackoff types.Duration `mapstructure:"MaxBackoff"`

	// BackoffMultiplier is the multiplier for the backoff duration
	BackoffMultiplier float64 `mapstructure:"BackoffMultiplier"`

	// MaxAttempts is the maximum number of retries for a request
	MaxAttempts int `mapstructure:"MaxAttempts"`

	// Excluded captures functions which are excluded from retry policies
	Excluded []Method `mapstructure:"Excluded"`
}

RetryConfig denotes the gRPC retry policy

func (*RetryConfig) String

func (r *RetryConfig) String() string

func (*RetryConfig) Validate

func (r *RetryConfig) Validate() error

Validate checks if the gRPC retry configuration is valid. It returns an error if any of the required fields are missing or invalid.

type RetryPolicy

type RetryPolicy struct {
	// Maximum number of attempts (initial + retries).
	MaxAttempts int `json:"maxAttempts"`
	// Delay before first retry (e.g., "0.1s").
	InitialBackoff string `json:"initialBackoff"`
	// Maximum delay between retries (e.g., "2s").
	MaxBackoff string `json:"maxBackoff"`
	// Multiplier for exponential backoff (e.g., 2.0).
	BackoffMultiplier float64 `json:"backoffMultiplier"`
	// gRPC status codes that are considered retryable (e.g., "UNAVAILABLE").
	RetryableStatusCodes []string `json:"retryableStatusCodes"`
}

RetryPolicy specifies how failed RPCs should be retried, including delays and allowed status codes.

type ServiceConfig

type ServiceConfig struct {
	// List of method-specific configurations.
	MethodConfig []MethodConfig `json:"methodConfig"`
}

ServiceConfig represents the top-level configuration for service method behaviors, including retry policies. It mirrors the gRPC service configuration format.

Jump to

Keyboard shortcuts

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