client

package
v0.0.0-...-5e9c659 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultRetry is the default check-for-retry function for retries
	DefaultRetry = RetryOnError
	// DefaultRetries is the default number of times a request is tried
	DefaultRetries = 1
	// DefaultRequestTimeout is the default request timeout
	DefaultRequestTimeout = time.Second * 20
	// DefaultPoolSize sets the connection pool size
	DefaultPoolSize = 100
	// DefaultPoolTTL sets the connection pool ttl
	DefaultPoolTTL = time.Minute
)

Functions

func Logger

func Logger() logger.ILogger

func RetryAlways

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

RetryAlways always retry on error

func RetryOnError

func RetryOnError(ctx context.Context, req IRequest, retryCount int, err error) (bool, error)

RetryOnError retries a request on a 500 or timeout error

Types

type CallOption

type CallOption func(*CallOptions)

CallOption used by Call or Stream

func WithAddress

func WithAddress(a ...string) CallOption

WithAddress sets the remote addresses to use rather than using service discovery

func WithContext

func WithContext(ctx context.Context) CallOption

func WithRequestTimeout

func WithRequestTimeout(d time.Duration) CallOption

WithRequestTimeout is a CallOption which overrides that which set in Options.CallOptions

type CallOptions

type CallOptions struct {
	SelectOptions []selector.SelectOption

	// Address of remote hosts
	Address []string
	// Backoff func
	//Backoff BackoffFunc
	// Check if retriable func
	Retry RetryFunc
	// Transport Dial Timeout
	DialTimeout time.Duration
	// Number of Call attempts
	Retries int
	// Request/Response timeout
	RequestTimeout time.Duration
	// Stream timeout for the stream
	StreamTimeout time.Duration
	// Use the services own auth token
	ServiceToken bool
	// Duration to cache the response for
	CacheExpiry time.Duration

	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

type Config

type Config struct {
	config.Config `field:"-"`
	Name          string `field:"-"` // config name/path in config file
	PrefixName    string `field:"-"` // config prefix name
	// Other options for implementations of the interface
	// can be stored in a context
	Logger      logger.ILogger         `field:"-"` // 保留:提供给扩展使用
	Context     context.Context        `field:"-"`
	Client      IClient                `field:"-"`
	Transport   transport.ITransport   `field:"-"`
	Registry    registry.IRegistry     `field:"-"`
	Selector    selector.ISelector     `field:"-"`
	TlsConfig   *tls.Config            `field:"-"` // TLSConfig for tcp and quic
	CallOptions CallOptions            `field:"-"` // Default Call Options
	DialOptions []transport.DialOption `field:"-"` // TODO 		// 提供实时变化sturct

	// Connection Pool
	PoolSize int
	PoolTtl  time.Duration
	Retries  int // Retries retries to send

	// Used to select codec
	ContentType string

	// Group is used to select the services in the same group. Services set group info in their meta.
	// If it is empty, clients will ignore group.
	Group string

	// BackupLatency is used for Failbackup mode. rpc will sends another request if the first response doesn't return in BackupLatency time.
	BackupLatency time.Duration

	// 传输序列类型
	Serialize     codec.SerializeType `field:"-"`
	SerializeType string              //codec.SerializeType
	CompressType  transport.CompressType

	Heartbeat         bool
	HeartbeatInterval time.Duration

	Ja3      transport.Ja3 `field:"-"`
	ProxyUrl string
	// http options
	UserAgent     string
	AllowRedirect bool

	// Debug mode
	PrintRequest    bool
	PrintRequestAll bool

	// Registry
	RegistryType string
	RegistryHost string

	// Selector
	SelectorStrategy string
	// contains filtered or unexported fields
}

func (*Config) Init

func (self *Config) Init(opts ...Option)

init options

func (*Config) Load

func (self *Config) Load() error

func (*Config) Save

func (self *Config) Save(immed ...bool) error

func (*Config) String

func (self *Config) String() string

type HttpClient

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

func NewHttpClient

func NewHttpClient(opts ...Option) (*HttpClient, error)

func (*HttpClient) Call

func (self *HttpClient) Call(request *httpRequest, opts ...CallOption) (*httpResponse, error)

阻塞请求

func (*HttpClient) Config

func (self *HttpClient) Config() *Config

func (*HttpClient) CookiesManager

func (self *HttpClient) CookiesManager() http.CookieJar

func (*HttpClient) Init

func (self *HttpClient) Init(opts ...Option) error

func (*HttpClient) NewRequest

func (self *HttpClient) NewRequest(method, url string, data interface{}, optinos ...RequestOption) (*httpRequest, error)

新建请求

func (*HttpClient) String

func (self *HttpClient) String() string

type HttpOption

type HttpOption func(*Config)

func AllowRedirect

func AllowRedirect() HttpOption

func WithCookiejar

func WithCookiejar(jar http.CookieJar) HttpOption

func WithUserAgent

func WithUserAgent(userAgent string) HttpOption

type HttpRequest

type HttpRequest = httpRequest

type IClient

type IClient interface {
	Init(...Option) error
	Config() *Config
}

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 Default

func Default(opts ...Option) IClient

func New

func New(opts ...Option) IClient

NewClient returns a new client

type IRequest

type IRequest interface {
	// The service to call
	Service() string
	// The action to take
	Method() string
	// The endpoint to invoke
	//Endpoint() string
	// The content type
	ContentType() string
	Header() header.Header
	// The unencoded request body
	Body() *body.TBody
	// Write to the encoded request writer. This is nil before a call is made
	//Codec() codec.Writer
	// 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 IResponse

type IResponse interface {
	Body() *body.TBody
	// Read the response
	//Codec() codec.Reader
	// read the header
	Header() header.Header
}

Response is the response received from a service

type Option

type Option func(*Config)

Option contains all options for creating clients.

func Debug

func Debug() Option

增加超时到60s

func WithConfigPrefixName

func WithConfigPrefixName(prefixName string) Option

修改Config.json的路径

func WithHost

func WithHost(adr ...string) Option

固定服务器列表

func WithHttpOptions

func WithHttpOptions(opts ...HttpOption) Option

func WithJa3

func WithJa3(ja3, userAgent string) Option

func WithPrintRequest

func WithPrintRequest(all ...bool) Option

打印请求信息

func WithProxyURL

func WithProxyURL(proxyURL string) Option

func WithRegistries

func WithRegistries(typ string, Host string) Option

func WithRegistry

func WithRegistry(r registry.IRegistry) Option

Registry to find nodes for a given service

func WithSerializeType

func WithSerializeType(st codec.SerializeType) Option

Codec to be used to encode/decode requests for a given content type

func WithTransport

func WithTransport(t transport.ITransport) Option

Transport to use for communication e.g http, rabbitmq, etc

func WithTransportOptions

func WithTransportOptions(opts ...transport.Option) Option

init transport

type RequestOption

type RequestOption func(*RequestOptions)

RequestOption used by NewRequest

func Encoded

func Encoded(on bool) RequestOption

func WithCodec

func WithCodec(c codec.SerializeType) RequestOption

func WithServiceName

func WithServiceName(name string) RequestOption

type RequestOptions

type RequestOptions struct {
	ContentType   string
	Stream        bool
	Codec         codec.ICodec
	Encoded       bool // 传入的数据已经是编码器过的
	SerializeType codec.SerializeType

	Service string // 为该请求指定微服务名称
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

type RetryFunc

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

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

type RpcClient

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

func NewRpcClient

func NewRpcClient(opts ...Option) *RpcClient

func (*RpcClient) Call

func (self *RpcClient) Call(request IRequest, opts ...CallOption) (IResponse, error)

阻塞请求

func (*RpcClient) Config

func (self *RpcClient) Config() *Config

func (*RpcClient) Init

func (self *RpcClient) Init(opts ...Option) error

func (*RpcClient) NewRequest

func (self *RpcClient) NewRequest(service, method string, request interface{}, optinos ...RequestOption) (*rpcRequest, error)

新建请求

func (*RpcClient) String

func (self *RpcClient) String() string

Jump to

Keyboard shortcuts

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