grpccoooooonnpool

package module
v0.0.0-...-9528cf9 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2022 License: MIT Imports: 11 Imported by: 0

README

grpccoooooonnpool

gRPC connection pool for client side.

How to use it?

see ./mocker/simple-proxy as your nice example.

Contributing

Step 1
  • 🍴 Fork this repo!
Step 2
  • 🔨 HACK AWAY!
Step 3

Support

License

  • This project is licensed under the MIT License - see the MIT license for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultMaxSendMsgSize int = 8 * 1024 * 1024
	DefaultMaxRecvMsgSize int = 8 * 1024 * 1024

	DefaultInitialWindowSize     int32 = 128 * 1024 * 1024
	DefaultInitialConnWindowSize int32 = 128 * 1024 * 1024

	DefaultDialTimeout       = 5 * time.Second
	DefaultMinConnectTimeout = 3 * time.Second
	DefaultKeepAliveTime     = 10 * time.Second
	DefaultKeepAliveTimeout  = 3 * time.Second
)

NOTE: Tune the default values to provide better system's throughput.

View Source
var (
	ErrInvalidPoolSettings          = errors.New("invalid pool settings")
	ErrPoolAlreadyClosed            = errors.New("pool is already closed")
	ErrPoolResourceAlreadyExhausted = errors.New("pool has no available connection")
)

Functions

func DefaultDial

func DefaultDial(serverAddr, serverName, certFile string) (*grpc.ClientConn, error)

DefaultDial returns a secure grpc client connection with default settings.

func DefaultDialWithInsecure

func DefaultDialWithInsecure(serverAddr string) (*grpc.ClientConn, error)

DefaultDialWithInsecure returns an insecure grpc client connection with default settings.

Types

type GrpcClientConnPool

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

func NewGrpcClientConnPool

func NewGrpcClientConnPool(opts ...PoolSettingsOption) (p *GrpcClientConnPool, err error)

NewGrpcClientConnPool returns a new fresh grpc client connection pool.

func (*GrpcClientConnPool) PickOne

func (p *GrpcClientConnPool) PickOne(wait bool, waitTime int64) (*GrpcConn, error)

PickOne returns a available connection from the pool. User should use GrpcConn.Close() to put the connection back to the pool. If waitTime <= 0, which means block waiting for PickOne.

func (*GrpcClientConnPool) Release

func (p *GrpcClientConnPool) Release() error

Release releases all pool resources.

func (*GrpcClientConnPool) Status

func (p *GrpcClientConnPool) Status() string

Status returns the current status of the pool.

type GrpcConn

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

GrpcConn encapsulates the grpc.ClientConn.

func (*GrpcConn) Close

func (c *GrpcConn) Close()

Close decreases the reference of grpc connection if pool not full or just close the underlay TCP connection.

func (*GrpcConn) Recycle

func (c *GrpcConn) Recycle()

func (*GrpcConn) Underlay

func (c *GrpcConn) Underlay() *grpc.ClientConn

Underlay returns the actual grpc connection.

type PoolSettings

type PoolSettings struct {
	// Grpc server address to serve client connections.
	Addr string
	// An app-supplied-function for connection creation and configuration.
	Dial func(addr string) (conn *grpc.ClientConn, err error)
	// Maximum number of idle connections inside the pool, should be the power of 2.
	MaxIdles uint32
	// Maximum number of connections can be allocated by the pool at a given time, should be the power of 2.
	MaxActives uint32
	// Maximum number of concurrent streams attached to a single tcp connection, should be the power of 2.
	MaxStreams uint32
}

PoolSettings includes pool settings for pool initialization.

type PoolSettingsOption

type PoolSettingsOption func(settings *PoolSettings)

func WithAddr

func WithAddr(addr string) PoolSettingsOption

func WithDialFunc

func WithDialFunc(f func(addr string) (conn *grpc.ClientConn, err error)) PoolSettingsOption

func WithMaxActives

func WithMaxActives(n uint32) PoolSettingsOption

func WithMaxIdles

func WithMaxIdles(n uint32) PoolSettingsOption

func WithMaxStreams

func WithMaxStreams(n uint32) PoolSettingsOption

Directories

Path Synopsis
internal
mocker

Jump to

Keyboard shortcuts

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