Documentation
¶
Overview ¶
Package client is an interface for an RPC client
Index ¶
- Variables
- func AuthToken(t string) options.Option
- func Backoff(fn BackoffFunc) options.Option
- func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, error)
- func ContextDialer(fn func(context.Context, string) (net.Conn, error)) options.Option
- func DialTimeout(td time.Duration) options.Option
- func Lookup(fn LookupFunc) options.Option
- func LookupRoute(_ context.Context, req Request, opts CallOptions) ([]string, error)
- func Network(n string) options.Option
- func NewContext(ctx context.Context, c Client) context.Context
- func PoolSize(d int) options.Option
- func PoolTTL(td time.Duration) options.Option
- func Proxy(addr string) options.Option
- func RequestMetadata(md metadata.Metadata) options.Option
- func RequestTimeout(td time.Duration) options.Option
- func ResponseMetadata(md *metadata.Metadata) options.Option
- func Retries(n int) options.Option
- func Retry(fn RetryFunc) options.Option
- func RetryAlways(ctx context.Context, req Request, retryCount int, err error) (bool, error)
- func RetryNever(ctx context.Context, req Request, retryCount int, err error) (bool, error)
- func RetryOnError(_ context.Context, _ Request, _ int, err error) (bool, error)
- func Selector(s selector.Selector) options.Option
- func StreamTimeout(td time.Duration) options.Option
- func StreamingRequest(b bool) options.Option
- func WithCallWrapper(fn CallWrapper) options.Option
- type BackoffFunc
- type CallFunc
- type CallOptions
- type CallWrapper
- type Client
- type LookupFunc
- type Options
- type Request
- type RequestOptions
- type Response
- type RetryFunc
- type Stream
- type StreamWrapper
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultClient is the global default client DefaultClient = NewClient() // DefaultContentType is the default content-type if not specified DefaultContentType = "" // DefaultBackoff is the default backoff function for retries (minimum 10 millisecond and maximum 5 second) DefaultBackoff = BackoffInterval(10*time.Millisecond, 5*time.Second) // DefaultRetry is the default check-for-retry function for retries DefaultRetry = RetryNever // DefaultRetries is the default number of times a request is tried DefaultRetries = 0 // DefaultRequestTimeout is the default request timeout DefaultRequestTimeout = time.Second * 5 // DefaultDialTimeout the default dial timeout DefaultDialTimeout = time.Second * 5 // DefaultPoolSize sets the connection pool size DefaultPoolSize = 100 // DefaultPoolTTL sets the connection pool ttl DefaultPoolTTL = time.Minute )
var DefaultCodecs = map[string]codec.Codec{ "application/octet-stream": codec.NewCodec(), }
DefaultCodecs will be used to encode/decode data
Functions ¶
func AuthToken ¶ added in v4.0.6
AuthToken is a CallOption which overrides the authorization header with the services own auth token
func Backoff ¶
func Backoff(fn BackoffFunc) options.Option
Backoff is used to set the backoff function used when retrying Calls
func BackoffExp ¶
BackoffExp using exponential backoff func
func ContextDialer ¶
ContextDialer pass ContextDialer to client
func DialTimeout ¶
DialTimeout sets the dial timeout
func Lookup ¶
func Lookup(fn LookupFunc) options.Option
Lookup sets the lookup function to use for resolving service names
func LookupRoute ¶
LookupRoute for a request using the router and then choose one using the selector
func NewContext ¶
NewContext put client in context
func RequestMetadata ¶ added in v4.0.6
WithRequestMetadata is a CallOption which adds metadata.Metadata to Options.CallOptions
func RequestTimeout ¶
RequestTimeout is the request timeout.
func ResponseMetadata ¶ added in v4.0.6
WithResponseMetadata is a CallOption which adds metadata.Metadata to Options.CallOptions
func RetryAlways ¶
RetryAlways always retry on error
func RetryNever ¶
RetryNever never retry on error
func RetryOnError ¶
RetryOnError retries a request on a 500 or 408 (timeout) error
func StreamTimeout ¶
StreamTimeout sets the stream timeout
func StreamingRequest ¶
StreamingRequest specifies that request is streaming
func WithCallWrapper ¶
func WithCallWrapper(fn CallWrapper) options.Option
WithCallWrapper sets the retry function to be used when re-trying.
Types ¶
type BackoffFunc ¶
BackoffFunc is the backoff call func
func BackoffInterval ¶
func BackoffInterval(min time.Duration, max time.Duration) BackoffFunc
BackoffInterval specifies randomization interval for backoff func
type CallFunc ¶
type CallFunc func(ctx context.Context, addr string, req Request, rsp interface{}, opts CallOptions) error
CallFunc represents the individual call func
type CallOptions ¶
type CallOptions struct { // Selector selects addr Selector selector.Selector // Context used for deadline Context context.Context // Router used for route Router router.Router // Retry func used for retries Retry RetryFunc // Backoff func used for backoff when retry Backoff BackoffFunc // Network name Network string // Content-Type ContentType string // AuthToken string AuthToken string // Address specifies static addr list Address []string // SelectOptions selector options SelectOptions []selector.SelectOption // CallWrappers call wrappers CallWrappers []CallWrapper // StreamTimeout stream timeout StreamTimeout time.Duration // RequestTimeout request timeout RequestTimeout time.Duration // RequestMetadata holds additional metadata for call RequestMetadata metadata.Metadata // ResponseMetadata holds additional metadata from call ResponseMetadata *metadata.Metadata // DialTimeout dial timeout DialTimeout time.Duration // Retries specifies retries num Retries int // ContextDialer used to connect ContextDialer func(context.Context, string) (net.Conn, error) }
CallOptions holds client call options
func NewCallOptions ¶
func NewCallOptions(opts ...options.Option) CallOptions
NewCallOptions creates new call options struct
type CallWrapper ¶
CallWrapper is a low level wrapper for the CallFunc
type Client ¶
type Client interface { Name() string Init(opts ...options.Option) error Options() Options NewRequest(service string, endpoint string, req interface{}, opts ...options.Option) Request Call(ctx context.Context, req Request, rsp interface{}, opts ...options.Option) error Stream(ctx context.Context, req Request, opts ...options.Option) (Stream, error) String() string }
Client is the interface used to make requests to services. It supports Request/Response via Transport and Publishing via the Broker. It also supports bidirectional streaming of requests.
func FromContext ¶
FromContext get client from context
type LookupFunc ¶
LookupFunc is used to lookup routes for a service
type Options ¶
type Options struct { // Selector used to select needed address Selector selector.Selector // Logger used to log messages Logger logger.Logger // Tracer used for tracing Tracer tracer.Tracer // Meter used for metrics Meter meter.Meter // Context is used for external options Context context.Context // Router used to get route Router router.Router // TLSConfig specifies tls.Config for secure connection TLSConfig *tls.Config // Codecs map Codecs map[string]codec.Codec // Lookup func used to get destination addr Lookup LookupFunc // Proxy is used for proxy requests Proxy string // ContentType is used to select codec ContentType string // Name is the client name Name string // CallOptions contains default CallOptions CallOptions CallOptions // PoolSize connection pool size PoolSize int // PoolTTL connection pool ttl PoolTTL time.Duration // ContextDialer used to connect ContextDialer func(context.Context, string) (net.Conn, error) // Hooks may contains Client func wrapper Hooks options.Hooks }
Options holds client options
func NewOptions ¶
NewOptions creates new options struct
type Request ¶
type Request interface { // The service to call Service() string // The action to take Method() string // The endpoint to invoke Endpoint() string // The content type ContentType() string // The unencoded request body Body() interface{} // Write to the encoded request writer. This is nil before a call is made Codec() codec.Codec // indicates whether the request will be a streaming one rather than unary Stream() bool }
Request is the interface for a synchronous request used by Call or Stream
type RequestOptions ¶
type RequestOptions struct { // Context used for external options Context context.Context // ContentType specify content-type of message ContentType string // Stream flag Stream bool }
RequestOptions holds client request options
func NewRequestOptions ¶
func NewRequestOptions(opts ...options.Option) RequestOptions
NewRequestOptions creates new RequestOptions struct
type Response ¶
type Response interface { // Read the response Codec() codec.Codec // The content type // ContentType() string // Header data // Header() metadata.Metadata // Read the undecoded response Read() ([]byte, error) }
Response is the response received from a service
type RetryFunc ¶
RetryFunc that returning either false or a non-nil error will result in the call not being retried
func RetryOnErrors ¶
RetryOnErrors retries a request on specified error codes
type Stream ¶
type Stream interface { // Context for the stream Context() context.Context // The request made Request() Request // The response read Response() Response // Send will encode and send a request Send(msg interface{}) error // Recv will decode and read a response Recv(msg interface{}) error // SendMsg will encode and send a request SendMsg(msg interface{}) error // RecvMsg will decode and read a response RecvMsg(msg interface{}) error // Error returns the stream error Error() error // Close closes the stream Close() error // CloseSend closes the send direction of the stream CloseSend() error }
Stream is the interface for a bidirectional synchronous stream
type StreamWrapper ¶
StreamWrapper wraps a Stream and returns the equivalent