Documentation ¶
Index ¶
- Constants
- Variables
- type Config
- type ConnStats
- type DialFunc
- type Logger
- type LoggerFunc
- type Option
- func WithAcquireTimeout(timeout time.Duration) Option
- func WithCleanupInterval(interval time.Duration) Option
- func WithLogger(logger Logger) Option
- func WithMaxConcurrency(max uint) Option
- func WithMaxConnections(max uint) Option
- func WithMaxIdleConnections(max uint) Option
- func WithMaxIdleTime(max time.Duration) Option
- func WithMaxLifetime(max time.Duration) Option
- type Pool
- type Stats
Constants ¶
const ( // DefaultAcquireTimeout is the default timeout for acquiring a connection from the pool using reflect.Select DefaultAcquireTimeout = 50 * time.Millisecond // DefaultCleanupInterval is the default interval for cleaning up idle connections and connections that passed their max lifetime DefaultCleanupInterval = 5 * time.Second // DefaultMaxConcurrency is the default maximum number of concurrent connections DefaultMaxConcurrency = 1000 // DefaultMaxIdleTime is the default maximum time to mark connection as idle when it wasn't used DefaultMaxIdleTime = time.Second * 60 // DefaultMaxLifetime is the default maximum lifetime of a connection DefaultMaxLifetime = 30 * time.Minute )
if these are changed, please change also config values in config.go
const ( // ChosenContextDeadline is returned from Select when context deadline is reached ChosenContextDeadline = 0 // ChosenAcquireTimeout is returned from Select when acquire timeout is reached ChosenAcquireTimeout = 1 )
These chosen constants are in strict order, so we can use them to check which channel was selected Warning: do not change the order of these constants, it will break main loop select.
Variables ¶
var ( ErrInvalidAcquireTimeout = errors.New("invalid acquire timeout") ErrInvalidCleanupInterval = errors.New("invalid cleanup interval") ErrInvalidConnection = errors.New("invalid connection") ErrInvalidDialFunc = errors.New("invalid dial func") ErrDialFailed = errors.New("dial failed") ErrInvalidMaxConcurrency = errors.New("invalid max concurrency") ErrInvalidMaxIdleTime = errors.New("invalid max idle time") ErrInvalidMaxLifetime = errors.New("invalid max lifetime") ErrMaxConnectionsReached = errors.New("max connections reached") ErrAlreadyClosed = errors.New("pool already isClosed") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { AcquireTimeout time.Duration `mapstructure:"acquire_timeout" default:"50ms"` CleanupInterval time.Duration `mapstructure:"cleanup_interval" default:"5s"` MaxConcurrency uint `mapstructure:"max_concurrency" default:"1000"` MaxConnections uint `mapstructure:"max_connections" default:"0"` MaxIdleConnections uint `mapstructure:"max_idle_connections" default:"0"` MaxIdleTime time.Duration `mapstructure:"max_idle_time" default:"60s"` MaxLifetime time.Duration `mapstructure:"max_lifetime" default:"30m"` }
Config is compatible with viper config and mapstructure
It supports default values in struct tags, so you can use it with https://github.com/mcuadros/go-defaults
type ConnStats ¶
type ConnStats struct { Target string `json:"target"` Created time.Time `json:"created"` Deadline time.Time `json:"deadline"` LastChange time.Time `json:"last_change"` WorkingConns uint `json:"working_conns"` IdleConns uint `json:"idle_conns"` Usage uint64 `json:"used"` }
ConnStats represents pool connection statistics
type DialFunc ¶
type DialFunc func(ctx context.Context, stats *Stats, opts ...grpc.DialOption) (*grpc.ClientConn, error)
DialFunc is a function that dials a gRPC connection. This function is passed as required argument to New.
You need to provide your own implementation of this function.
It adds stats information to have context about already established connections. This is for case when you need to do more advanced client side load balancing based on already connected connections
func StaticHostDialFunc ¶
func StaticHostDialFunc(address string, options ...grpc.DialOption) DialFunc
StaticHostDialFunc returns DialFunc that always connects to the same host.
type Logger ¶
type Logger interface { // Log logs a message, this is useful for debugging purposes Log(ctx context.Context, msg string) }
Logger interface for logging
type LoggerFunc ¶
LoggerFunc eases the creation of custom loggers
type Option ¶
type Option func(*options) error
Option is a function that can be passed to New to configure the pool.
func WithAcquireTimeout ¶
WithAcquireTimeout sets the timeout for acquiring a connection from the pool before retrying again. Warning! This is very sensitive value, so please be careful when changing it. It should be very low value. Preferably under 100ms. Please set this value to a higher value only if you know what you are doing!
func WithCleanupInterval ¶
WithCleanupInterval sets the interval for cleaning up idle connections.
func WithMaxConcurrency ¶
WithMaxConcurrency sets the maximum number of concurrent method calls on single connection.
func WithMaxConnections ¶
WithMaxConnections sets the maximum number of connections. This is optional value.
func WithMaxIdleConnections ¶
WithMaxIdleConnections sets the maximum number of idle connections. This is optional value.
func WithMaxIdleTime ¶
WithMaxIdleTime sets the maximum idle time of a connection. It is necessary to set this option.
func WithMaxLifetime ¶
WithMaxLifetime sets the maximum lifetime of a connection. It is necessary to set this option to a value lower than zero.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool implementation
func New ¶
New creates a new pool of gRPC connections.
DialFUnc is required because pool cannot work without it. Options can be passed to configure the pool.
func (*Pool) Acquire ¶
Acquire acquires single connection from the pool. It checks if there is any connection available, and if not, it will dial new connection.
It uses unsafe, but uses it in a very safe way. It is safe to use it in concurrent environment.
Do not forget to Release the connection when you are done with it. Otherwise, you will have a problem.
func (*Pool) Close ¶
Close closes the pool, connections and other background resources.
After pool is closed, you cannot do anything with it.
func (*Pool) Forget ¶
func (p *Pool) Forget(cc *grpc.ClientConn) error
Forget directly removes connection from the pool.
Warning! This method should be only used when you want the connection to be closed asap. For usual use cases, use Release method.
After calling this method, you don't need to call Release.