Documentation
¶
Overview ¶
Package grpcwrapper provides a production-ready gRPC client and server wrapper with built-in connection pooling, retry policies, and TLS support.
Package grpcwrapper provides a production-ready gRPC client and server wrapper with built-in connection pooling, retry policies, and TLS support.
Package grpcwrapper provides a production-ready gRPC client and server wrapper with built-in connection pooling, retry policies, and TLS support.
Index ¶
Constants ¶
const ( // DefaultServiceMaxMsgSize sets the limit for gRPC message transfer to 64MB. // This is crucial for components like the Scraper that pull large amounts of traffic data. DefaultServiceMaxMsgSize = 1024 * 1024 * 64 // DefaultClientRetryPolicy defines the standard retry behavior using gRPC's ServiceConfig. // It implements exponential backoff and retries on UNAVAILABLE/INTERNAL errors, // providing "transparent retries" for the business layer. DefaultClientRetryPolicy = `` /* 395-byte string literal not displayed */ )
const (
// DefaultServerMaxMsgSize sets the limit for gRPC message transfer to 128MB for incoming/outgoing server messages.
DefaultServerMaxMsgSize = 1024 * 1024 * 128
)
Variables ¶
This section is empty.
Functions ¶
func GetClient ¶
func GetClient[T any](wrapper *RpcConnWrapper, addr string, factory ClientFactory[T], opts ...DialOption) (T, bool)
GetClient retrieves a cached connection for the given address and creates a specific gRPC client. It uses the provided factory function (generated by protoc-gen-go-grpc).
func NewGrpcServer ¶
func NewGrpcServer(unaryInterceptors []grpc.UnaryServerInterceptor, streamInterceptors []grpc.StreamServerInterceptor) *grpc.Server
NewGrpcServer creates a new gRPC server with unified production settings. Features:
- Max message sizes: Standardized at 128MB across all services.
- Keepalive Enforcement: Prevents client connections from becoming stale.
- Interceptors: Allows for easy injection of logging, metrics, etc.
Types ¶
type ClientFactory ¶
type ClientFactory[T any] func(cc grpc.ClientConnInterface) T
ClientFactory is a generic function type that wraps the gRPC client constructor. E.g., proto.NewControlPlaneServiceClient
type DialOption ¶
type DialOption func(*dialOptions)
func WithInsecure ¶
func WithInsecure() DialOption
WithInsecure enables TLS but skips certificate verification. Useful for self-signed certificates or development environments.
func WithMaxMsgSize ¶
func WithMaxMsgSize(s int) DialOption
WithMaxMsgSize sets the maximum message size for sending and receiving.
func WithRetryPolicy ¶
func WithRetryPolicy(p string) DialOption
WithRetryPolicy sets the custom retry policy in JSON format.
func WithStreamInterceptors ¶
func WithStreamInterceptors(i ...grpc.StreamClientInterceptor) DialOption
WithStreamInterceptors adds stream interceptors to the client.
func WithTLS ¶
func WithTLS() DialOption
WithTLS enables standard TLS for the connection (verifies server certificates).
func WithUnaryInterceptors ¶
func WithUnaryInterceptors(i ...grpc.UnaryClientInterceptor) DialOption
WithUnaryInterceptors adds unary interceptors to the client.
type RpcConnWrapper ¶
type RpcConnWrapper struct {
// contains filtered or unexported fields
}
RpcConnWrapper provides a thread-safe singleton pool for gRPC connections. It ensures that only one physical TCP connection is established per destination address.
func NewRpcConnWrapper ¶
func NewRpcConnWrapper() *RpcConnWrapper
NewRpcConnWrapper returns the singleton instance of RpcConnWrapper.
func (*RpcConnWrapper) Close ¶
func (g *RpcConnWrapper) Close()
Close closes all cached connections and clears the pool.
func (*RpcConnWrapper) DeleteConn ¶
func (g *RpcConnWrapper) DeleteConn(serverAddr string) *grpc.ClientConn
DeleteConn removes and returns a specific connection from the cache.
func (*RpcConnWrapper) GetConn ¶
func (g *RpcConnWrapper) GetConn(serverAddr string, opts ...DialOption) (*grpc.ClientConn, bool)
GetConn lazily creates or retrieves a cached gRPC connection for the given address. The 'serverAddr' parameter is the target host:port string which serves as the cache key.