grpc

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 32 Imported by: 0

Documentation

Overview

Package grpc 提供 grpc-free 契约的断言辅助函数。

Package grpc provides the gRPC-based RPC client for the gorp framework. This file implements the RPCClient contract with connection pooling, service discovery integration, and governance middleware chain.

本包提供 gorp 框架基于 gRPC 的 RPC 客户端实现。 本文件实现 RPCClient 契约,包含连接池、服务发现集成和治理中间件链。

Package grpc provides service authentication interceptors for gRPC. This file implements client and server interceptors for service-to-service auth.

本包提供 gRPC 服务认证拦截器。 本文件实现服务间认证的客户端和服务端拦截器。

Package grpc provides gRPC metadata carrier for propagation. This file implements transportcontract.MetadataCarrier using gRPC metadata.MD.

本包提供 gRPC metadata carrier 用于传播。 本文件使用 gRPC metadata.MD 实现 transportcontract.MetadataCarrier。

Package grpc provides the gRPC-based RPC client and server provider for the gorp framework. This package implements the core gRPC transport layer with full governance capabilities including tracing, metadata propagation, service authentication, circuit breaker and retry.

Package grpc 提供 gorp 框架基于 gRPC 的 RPC 客户端和服务端 provider。 本包实现核心 gRPC 传输层,具备完整的治理能力, 包括 tracing、metadata 传播、服务认证、熔断器和重试。

Package grpc provides the gRPC-based RPC server for the gorp framework. This file implements the RPCServer contract with health checking, reflection, and middleware chain integration.

本包提供 gorp 框架基于 gRPC 的 RPC 服务端实现。 本文件实现 RPCServer 契约,包含健康检查、反射和中间件链集成。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustGRPCConn added in v0.2.0

func MustGRPCConn(v any) *grpc.ClientConn

MustGRPCConn 把契约层返回的 any 断言为 *grpc.ClientConn。

MustGRPCConn 将 any 断言为 *grpc.ClientConn。

func MustGRPCServer added in v0.2.0

func MustGRPCServer(v any) *grpc.Server

MustGRPCServer 把契约层返回的 any 断言为 *grpc.Server。 契约层为保持 grpc-free 用 any 暴露,proto-first 应用注册服务时用它转换。

MustGRPCServer 将 any 断言为 *grpc.Server。

Types

type Client

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

Client implements transportcontract.RPCClient using gRPC. It manages connection pooling, service discovery, metadata propagation, tracing, service authentication, circuit breaker and retry.

Connection Pool Behavior:

  • Connections are cached by address in a sync.Map with no upper limit.
  • Each unique service address creates one gRPC connection that is reused.
  • Connections are closed when Client.Close() is called.
  • For scenarios with many services, consider implementing a connection pool with LRU eviction or using a connection pool library.

Client 使用 gRPC 实现 transportcontract.RPCClient。 管理连接池、服务发现、metadata 传播、tracing、服务认证、熔断和重试。

连接池行为: - 连接按地址缓存在 sync.Map 中,无上限。 - 每个唯一服务地址创建一个 gRPC 连接并复用。 - 调用 Client.Close() 时关闭所有连接。 - 对于服务数量多的场景,建议实现带 LRU 淘汰的连接池或使用连接池库。

func NewClient

NewClient creates a new gRPC Client instance with full governance capabilities.

NewClient 创建具备完整治理能力的新 gRPC Client 实例。

func (*Client) Call

func (c *Client) Call(ctx context.Context, service, method string, req, resp any) error

Call invokes a gRPC method on the specified service with governance middleware chain. Implements transportcontract.RPCClient.Call.

Call 在指定服务上调用 gRPC 方法,应用治理中间件链。 实现 transportcontract.RPCClient.Call。

func (*Client) CallRaw

func (c *Client) CallRaw(ctx context.Context, service, method string, data []byte) ([]byte, error)

CallRaw is not supported for gRPC as it requires protobuf messages. Implements transportcontract.RPCClient.CallRaw.

CallRaw gRPC 不支持原始字节调用,需要 protobuf 消息。 实现 transportcontract.RPCClient.CallRaw。

func (*Client) Close

func (c *Client) Close() error

Close closes all pooled connections and marks the client as closed. Implements transportcontract.RPCClient.Close.

Close 关闭所有池化连接并标记客户端为已关闭。 实现 transportcontract.RPCClient.Close。

func (*Client) Conn

func (c *Client) Conn(ctx context.Context, service string) (any, error)

Conn returns the underlying gRPC connection for advanced usage. Allows direct access to gRPC client connection for streaming or custom invocations. 返回 any(实际为 *grpc.ClientConn),满足 grpc-free 契约;调用方用类型断言转换。

Conn 返回底层 gRPC 连接供高级使用。 允许直接访问 gRPC 客户端连接进行流式调用或自定义调用。

type Provider

type Provider struct{}

Provider is the gRPC RPC provider that registers client and server capabilities.

Provider 是 gRPC RPC provider,注册客户端和服务端能力。

func NewProvider

func NewProvider() *Provider

NewProvider creates a new gRPC provider instance.

NewProvider 创建新的 gRPC provider 实例。

func (*Provider) Boot

Boot is a no-op for this provider as all setup happens in Register.

Boot 是空操作,因为所有设置都在 Register 中完成。

func (*Provider) DependsOn

func (p *Provider) DependsOn() []string

DependsOn returns the keys this provider depends on. gRPC RPC depends on Config, Discovery, and Tracer.

DependsOn 返回该 provider 依赖的 key。 gRPC RPC 依赖 Config、Discovery 和 Tracer。

func (*Provider) IsDefer

func (p *Provider) IsDefer() bool

IsDefer returns true indicating this provider should be deferred during boot.

IsDefer 返回 true,表示此 provider 应在 boot 阶段延迟执行。

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider identifier "rpc.grpc".

Name 返回 provider 标识符 "rpc.grpc"。

func (*Provider) Provides

func (p *Provider) Provides() []string

Provides returns the list of keys this provider binds to the container. Keys include RPCClientKey, RPCServerKey, GRPCConnFactoryKey, and GRPCServerRegistrarKey.

Provides 返回此 provider 绑定到容器的键列表。 键包括 RPCClientKey、RPCServerKey、GRPCConnFactoryKey 和 GRPCServerRegistrarKey。

func (*Provider) Register

func (p *Provider) Register(c runtimecontract.Container) error

Register binds the gRPC client factory, RPC client, server registrar, and RPC server to the container with singleton lifecycle.

Register 将 gRPC client factory、RPC client、server registrar 和 RPC server 以 singleton 生命周期绑定到容器。

type Server

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

Server implements transportcontract.RPCServer using gRPC. It manages gRPC server lifecycle, health checking, reflection, and middleware chain integration.

Server 使用 gRPC 实现 transportcontract.RPCServer。 管理 gRPC 服务器生命周期、健康检查、反射和中间件链集成。

func NewServer

NewServer creates a new gRPC Server instance with container for middleware resolution.

NewServer 创建新的 gRPC Server 实例,使用容器解析中间件依赖。

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the server's listening address. Implements transportcontract.RPCServer.Addr.

Addr 返回服务器的监听地址。 实现 transportcontract.RPCServer.Addr。

func (*Server) GRPCServer

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

GRPCServer returns the underlying gRPC server with explicit type. Provides typed access for grpc-aware callers (bootstrap/host assembly).

GRPCServer 返回底层 gRPC 服务器(显式类型),供 grpc 相关装配使用。

func (*Server) Register

func (s *Server) Register(service string, handler any) error

Register is not supported by the gRPC backend and returns an explicit error. gRPC services must be registered through RegisterProto with a protobuf-generated registration function; a generic any handler cannot be mounted onto a grpc.Server. Returning nil here used to silently discard the handler while the caller believed registration succeeded.

Register 不被 gRPC 后端支持,返回显式错误。 gRPC 服务必须通过 RegisterProto 使用 protobuf 生成的注册函数注册; 泛型 any handler 无法挂载到 grpc.Server。此前返回 nil 会静默丢弃 handler,调用方却以为注册成功。

func (*Server) RegisterProto

func (s *Server) RegisterProto(register func(server any) error) error

RegisterProto registers a gRPC service using a registration function. Allows direct registration of protobuf-generated service implementations. register 回调收到的 any 实际为 *grpc.Server(契约 grpc-free,调用方断言)。

RegisterProto 使用注册函数注册 gRPC 服务。 允许直接注册 protobuf 生成的服务实现。

func (*Server) ServeError added in v0.2.0

func (s *Server) ServeError() <-chan error

ServeError 返回一个 channel:Serve 意外退出(非主动 Stop)时会向其 投递一个错误后关闭。供 lifecycle/守护逻辑探测"gRPC 服务静默死亡"。

func (*Server) Server

func (s *Server) Server() any

Server returns the underlying gRPC server instance. Lazy initializes the server if not already created. 返回 any(实际为 *grpc.Server),满足 grpc-free 契约。

Server 返回底层 gRPC 服务器实例。 如果尚未创建则延迟初始化。

func (*Server) Start

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

Start starts the gRPC server on the configured address. Implements transportcontract.RPCServer.Start.

Start 在配置地址启动 gRPC 服务器。 实现 transportcontract.RPCServer.Start。

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop gracefully stops the gRPC server. Implements transportcontract.RPCServer.Stop.

Stop 优雅停止 gRPC 服务器。 实现 transportcontract.RPCServer.Stop。

Jump to

Keyboard shortcuts

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