Documentation
¶
Overview ¶
Package grpc provides client-side gRPC bootstrap helpers for the config module.
It wraps grpc.ClientConn setup so application code can create generated proto clients with consistent transport configuration, TLS or mTLS resolution, and outbound tracing.
The package is transport-oriented: it does not depend on a concrete generated client. Any generated gRPC client can be created through BuildClient.
When Connect creates the connection itself, it also attaches:
- TLS or mTLS transport credentials resolved from viper under client.grpc.*
- unary and stream interceptors that trace outbound requests through the logger package
Supported configuration keys:
- client.grpc.tls.enable
- client.grpc.tls.caFile
- client.grpc.tls.serverName
- client.grpc.tls.version
- client.grpc.tls.insecureSkipVerify
- client.grpc.mtls.enable
- client.grpc.mtls.certFile
- client.grpc.mtls.keyFile
You can also bypass viper-based transport resolution by calling SetTLSConfig with a prebuilt *tls.Config before Connect.
Tracing stores metadata, target, method name, status code, request payload, and response payload in formatter.Service entries, mirroring the HTTP client instrumentation used elsewhere in the module.
Basic usage:
package main
import (
"context"
"log"
clientgrpc "github.com/PointerByte/QuicksGo/config/client/grpc"
pb "github.com/PointerByte/QuicksGo/config/proto"
)
func main() {
cli := clientgrpc.NewIClient(nil)
cli.SetAddress("localhost:50051")
greeter, err := clientgrpc.BuildClient(cli, pb.NewGreeterClient)
if err != nil {
log.Fatal(err)
}
resp, err := greeter.SayHello(context.Background(), &pb.HelloRequest{Name: "Manuel"})
if err != nil {
log.Fatal(err)
}
log.Println(resp.GetMessage())
}
Package client_gRPC is a generated GoMock package.
Index ¶
- func BuildClient[T any](client IClient, build BuildClientFunc[T]) (T, error)
- func SetTLSConfig(config *tls.Config)
- type BuildClientFunc
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect() error
- func (c *Client) GetConn() *grpc.ClientConn
- func (c *Client) SetAddress(address string)
- func (c *Client) SetConn(conn *grpc.ClientConn)
- func (c *Client) SetContext(ctx context.Context)
- func (c *Client) SetDialOptions(opts ...grpc.DialOption)
- type IClient
- type MockIClient
- func (m *MockIClient) Close() error
- func (m *MockIClient) Connect() error
- func (m *MockIClient) EXPECT() *MockIClientMockRecorder
- func (m *MockIClient) GetConn() *grpc.ClientConn
- func (m *MockIClient) SetAddress(address string)
- func (m *MockIClient) SetConn(conn *grpc.ClientConn)
- func (m *MockIClient) SetContext(ctx context.Context)
- func (m *MockIClient) SetDialOptions(opts ...grpc.DialOption)
- type MockIClientMockRecorder
- func (mr *MockIClientMockRecorder) Close() *gomock.Call
- func (mr *MockIClientMockRecorder) Connect() *gomock.Call
- func (mr *MockIClientMockRecorder) GetConn() *gomock.Call
- func (mr *MockIClientMockRecorder) SetAddress(address interface{}) *gomock.Call
- func (mr *MockIClientMockRecorder) SetConn(conn interface{}) *gomock.Call
- func (mr *MockIClientMockRecorder) SetContext(ctx interface{}) *gomock.Call
- func (mr *MockIClientMockRecorder) SetDialOptions(opts ...interface{}) *gomock.Call
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildClient ¶
func BuildClient[T any](client IClient, build BuildClientFunc[T]) (T, error)
BuildClient creates any generated proto client from the current connection.
func SetTLSConfig ¶
SetTLSConfig sets the TLS configuration that Connect should use when it has to create a new grpc.ClientConn and no explicit transport credentials were provided through SetDialOptions.
Types ¶
type BuildClientFunc ¶
type BuildClientFunc[T any] func(grpc.ClientConnInterface) T
BuildClientFunc creates a generated proto client from a grpc connection. Example:
greeter, err := client_gRPC.BuildClient(cli, proto.NewGreeterClient)
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) GetConn ¶
func (c *Client) GetConn() *grpc.ClientConn
func (*Client) SetAddress ¶
func (*Client) SetConn ¶
func (c *Client) SetConn(conn *grpc.ClientConn)
func (*Client) SetContext ¶
func (*Client) SetDialOptions ¶
func (c *Client) SetDialOptions(opts ...grpc.DialOption)
type IClient ¶
type IClient interface {
SetAddress(address string)
SetConn(conn *grpc.ClientConn)
SetContext(ctx context.Context)
SetDialOptions(opts ...grpc.DialOption)
Connect() error
Close() error
GetConn() *grpc.ClientConn
}
IClient defines the transport operations required to configure and manage a gRPC client connection.
It is protocol-agnostic: any client generated in the proto package can be created from the stored connection.
func NewIClient ¶
func NewIClient(conn *grpc.ClientConn) IClient
NewIClient creates a new gRPC client wrapper.
If conn is nil, Connect will create one from the configured address. If no dial options are set, the package resolves TLS/mTLS from configuration and falls back to insecure transport credentials when TLS is disabled.
type MockIClient ¶
type MockIClient struct {
// contains filtered or unexported fields
}
MockIClient is a mock of IClient interface.
func NewMockIClient ¶
func NewMockIClient(ctrl *gomock.Controller) *MockIClient
NewMockIClient creates a new mock instance.
func (*MockIClient) EXPECT ¶
func (m *MockIClient) EXPECT() *MockIClientMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockIClient) GetConn ¶
func (m *MockIClient) GetConn() *grpc.ClientConn
GetConn mocks base method.
func (*MockIClient) SetAddress ¶
func (m *MockIClient) SetAddress(address string)
SetAddress mocks base method.
func (*MockIClient) SetConn ¶
func (m *MockIClient) SetConn(conn *grpc.ClientConn)
SetConn mocks base method.
func (*MockIClient) SetContext ¶
func (m *MockIClient) SetContext(ctx context.Context)
SetContext mocks base method.
func (*MockIClient) SetDialOptions ¶
func (m *MockIClient) SetDialOptions(opts ...grpc.DialOption)
SetDialOptions mocks base method.
type MockIClientMockRecorder ¶
type MockIClientMockRecorder struct {
// contains filtered or unexported fields
}
MockIClientMockRecorder is the mock recorder for MockIClient.
func (*MockIClientMockRecorder) Close ¶
func (mr *MockIClientMockRecorder) Close() *gomock.Call
Close indicates an expected call of Close.
func (*MockIClientMockRecorder) Connect ¶
func (mr *MockIClientMockRecorder) Connect() *gomock.Call
Connect indicates an expected call of Connect.
func (*MockIClientMockRecorder) GetConn ¶
func (mr *MockIClientMockRecorder) GetConn() *gomock.Call
GetConn indicates an expected call of GetConn.
func (*MockIClientMockRecorder) SetAddress ¶
func (mr *MockIClientMockRecorder) SetAddress(address interface{}) *gomock.Call
SetAddress indicates an expected call of SetAddress.
func (*MockIClientMockRecorder) SetConn ¶
func (mr *MockIClientMockRecorder) SetConn(conn interface{}) *gomock.Call
SetConn indicates an expected call of SetConn.
func (*MockIClientMockRecorder) SetContext ¶
func (mr *MockIClientMockRecorder) SetContext(ctx interface{}) *gomock.Call
SetContext indicates an expected call of SetContext.
func (*MockIClientMockRecorder) SetDialOptions ¶
func (mr *MockIClientMockRecorder) SetDialOptions(opts ...interface{}) *gomock.Call
SetDialOptions indicates an expected call of SetDialOptions.