grpc

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

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/forge-go/config/client/grpc"
	pb "github.com/PointerByte/forge-go/config/proto"
	"google.golang.org/grpc/metadata"
)

func main() {
	cli := clientgrpc.NewIClient(nil)
	cli.SetAddress("localhost:50051")

	greeter, err := clientgrpc.BuildClient(cli, pb.NewGreeterClient)
	if err != nil {
		log.Fatal(err)
	}

	ctx := metadata.AppendToOutgoingContext(
		context.Background(),
		"authorization", "Bearer <JWT>",
	)

	resp, err := greeter.SayHello(ctx, &pb.HelloRequest{Name: "Manuel"})
	if err != nil {
		log.Fatal(err)
	}

	log.Println(resp.GetMessage())
}

The same metadata pattern applies to streams: pass the outgoing metadata context when creating the generated client stream.

Index

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

func SetTLSConfig(config *tls.Config)

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) Close

func (c *Client) Close() error

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) GetConn

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

func (*Client) SetAddress

func (c *Client) SetAddress(address string)

func (*Client) SetConn

func (c *Client) SetConn(conn *grpc.ClientConn)

func (*Client) SetContext

func (c *Client) SetContext(ctx context.Context)

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.

Jump to

Keyboard shortcuts

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