Documentation ¶
Overview ¶
Package client is tRPC-Go clientside implementation, including network transportation, resolving, routing etc.
Index ¶
- Variables
- func DefaultClientConfig() map[string]*BackendConfig
- func IsOptionsImmutable(ctx context.Context) bool
- func LoadClientConfig(path string, opts ...config.LoadOption) error
- func RegisterClientConfig(callee string, conf *BackendConfig) error
- func RegisterConfig(conf map[string]*BackendConfig) error
- func RegisterStreamFilter(name string, filter StreamFilter)
- func WithOptionsImmutable(ctx context.Context) context.Context
- type Attachment
- type BackendConfig
- type Client
- type ClientStream
- type ClientStreamDesc
- type Option
- func WithAttachment(attachment *Attachment) Option
- func WithBalancerName(balancerName string) Option
- func WithCalleeEnvName(envName string) Option
- func WithCalleeMetadata(key string, val string) Option
- func WithCalleeMethod(method string) Option
- func WithCalleeSetName(setName string) Option
- func WithCallerEnvName(envName string) Option
- func WithCallerMetadata(key string, val string) Option
- func WithCallerNamespace(s string) Option
- func WithCallerServiceName(s string) Option
- func WithCallerSetName(setName string) Option
- func WithCircuitBreakerName(name string) Option
- func WithClientStreamQueueSize(size int) Option
- func WithCompressType(t int) Option
- func WithConnectionMode(connMode transport.ConnectionMode) Option
- func WithCurrentCompressType(t int) Option
- func WithCurrentSerializationType(t int) Option
- func WithDialTimeout(dur time.Duration) Option
- func WithDisableConnectionPool() Option
- func WithDisableFilter() Option
- func WithDisableServiceRouter() Option
- func WithDiscoveryName(name string) Option
- func WithEnvKey(key string) Option
- func WithFilter(f filter.ClientFilter) Option
- func WithFilters(fs []filter.ClientFilter) Option
- func WithKey(key string) Option
- func WithLocalAddr(addr string) Option
- func WithMaxWindowSize(s uint32) Option
- func WithMetaData(key string, val []byte) Option
- func WithMultiplexed(enable bool) Option
- func WithMultiplexedPool(p multiplexed.Pool) Option
- func WithNamedFilter(name string, f filter.ClientFilter) Option
- func WithNamespace(namespace string) Option
- func WithNetwork(s string) Option
- func WithPassword(s string) Option
- func WithPool(pool connpool.Pool) Option
- func WithProtocol(s string) Option
- func WithRecvControl(rc RecvControl) Option
- func WithReplicas(r int) Option
- func WithReqHead(h interface{}) Option
- func WithRspHead(h interface{}) Option
- func WithSelectorNode(n *registry.Node) Option
- func WithSendControl(sc SendControl) Option
- func WithSendOnly() Option
- func WithSerializationType(t int) Option
- func WithServiceName(s string) Option
- func WithServiceRouterName(name string) Option
- func WithShouldErrReportToSelector(f func(error) bool) Option
- func WithStreamFilter(sf StreamFilter) Option
- func WithStreamFilters(sfs ...StreamFilter) Option
- func WithStreamTransport(st transport.ClientStreamTransport) Option
- func WithTLS(certFile, keyFile, caFile, serverName string) Option
- func WithTarget(t string) Option
- func WithTimeout(t time.Duration) Option
- func WithTransport(t transport.ClientTransport) Option
- type Options
- type RecvControl
- type SendControl
- type Stream
- type StreamFilter
- type StreamFilterChain
- type Streamer
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = New()
DefaultClient is the default global client. It's thread-safe.
var ( // DefaultSelectorFilterName is the default name of selector filter. // It can be modified if conflict exists. DefaultSelectorFilterName = "selector" )
var DefaultStream = NewStream()
DefaultStream is the default client Stream.
var New = func() Client {
return &client{}
}
New creates a client that uses default client transport.
var NewStream = func() Stream {
return &stream{}
}
NewStream is the function that returns a Stream.
Functions ¶
func DefaultClientConfig ¶
func DefaultClientConfig() map[string]*BackendConfig
DefaultClientConfig returns the default client config.
Note: if multiple client configs with same callee and different service name exist in trpc_go.yaml, this function will only return the last config for the same callee key.
func IsOptionsImmutable ¶
IsOptionsImmutable checks the ctx if options are immutable.
func LoadClientConfig ¶
func LoadClientConfig(path string, opts ...config.LoadOption) error
LoadClientConfig loads client config by path.
func RegisterClientConfig ¶
func RegisterClientConfig(callee string, conf *BackendConfig) error
RegisterClientConfig is called to replace backend config of single callee service by name.
func RegisterConfig ¶
func RegisterConfig(conf map[string]*BackendConfig) error
RegisterConfig is called to replace the global backend config, allowing updating backend config regularly.
func RegisterStreamFilter ¶
func RegisterStreamFilter(name string, filter StreamFilter)
RegisterStreamFilter registers a StreamFilter by name.
Types ¶
type Attachment ¶
type Attachment struct {
// contains filtered or unexported fields
}
Attachment stores the Attachment of tRPC requests/responses.
func NewAttachment ¶
func NewAttachment(request io.Reader) *Attachment
NewAttachment returns a new Attachment whose response Attachment is a NoopAttachment.
func (*Attachment) Response ¶
func (a *Attachment) Response() io.Reader
Response returns Response Attachment.
type BackendConfig ¶
type BackendConfig struct { // Callee is the name of the backend service. // The config file uses it as the key to set the parameters. // Usually, it is the proto name of the callee service defined in proto stub file, // and it is the same as ServiceName below. Callee string `yaml:"callee"` // Name of the backend service. ServiceName string `yaml:"name"` // Backend service name. EnvName string `yaml:"env_name"` // Env name of the callee. SetName string `yaml:"set_name"` // "Set" name of the callee. // DisableServiceRouter, despite its inherent inappropriate and vague nomenclature, // is an option for naming service that denotes the de-facto meaning of disabling // out-rule routing for the source service. DisableServiceRouter bool `yaml:"disable_servicerouter"` Namespace string `yaml:"namespace"` // Namespace of the callee: Production/Development. CalleeMetadata map[string]string `yaml:"callee_metadata"` // Set callee metadata. Target string `yaml:"target"` // Polaris by default, generally no need to configure this. Password string `yaml:"password"` // Password for authentication. // Naming service four swordsmen. // Discovery.List => ServiceRouter.Filter => Loadbalancer.Select => Circuitbreaker.Report Discovery string `yaml:"discovery"` // Discovery for the backend service. ServiceRouter string `yaml:"servicerouter"` // Service router for the backend service. Loadbalance string `yaml:"loadbalance"` // Load balancing algorithm. Circuitbreaker string `yaml:"circuitbreaker"` // Circuit breaker configuration. Network string `yaml:"network"` // Transport protocol type: tcp or udp. Timeout int `yaml:"timeout"` // Client timeout in milliseconds. Protocol string `yaml:"protocol"` // Business protocol type: trpc, http, http_no_protocol, etc. Transport string `yaml:"transport"` // Transport type. // Serialization type. Use a pointer to check if it has been set (0 means pb). Serialization *int `yaml:"serialization"` Compression int `yaml:"compression"` // Compression type. TLSKey string `yaml:"tls_key"` // Client TLS key. TLSCert string `yaml:"tls_cert"` // Client TLS certificate. // CA certificate used to validate the server cert when calling a TLS service (e.g., an HTTPS server). CACert string `yaml:"ca_cert"` // Server name used to validate the server (default: hostname) when calling an HTTPS server. TLSServerName string `yaml:"tls_server_name"` Filter []string `yaml:"filter"` // Filters for the backend service. StreamFilter []string `yaml:"stream_filter"` // Stream filters for the backend service. // Report any error to the selector if this value is true. ReportAnyErrToSelector bool `yaml:"report_any_err_to_selector"` }
BackendConfig defines the configuration needed to call the backend service. It's empty by default and can be replaced.
func Config ¶
func Config(callee string) *BackendConfig
Config returns BackendConfig by callee service name.
type Client ¶
type Client interface { // Invoke performs a unary RPC. Invoke(ctx context.Context, reqBody interface{}, rspBody interface{}, opt ...Option) error }
Client is the interface that initiates RPCs and sends request messages to a server.
type ClientStream ¶
type ClientStream interface { // RecvMsg receives messages. RecvMsg(m interface{}) error // SendMsg sends messages. SendMsg(m interface{}) error // CloseSend closes sender. // No more sending messages, // but it's still allowed to continue to receive messages. CloseSend() error // Context gets the Context. Context() context.Context }
ClientStream is the interface returned to users to call its methods.
type ClientStreamDesc ¶
type ClientStreamDesc struct { // StreamName is the name of the stream, corresponding to Method of unary RPC. StreamName string // ClientStreams indicates whether it's client streaming. ClientStreams bool // ServerStreams indicates whether it's server streaming. ServerStreams bool }
ClientStreamDesc is the client stream description.
type Option ¶
type Option func(*Options)
Option sets client options.
func WithAttachment ¶
func WithAttachment(attachment *Attachment) Option
WithAttachment returns an Option that sets attachment.
func WithBalancerName ¶
WithBalancerName returns an Option that sets load balancer by name.
func WithCalleeEnvName ¶
WithCalleeEnvName returns an Option that sets env name of the callee service.
func WithCalleeMetadata ¶
WithCalleeMetadata returns an Option that sets metadata of callee. It should not be used for env/set as specific methods are provided for env/set.
func WithCalleeMethod ¶
WithCalleeMethod returns an Option that sets callee method name.
func WithCalleeSetName ¶
WithCalleeSetName returns an Option that sets "Set" of the callee service.
func WithCallerEnvName ¶
WithCallerEnvName returns an Option that sets env name of the caller service itself.
func WithCallerMetadata ¶
WithCallerMetadata returns an Option that sets metadata of caller. It should not be used for env/set as specific methods are provided for env/set.
func WithCallerNamespace ¶
WithCallerNamespace returns an Option that sets namespace of the caller service itself.
func WithCallerServiceName ¶
WithCallerServiceName returns an Option that sets service name of the caller service itself.
func WithCallerSetName ¶
WithCallerSetName returns an Option that sets "Set" of the caller service itself.
func WithCircuitBreakerName ¶
WithCircuitBreakerName returns an Option that sets circuit breaker by name.
func WithClientStreamQueueSize ¶
WithClientStreamQueueSize returns an Option that sets the size of client stream's buffer queue, that is, max number of received messages to put into the channel.
func WithCompressType ¶
WithCompressType returns an Option that sets compression type of backend service. Generally, only WithCompressType will be used as WithCurrentCompressType is used for reverse proxy.
func WithConnectionMode ¶
func WithConnectionMode(connMode transport.ConnectionMode) Option
WithConnectionMode returns an Option that sets whether connection mode is connected. If connection mode is connected, udp will isolate packets from non-same path.
func WithCurrentCompressType ¶
WithCurrentCompressType returns an Option that sets compression type of caller itself. WithCompressType should be used to set compression type of backend service.
func WithCurrentSerializationType ¶
WithCurrentSerializationType returns an Option that sets serialization type of caller itself. WithSerializationType should be used to set serialization type of backend service.
func WithDialTimeout ¶
WithDialTimeout returns an Option that sets timeout.
func WithDisableConnectionPool ¶
func WithDisableConnectionPool() Option
WithDisableConnectionPool returns an Option that disables connection pool.
func WithDisableFilter ¶
func WithDisableFilter() Option
WithDisableFilter returns an Option that sets whether to disable filter. It's used when a plugin setup and need a client to send request but filters' initialization has not been done.
func WithDisableServiceRouter ¶
func WithDisableServiceRouter() Option
WithDisableServiceRouter returns an Option that disables service router.
func WithDiscoveryName ¶
WithDiscoveryName returns an Option that sets service discovery by name.
func WithEnvKey ¶
WithEnvKey returns an Option that sets env key.
func WithFilter ¶
func WithFilter(f filter.ClientFilter) Option
WithFilter returns an Option that appends client filter to client filter chain. ClientFilter processing could be before encoding or after decoding. Selector filter is built-in filter and is at the end of the client filter chain by default. It is also supported to set pos of selector filter through the yaml config file.
func WithFilters ¶
func WithFilters(fs []filter.ClientFilter) Option
WithFilters returns an Option that appends multiple client filters to the client filter chain.
func WithLocalAddr ¶
WithLocalAddr returns an Option that sets local addr. Randomly picking for multiple NICs.
for non-persistent conn, ip & port can be specified: client.WithLocalAddr("127.0.0.1:8080") for conn pool or multiplexed, only ip can be specified: client.WithLocalAddr("127.0.0.1:")
func WithMaxWindowSize ¶
WithMaxWindowSize returns an Option that sets max size of receive window. Client as the receiver will notify the sender of the window.
func WithMetaData ¶
WithMetaData returns an Option that sets transparent transmitted metadata.
func WithMultiplexed ¶
WithMultiplexed returns an Option that enables multiplexed. WithMultiplexedPool should be used for custom Multiplexed.
func WithMultiplexedPool ¶
func WithMultiplexedPool(p multiplexed.Pool) Option
WithMultiplexedPool returns an Option that sets multiplexed pool. Calling this method enables multiplexing.
func WithNamedFilter ¶
func WithNamedFilter(name string, f filter.ClientFilter) Option
WithNamedFilter returns an Option that adds named filter
func WithNamespace ¶
WithNamespace returns an Option that sets namespace of backend service: Production/Development.
func WithNetwork ¶
WithNetwork returns an Option that sets dial network: tcp/udp, tcp by default.
func WithPassword ¶
WithPassword returns an Option that sets dial password.
func WithProtocol ¶
WithProtocol returns an Option that sets protocol of backend service like trpc.
func WithRecvControl ¶
func WithRecvControl(rc RecvControl) Option
WithRecvControl returns an Option that sets recv control.
func WithReplicas ¶
WithReplicas returns an Option that sets node replicas of stateful routing.
func WithReqHead ¶
func WithReqHead(h interface{}) Option
WithReqHead returns an Option that sets req head. It's default to clone server req head from source request.
func WithRspHead ¶
func WithRspHead(h interface{}) Option
WithRspHead returns an Option that sets rsp head. Usually used for gateway service.
func WithSelectorNode ¶
WithSelectorNode returns an Option that records the selected node. It's usually used for debugging.
func WithSendControl ¶
func WithSendControl(sc SendControl) Option
WithSendControl returns an Option that sets send control.
func WithSendOnly ¶
func WithSendOnly() Option
WithSendOnly returns an Option that sets CallType SendOnly. Generally it's used for udp async sending.
func WithSerializationType ¶
WithSerializationType returns an Option that sets serialization type of backend service. Generally, only WithSerializationType will be used as WithCurrentSerializationType is used for reverse proxy.
func WithServiceName ¶
WithServiceName returns an Option that sets service name of backend service.
func WithServiceRouterName ¶
WithServiceRouterName returns an Option that sets service router by name.
func WithShouldErrReportToSelector ¶
WithShouldErrReportToSelector returns an Option that sets should err report to selector
func WithStreamFilter ¶
func WithStreamFilter(sf StreamFilter) Option
WithStreamFilter returns an Option that appends a client stream filter to the client stream filter chain.
func WithStreamFilters ¶
func WithStreamFilters(sfs ...StreamFilter) Option
WithStreamFilters returns an Option that appends multiple client stream filters to the client stream filter chain. StreamFilter processing could be before or after stream's establishing, before or after sending data, before or after receiving data.
func WithStreamTransport ¶
func WithStreamTransport(st transport.ClientStreamTransport) Option
WithStreamTransport returns an Option that sets client stream transport.
func WithTLS ¶
WithTLS returns an Option that sets client tls files. If caFile="none", no server cert validation. If caFile="root", local ca cert will be used to validate server. certFile is only used for mTLS or should be empty. serverName is used to validate the name of server. hostname by default for https.
func WithTarget ¶
WithTarget returns an Option that sets target address using URI scheme://endpoint. e.g. ip://ip_addr:port
func WithTimeout ¶
WithTimeout returns an Option that sets timeout.
func WithTransport ¶
func WithTransport(t transport.ClientTransport) Option
WithTransport returns an Option that sets client transport plugin.
type Options ¶
type Options struct { ServiceName string // Backend service name. CallerServiceName string // Service name of caller itself. CalleeMethod string // Callee method name, usually used for metrics. Timeout time.Duration // Timeout. // Target is address of backend service: name://endpoint, // also compatible with old addressing like ip://ip:port Target string Network string Protocol string CallType codec.RequestType // Type of request, referring to transport.RequestType. CallOptions []transport.RoundTripOption // Options for client transport to call server. Transport transport.ClientTransport EnableMultiplexed bool StreamTransport transport.ClientStreamTransport SelectOptions []selector.Option Selector selector.Selector DisableServiceRouter bool CurrentSerializationType int CurrentCompressType int SerializationType int CompressType int Codec codec.Codec MetaData codec.MetaData ClientStreamQueueSize int // Size of client stream's queue. Filters filter.ClientChain // Filter chain. FilterNames []string // The name of filters. DisableFilter bool // Whether to disable filter. ReqHead interface{} // Allow custom req head. RspHead interface{} // Allow custom rsp head. Node *onceNode // For getting node info. MaxWindowSize uint32 // Max size of stream receiver's window. SControl SendControl // Sender's flow control. RControl RecvControl // Receiver's flow control. StreamFilters StreamFilterChain // Stream filter chain. // contains filtered or unexported fields }
Options are clientside options.
func NewOptions ¶
func NewOptions() *Options
NewOptions creates a new Options with fields set to default value.
func OptionsFromContext ¶
OptionsFromContext returns options from context.
func (*Options) LoadNodeConfig ¶
LoadNodeConfig loads node config from config center.
type RecvControl ¶
RecvControl is the interface used for receiver's flow control.
type SendControl ¶
SendControl is the interface used for sender's flow control.
type Stream ¶
type Stream interface { // Send sends stream messages. Send(ctx context.Context, m interface{}) error // Recv receives stream messages. Recv(ctx context.Context) ([]byte, error) // Init initiates all stream related options. Init(ctx context.Context, opt ...Option) (*Options, error) // Invoke initiates the lower layer connection to build the stream. Invoke(ctx context.Context) error // Close closes the stream. Close(ctx context.Context) error }
Stream is the interface that performs streaming RPCs.
type StreamFilter ¶
type StreamFilter func(ctx context.Context, desc *ClientStreamDesc, streamer Streamer) (ClientStream, error)
StreamFilter is the client stream filter. StreamFilter processing happens before or after stream's establishing.
func GetStreamFilter ¶
func GetStreamFilter(name string) StreamFilter
GetStreamFilter returns a StreamFilter by name.
type StreamFilterChain ¶
type StreamFilterChain []StreamFilter
StreamFilterChain client stream filters chain.
func (StreamFilterChain) Filter ¶
func (c StreamFilterChain) Filter(ctx context.Context, desc *ClientStreamDesc, streamer Streamer) (ClientStream, error)
Filter implements StreamFilter for multi stream filters.
type Streamer ¶
type Streamer func(ctx context.Context, desc *ClientStreamDesc) (ClientStream, error)
Streamer is the wrapper filter function used to filter all methods of ClientStream.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package mockclient is a generated GoMock package.
|
Package mockclient is a generated GoMock package. |