grpc

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: Apache-2.0, MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// ClientVersionMetadataKey is the metadata key for the client version
	ClientVersionMetadataKey = "x-client-version"

	// ClientTypeMetadataKey is the metadata key for the client type
	ClientTypeMetadataKey = "x-client-type"

	// ClientTypeMetadataValue is the value for the client type
	ClientTypeMetadataValue = "aggkit"
)

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

func VersionHeaderInterceptor added in v0.7.0

func VersionHeaderInterceptor() grpc.UnaryClientInterceptor

VersionHeaderInterceptor adds the client version and type headers to all outgoing requests

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.

func (ClientConfig) WithURL added in v0.7.0

func (c ClientConfig) WithURL(url string) ClientConfig

WithURL returns a copy of the current ClientConfig with the URL field set to the given value. This method does not modify the original ClientConfig.

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 Server added in v0.7.0

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

Server encapsulates a gRPC server instance, its network listener, and the address it listens on. It provides the necessary fields to manage the lifecycle and configuration of a gRPC server.

func NewServer added in v0.7.0

func NewServer(cfg ServerConfig, opts ...grpc.ServerOption) (*Server, error)

NewServer creates and returns a new gRPC Server instance listening on the specified address. It accepts an address string, a boolean to enable server reflection, and optional gRPC server options. If enableReflection is true, the server will register the reflection service for introspection. Returns a pointer to the Server and an error if the listener cannot be created.

func (*Server) Addr added in v0.7.0

func (s *Server) Addr() string

Addr returns the address on which the server is listening.

func (*Server) GRPC added in v0.7.0

func (s *Server) GRPC() *grpc.Server

GRPC returns the underlying gRPC server instance.

func (*Server) Start added in v0.7.0

func (s *Server) Start(ctx context.Context)

Start launches the gRPC server and begins serving incoming connections. It blocks until the provided context is cancelled, at which point it gracefully stops the server. If the server fails to start, an error is logged.

type ServerConfig added in v0.7.0

type ServerConfig struct {
	// Host is the address to bind the gRPC server
	Host string `mapstructure:"Host"`
	// Port is the port to bind the gRPC server
	Port int `mapstructure:"Port"`
	// EnableReflection indicates whether gRPC server reflection is enabled
	// This allows clients to introspect the server's services and methods.
	EnableReflection bool `mapstructure:"EnableReflection"`
	// MaxDecodingMessageSize is the maximum size of a message that the server can receive.
	MaxDecodingMessageSize int `mapstructure:"MaxDecodingMessageSize"`
}

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