client

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: BSD-3-Clause Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeout
	DefaultTimeout = time.Second * 10
	// DefaultRetryTimes 如果请求失败,最多重试3次
	DefaultRetryTimes = 3
	// DefaultRetryDelay 在重试前,延迟等待100毫秒
	DefaultRetryDelay = time.Millisecond * 100
	RequestIDHeader   = "X-Request-Id"
)
View Source
const (
	KiB
	MiB
	GiB
	TiB

	// DialTimeout the timeout of create connection
	DialTimeout = 5 * time.Second

	// BackoffMaxDelay provided maximum delay when backing off after failed connection attempts.
	BackoffMaxDelay = 3 * time.Second

	// MaxSendMsgSize set max gRPC request message size sent to server.
	// If any request message size is larger than current value, an error will be reported from gRPC.
	MaxSendMsgSize = 4 << 30

	// MaxRecvMsgSize set max gRPC receive message size received from server.
	// If any message size is larger than current value, an error will be reported from gRPC.
	MaxRecvMsgSize = 4 << 30

	// InitialWindowSize we set it 1GB is to provide system's throughput.
	InitialWindowSize = 1 << 30

	// InitialConnWindowSize we set it 1GB is to provide system's throughput.
	InitialConnWindowSize = 1 << 30
)

Variables

View Source
var (
	ErrPoolClosed = errors.New("connection pool is closed")
	ErrNoConn     = errors.New("no available connections")
)

Functions

func BindMetadataForContext

func BindMetadataForContext(ctx context.Context, data map[string]string) context.Context

BindMetadataForContext

func NewClient

func NewClient(ctx context.Context, uri string, pbNewXxxClient interface{},
	opts ...ClientOptional) (interface{}, error)

NewClient 参数 bNewXxxClient 对应 pb.NewXxxClient 方法

func NewClientWithPool added in v0.4.0

func NewClientWithPool(ctx context.Context, pool *ConnPool, pbNewXxxClient interface{}) (interface{}, error)

NewClientWithPool 使用连接池创建客户端

Types

type ClientOptional

type ClientOptional func(o *ClientOptions)

ClientOptional

func WithConnectionPool added in v0.4.0

func WithConnectionPool(pool *ConnPool) ClientOptional

WithConnectionPool

func WithDialOptions added in v0.2.30

func WithDialOptions(options ...grpc.DialOption) ClientOptional

WithDialOptions

func WithLoadBalance

func WithLoadBalance(loadBalance string) ClientOptional

WithLoadBalance

func WithLogger

func WithLogger(logger *zap.Logger) ClientOptional

WithLogger

func WithMetrics

func WithMetrics(metrics bool) ClientOptional

WithMetrics

func WithResolver

func WithResolver(r resolver.Builder) ClientOptional

WithResolver

func WithSecure

func WithSecure(secure bool) ClientOptional

WithSecure

func WithTimeout

func WithTimeout(t time.Duration) ClientOptional

WithTimeout

func WithTracer

func WithTracer(tracer opentracing.Tracer) ClientOptional

WithTracer

type ClientOptions

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

ClientOptions

type ConnPool added in v0.4.0

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

ConnPool gRPC连接池

func NewConnPool added in v0.4.0

func NewConnPool(config ConnPoolConfig) (*ConnPool, error)

NewConnPool 创建新的连接池

func (*ConnPool) Close added in v0.4.0

func (p *ConnPool) Close() error

Close 关闭连接池

func (*ConnPool) Get added in v0.4.0

func (p *ConnPool) Get(ctx context.Context) (*grpc.ClientConn, error)

Get 获取连接

func (*ConnPool) GetPooled added in v0.4.0

func (p *ConnPool) GetPooled(ctx context.Context) (*PooledClientConn, error)

GetPooled 获取池化连接(带自动归还功能)

func (*ConnPool) Put added in v0.4.0

func (p *ConnPool) Put(conn *grpc.ClientConn)

Put 归还连接

func (*ConnPool) Stats added in v0.4.0

func (p *ConnPool) Stats() ConnPoolStats

Stats 获取连接池统计信息

type ConnPoolConfig added in v0.4.0

type ConnPoolConfig struct {
	Target              string
	MaxSize             int
	MinSize             int
	HealthCheckInterval time.Duration
	Options             []grpc.DialOption
}

ConnPoolConfig 连接池配置

type ConnPoolStats added in v0.4.0

type ConnPoolStats struct {
	TotalConns    int   `json:"total_conns"`
	ActiveConns   int   `json:"active_conns"`
	IdleConns     int   `json:"idle_conns"`
	TotalRequests int64 `json:"total_requests"`
	TotalErrors   int64 `json:"total_errors"`
}

ConnPoolStats 连接池统计信息

type HttpClient added in v0.2.30

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

HttpClient

func NewHttpClient added in v0.2.30

func NewHttpClient(ctx context.Context, uri string, opts ...HttpClientOptional) (*HttpClient, error)

NewHttpClient

func (*HttpClient) Get added in v0.3.0

func (c *HttpClient) Get(ctx context.Context, path string, header http.Header,
	reqData url.Values, rspPtr interface{}) (err error)

Get

func (*HttpClient) GetRaw added in v0.3.0

func (c *HttpClient) GetRaw(ctx context.Context, path string, header http.Header,
	reqData url.Values) (rsp string, err error)

GetRaw

func (*HttpClient) Post added in v0.3.0

func (c *HttpClient) Post(ctx context.Context, path string, header http.Header,
	reqData interface{}, rspPtr interface{}) (err error)

Post

func (*HttpClient) PostRaw added in v0.3.0

func (c *HttpClient) PostRaw(ctx context.Context, path string, header http.Header,
	reqData interface{}) (rsp string, err error)

PostRaw

func (*HttpClient) Request added in v0.2.30

func (c *HttpClient) Request(ctx context.Context, method,
	path string, header http.Header, reqData interface{},
	respDataPtr interface{},
) (err error)

Request 自动序列化和反序列化地请求 请求 和 响应 支持 struct 和 string 和 []byte 三种方式

type HttpClientOptional added in v0.2.30

type HttpClientOptional func(o *HttpClientOptions)

ClientOptional

func WithHttpLogger added in v0.2.30

func WithHttpLogger(logger *zap.Logger) HttpClientOptional

WithHttpLogger

func WithHttpResolver added in v0.2.30

func WithHttpResolver(resolver Resolver) HttpClientOptional

WithHttpResolver

func WithHttpTracer added in v0.2.30

func WithHttpTracer(tracer opentracing.Tracer) HttpClientOptional

WithHttpTracer

func WithReqestTimeout added in v0.2.11

func WithReqestTimeout(t time.Duration) HttpClientOptional

WithReqestTimeout

func WithRetryTimes

func WithRetryTimes(retryTimes int) HttpClientOptional

WithRetryTimes 设置失败重试

type HttpClientOptions added in v0.2.30

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

HttpClientOptions

type PooledClientConn added in v0.4.0

type PooledClientConn struct {
	*grpc.ClientConn
	// contains filtered or unexported fields
}

PooledClientConn 池化连接包装器,实现自动归还

func (*PooledClientConn) Close added in v0.4.0

func (p *PooledClientConn) Close() error

Close 重写Close方法,实现连接归还而非关闭

type Resolver

type Resolver func(ctx context.Context, service string, tags []string) (addr string, err error)

Resolver resolver the host

Jump to

Keyboard shortcuts

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