grpcclient

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package grpcclient demonstrates how to build a self-contained business client SDK on top of component-base's gRPC client SDK (pkg/client-sdk/grpc/v1). It is the client counterpart of examples/grpcserver and is structured like the task_scheduler client SDK in real projects (eir): a five-layer split whose transport owns dedicated encoder/decoder sub-packages and whose client owns the connection and a Config-driven constructor.

Layer architecture (each layer is one package):

Transport layer  transport/          -- builds one grpcclientv1.Client per RPC, owns *grpc.ClientConn,
                                          injects codecs from the encoder/ and decoder/ sub-packages
  request codecs transport/encoder/   -- Factory handing out grpcclientv1.Encoder per unary RPC
  response codecs transport/decoder/  -- Factory handing out Decoder / MsgExtractor / StreamDecoder
Endpoint layer   endpoint/           -- adapts Client into domain-typed grpcclientv1.Endpoint functions
Service layer    service/            -- business methods that assemble GRPCRequest and call endpoints
Client layer     client.go, config.go-- New/Config wire all layers, own the connection and expose Close

Assembly chain (see client.go):

transport.New -> endpoint.New -> service.New -> Client

Unlike the HTTP SDK, the gRPC SDK cannot marshal protobuf messages automatically, so codecs are injected explicitly through the transport's encoder/decoder sub-packages:

  • Encoder[REQ, ProtoReq] and Decoder[ProtoResp, RESP] for unary RPCs (see transport/encoder/demo.go, transport/decoder/demo.go);
  • StreamOpener[REQ], MsgExtractor[ProtoMsg] and StreamDecoder[RESP] for server-streaming RPCs. The conn-bound opener stays in transport/demo.go while the extractor/decoder come from the decoder factory. The SDK transport aggregates every streamed chunk, so the caller consumes Download as an ordinary unary response and never touches the stream directly.

Client-side middlewares are supported through the fluent builder: the unary Echo client composes a logging middleware via NewUnaryClientBuilder(...).Use(...).

Reusing this example in a business project

  1. Copy these directories (transport incl. encoder/decoder, endpoint, service, client.go, config.go) and rename the packages; regenerate your own pb code from your .proto files.
  2. Replace each transport RPC: point at your real service/method names and rewrite the encoders/decoders in the sub-packages (and the stream opener in the transport resource file) for your types.
  3. Keep the layer contracts and the transport.New -> endpoint.New -> service.New assembly chain unchanged; each layer only delegates below.
  4. Naming conventions: each layer entry point is a New returning XxxFactory; a resource transport exposes one method per RPC returning a Client.

Trying the client

The client dials and owns the connection, so it is closed via Close. Either dial directly:

client, err := grpcclient.New("127.0.0.1:8081", grpc.WithTransportCredentials(insecure.NewCredentials()))
defer client.Close()

or drive it from a Config:

client, err := grpcclient.NewConfig().Complete().NewClient()

then call the business methods:

resp, err := client.Demo().Echo("hello")   // -> "echo: hello"
data, err := client.Demo().Download("greeting")

Testing

The accompanying test boots examples/grpcserver on a free port and exercises the unary Echo and the aggregated Download round trips through this client SDK:

go test ./examples/grpcclient/...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	svcclientv1.Factory
	// contains filtered or unexported fields
}

Client is the example gRPC client. It embeds the service-layer Factory so callers use business methods directly (e.g. client.Demo().Echo("hi")), and it owns the underlying connection so Close can release it.

func New

func New(svraddr string, opts ...grpc.DialOption) (*Client, error)

New dials the given server address and returns a Client that owns the connection; the caller must call Close when finished.

func (*Client) Close

func (c *Client) Close() error

Close releases the gRPC connection owned by the client.

type CompletedConfig

type CompletedConfig struct {
	*Config
}

CompletedConfig is a Config that has had its defaults applied.

func (CompletedConfig) NewClient

func (c CompletedConfig) NewClient() (*Client, error)

NewClient dials the configured address and returns a Client. The returned client owns the connection; call Close when finished.

func (CompletedConfig) NewClientWithContext

func (c CompletedConfig) NewClientWithContext(ctx context.Context) (*Client, error)

NewClientWithContext is NewClient with an explicit root context for requests.

type Config

type Config struct {
	// Address is the target of the example gRPC server (host:port).
	Address string

	// DialOptions are passed to grpc.NewClient when dialing Address.
	DialOptions []grpc.DialOption
}

Config is the configuration for the example gRPC client. It mirrors the task_scheduler client SDK's config-driven construction, letting callers fill in the server address and dial options and then obtain a Client from a CompletedConfig.

func NewConfig

func NewConfig() *Config

NewConfig returns a Config defaulted to the example server's insecure address.

func (*Config) Complete

func (c *Config) Complete() CompletedConfig

Complete fills in unset fields with defaults and returns a CompletedConfig.

Directories

Path Synopsis
Package endpoint is the endpoint layer of the gRPC client SDK example.
Package endpoint is the endpoint layer of the gRPC client SDK example.
Package service is the service layer of the gRPC client SDK example.
Package service is the service layer of the gRPC client SDK example.
Package transport is the transport layer of the gRPC client SDK example.
Package transport is the transport layer of the gRPC client SDK example.
decoder
Package decoder is the response-decoder layer of the gRPC client SDK example's transport.
Package decoder is the response-decoder layer of the gRPC client SDK example's transport.
encoder
Package encoder is the request-encoder layer of the gRPC client SDK example's transport.
Package encoder is the request-encoder layer of the gRPC client SDK example's transport.

Jump to

Keyboard shortcuts

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