client

package
v0.0.0-...-0985de9 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotServer = errors.New("查找不到服务配置信息")

	ErrPoolSize = errors.New("连接池大小不能小于0个")

	ErrAddrNotExist   = errors.New("不存在服务地址")
	ErrPoolGetTimeout = errors.New("连接获取超时")

	ErrCreateConnHandleNotExit = errors.New("创建连接的处理方法不存在")

	ErrRequestTimeout = errors.New("请求超时")
)
View Source
var (
	// 默认连接池大小
	DefaultPoolSize = 5
	// 默认连接池生命周期
	DefaultPoolTTl = 10 * time.Minute
	// 默认连接超时时间
	DefaultConnTimeout = 3 * time.Second
	// 默认重试次数
	DefaultRequestTimeout = 3 * time.Second
	// 默认重试次数
	DefaultRetries = 1
	// 默认重试验证方法
	DefaultRetry = RetryAlways
)

Functions

func NewClient

func NewClient(opt ...DialOption) (client *rpcClient)

创建客户端

func RetryAlways

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

RetryAlways always retry on error

Types

type CallFunc

type CallFunc func(ctx context.Context, req Request, rsp interface{}, opts CallOptions) error

CallFunc represents the individual call func

type CallOption

type CallOption func(options *CallOptions)

func WithRequestTimeout

func WithRequestTimeout(timeout time.Duration) CallOption

全局设置 -- 请求超时

func WithRetries

func WithRetries(retries int) CallOption

重试次数 全局

func WithRetry

func WithRetry(fn RetryFunc) CallOption

重试次数 全局

func WrapCall

func WrapCall(cw ...CallWrapper) CallOption

type CallOptions

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

type CallWrapper

type CallWrapper func(CallFunc) CallFunc

CallWrapper is a low level wrapper for the CallFunc

type Conn

type Conn interface {
	// rpc调度服务方法
	Call(ctx context.Context, req Request, resp interface{}, callOption CallOptions) error
	// 关闭连接
	Close() error
	// 获取连接的创建时间
	Created() time.Time
	// 获取连接的地址
	Remote() string
	// 连接的异常记录
	Error() error
	// 连接id
	Id() int64
}

运用接口可以方便后续升级和维护

type CreateConnectHandle

type CreateConnectHandle func() (Conn, error)

定一个创建连接的方法

type DialOption

type DialOption interface {
	// contains filtered or unexported methods
}

func RequestTimeout

func RequestTimeout(timeout time.Duration) DialOption

全局设置 -- 请求超时

func Retries

func Retries(retries int) DialOption

重试次数 全局

func Retry

func Retry(fn RetryFunc) DialOption

重试次数 全局

func SetConnTimeout

func SetConnTimeout(timeout time.Duration) DialOption

设置超时

func SetPoolSize

func SetPoolSize(size int) DialOption

设置连接池大小

func SetPoolTTl

func SetPoolTTl(ttl time.Duration) DialOption

设置连接生命周期

func SetServer

func SetServer(name string, server *Server) DialOption

设置服务

func WithWrapCall

func WithWrapCall(cw ...CallWrapper) DialOption

type Message

type Message struct {
	Header http.Header
	Body   interface{}
}

type Pool

type Pool interface {
	// 获取连接
	Get(ctx context.Context) (Conn, error)
	// 释放连接
	Release(conn Conn)
	// 关闭连接
	Close()
}

type PoolOptions

type PoolOptions struct {
	Id   string
	Size int
	TTl  time.Duration
	CreateConnectHandle
}

连接池的配置信息

type Request

type Request interface {
	// 服务名
	Service() string
	// 请求方法
	Method() string
	// 请求主题,也就是参数
	Body() interface{}

	SetHeader(key string, value interface{})

	Header() http.Header
}

Request is the interface for a synchronous request used by Call or Stream

type RequestOption

type RequestOption func(optiones *requestOptiones)

type RetryFunc

type RetryFunc func(ctx context.Context, req Request, 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 interface {
	ConnRelease(serverName string, conn Conn)
	NewConnect(serverName string) (Conn, error)
	Call(ctx context.Context, req Request, resp interface{}, callOption ...CallOption) error
	NewRequest(serverName string, serverMethod string, req interface{}, opts ...RequestOption) Request
}

type Server

type Server struct {
	CartFile      string
	TlsServerName string
	Network       string
	Address       string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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