Categorygithub.com/phonkee/grpc-pool
modulepackage
0.2.0
Repository: https://github.com/phonkee/grpc-pool.git
Documentation: pkg.go.dev

# README

grpc_pool

High performance gRPC pool for grpc.ClientConn connections. This pool is special. It does not have single connection for single call, but rather it shares single connection for multiple concurrent calls. This is useful when you don't want to overload your servers with too many gRPC method calls on single connection.

how

So how does it work? It's pretty simple. It uses reflect.Select to select on multiple channels. Two channels represent context and acquire timeout, and rest of channels represent connections. Select can return from following channels:

  • 0. context is done - return context Error
  • 1. acquire timeout is done - try to create new connection
  • 2..n connection is ready - return connection

Whole pool is based on this idea. There are some additional features but basically this is how the core works. There are additional features, but it's minor to this main idea.

features

gRPC pool supports following features:

  • max concurrent calls on single connection
  • max idle connections count
  • max idle connection time
  • max connections count
  • max lifetime of connection

All have respective options With....

example

Let's have a look at example use in code.

// create new pool
pool, err := grpc_pool.New(
    // Dial function is used to create new connection
    func(ctx context.Context, stats *grpc_pool.PoolStats, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
        // add additional dial options
        opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
        // create new connection (always pass options from grpc-pool)
        return grpc.DialContext(ctx, "localhost:50051", opts...) 
    }, 
    // WithMaxConcurrency sets how many concurrent calls can be made on single connection 
    WithMaxConcurrency(1000),
    // WithMaxIdleConnections sets how many idle connections can be kept in pool
    WithMaxIdleConnections(5),
    // WithMaxIdleTime sets after how much time idle connection is marked as idle
    WithMaxIdleTime(time.Second*10),
    // WithMaxConnections sets how many connections can be kept in pool
    WithMaxConnections(20),
)

// prepare context with some timeout
ctx, cf := context.WithTimeout(context.Background(), time.Second*10)
defer cf()

// get connection
conn, err := pool.Acquire(ctx)
if err != nil {
	panic(err)
}

// don't forget to return connection back to pool, otherwise you will leak connections, and pool will be confused.
defer pool.Release(conn)

config

gRPC pool provides config compatible with viper (mapstructure), and also provides default tags. These tags work with https://github.com/mcuadros/go-defaults . You still need to provide dial function, so there is some small amout of work necessary. Bear in mind that you can squash this config and add your additional settings. This makes it easy to extend.

stats

gRPC pool provides stats about pool. You can use it to monitor your pool. It is safe to use in concurrent environment. However please note that it can have delay if pool is dialing new connection.

stats := pool.Stats()

author

Peter Vrba [email protected]

# Functions

New creates a new pool of gRPC connections.
StaticHostDialFunc returns DialFunc that always connects to the same host.
WithAcquireTimeout sets the timeout for acquiring a connection from the pool before retrying again.
WithCleanupInterval sets the interval for cleaning up idle connections.
WithLogger sets the logger for the pool.
WithMaxConcurrency sets the maximum number of concurrent method calls on single connection.
WithMaxConnections sets the maximum number of connections.
WithMaxIdleConnections sets the maximum number of idle connections.
WithMaxIdleTime sets the maximum idle time of a connection.
WithMaxLifetime sets the maximum lifetime of a connection.

# Constants

ChosenAcquireTimeout is returned from Select when acquire timeout is reached.
ChosenContextDeadline is returned from Select when context deadline is reached.
DefaultAcquireTimeout is the default timeout for acquiring a connection from the pool using reflect.Select.
DefaultCleanupInterval is the default interval for cleaning up idle connections and connections that passed their max lifetime.
DefaultMaxConcurrency is the default maximum number of concurrent connections.
DefaultMaxIdleTime is the default maximum time to mark connection as idle when it wasn't used.
DefaultMaxLifetime is the default maximum lifetime of a connection.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

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.
ConnStats represents pool connection statistics.
Pool implementation.
Stats represents pool statistics.

# Interfaces

Logger interface for logging.

# Type aliases

DialFunc is a function that dials a gRPC connection.
LoggerFunc eases the creation of custom loggers.
Option is a function that can be passed to New to configure the pool.