client

package
v4.0.20 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 19 Imported by: 5

Documentation

Overview

Package client is an interface for an RPC client

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
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

func AuthToken(t string) options.Option

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

func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, error)

BackoffExp using exponential backoff func

func ContextDialer

func ContextDialer(fn func(context.Context, string) (net.Conn, error)) options.Option

ContextDialer pass ContextDialer to client

func DialTimeout

func DialTimeout(td time.Duration) options.Option

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

func LookupRoute(_ context.Context, req Request, opts CallOptions) ([]string, error)

LookupRoute for a request using the router and then choose one using the selector

func Network added in v4.0.6

func Network(n string) options.Option

Network is a CallOption which sets the network attribute

func NewContext

func NewContext(ctx context.Context, c Client) context.Context

NewContext put client in context

func PoolSize

func PoolSize(d int) options.Option

PoolSize sets the connection pool size

func PoolTTL

func PoolTTL(td time.Duration) options.Option

PoolTTL sets the connection pool ttl

func Proxy

func Proxy(addr string) options.Option

Proxy sets the proxy address

func RequestMetadata added in v4.0.6

func RequestMetadata(md metadata.Metadata) options.Option

WithRequestMetadata is a CallOption which adds metadata.Metadata to Options.CallOptions

func RequestTimeout

func RequestTimeout(td time.Duration) options.Option

RequestTimeout is the request timeout.

func ResponseMetadata added in v4.0.6

func ResponseMetadata(md *metadata.Metadata) options.Option

WithResponseMetadata is a CallOption which adds metadata.Metadata to Options.CallOptions

func Retries

func Retries(n int) options.Option

Retries sets the retry count when making the request.

func Retry

func Retry(fn RetryFunc) options.Option

Retry sets the retry function to be used when re-trying.

func RetryAlways

func RetryAlways(ctx context.Context, req Request, retryCount int, err error) (bool, error)

RetryAlways always retry on error

func RetryNever

func RetryNever(ctx context.Context, req Request, retryCount int, err error) (bool, error)

RetryNever never retry on error

func RetryOnError

func RetryOnError(_ context.Context, _ Request, _ int, err error) (bool, error)

RetryOnError retries a request on a 500 or 408 (timeout) error

func Selector

func Selector(s selector.Selector) options.Option

Selector is used to select a route

func StreamTimeout

func StreamTimeout(td time.Duration) options.Option

StreamTimeout sets the stream timeout

func StreamingRequest

func StreamingRequest(b bool) options.Option

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

type BackoffFunc func(ctx context.Context, req Request, attempts int) (time.Duration, error)

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

type CallWrapper func(CallFunc) CallFunc

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

func FromContext(ctx context.Context) (Client, bool)

FromContext get client from context

func NewClient

func NewClient(opts ...options.Option) Client

NewClient returns new noop client

func NewClientCallOptions

func NewClientCallOptions(c Client, opts ...options.Option) Client

NewClientCallOptions add CallOption to every call

type LookupFunc

type LookupFunc func(context.Context, Request, CallOptions) ([]string, error)

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

func NewOptions(opts ...options.Option) Options

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

type RetryFunc func(ctx context.Context, req Request, retryCount int, err error) (bool, error)

RetryFunc that returning either false or a non-nil error will result in the call not being retried

func RetryOnErrors

func RetryOnErrors(codes ...int32) RetryFunc

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

type StreamWrapper func(Stream) Stream

StreamWrapper wraps a Stream and returns the equivalent

type Wrapper

type Wrapper func(Client) Client

Wrapper wraps a client and returns a client

Jump to

Keyboard shortcuts

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