README

grpc-connection-pool

GoDoc

Package grpc_connection_pool provides the ability to create connection pool for gRPC.

License

Licensed under the MIT License

Authors

Copyright(c) 2020 Fred Chien cfsghost@gmail.com

Expand ▾ Collapse ▴

Documentation

Overview

Package grpc_connection_pool provides the ability to create connection pool for gRPC.

Here is example to create connection pool:

// Create Options object
options := &pool.Options{
	InitCap: 8,
	MaxCap: 16,
	DialTimeout: time.Second * 20,
}

// Create connection pool with options
pool, err := grpc_connection_pool.NewGRPCPool("localhost:8888", options, grpc.WithInsecure())
if err != nil {
	log.Println(err)
	return
}

// Get a available gRPC client
client := pool.Get()

Index

Constants

This section is empty.

Variables

var ErrExceeded = errors.New("Maximum number of connections exceeded")

ErrExceeded is the error when maximum number of connections exceeded


var ErrUnavailable = errors.New("No available connection")

Functions

This section is empty.

Types

type Connection

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

func NewConnection

func NewConnection(pool *GRPCPool, c *grpc.ClientConn) *Connection

func (*Connection) GetStream

func (connection *Connection) GetStream(name string, fn StreamHandler) error

type GRPCPool

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

func NewGRPCPool

func NewGRPCPool(host string, options *Options, dialOptions ...grpc.DialOption) (*GRPCPool, error)

NewGRPCPool creates a new connection pool.

func (*GRPCPool) Get

func (pool *GRPCPool) Get() (*grpc.ClientConn, error)

Get will returns a available gRPC client.

func (*GRPCPool) GetStream

func (pool *GRPCPool) GetStream(name string, fn StreamHandler) error

func (*GRPCPool) GetStreamInitializer

func (pool *GRPCPool) GetStreamInitializer(name string) StreamInitializer

func (*GRPCPool) Pop

func (pool *GRPCPool) Pop() (*grpc.ClientConn, error)

Pop will return a availabe gRPC client and the gRPC client will not be reused before return client to the pool.

func (*GRPCPool) Push

func (pool *GRPCPool) Push(connection *grpc.ClientConn) error

Push will put gRPC client to the pool.

func (*GRPCPool) SetStreamInitializer

func (pool *GRPCPool) SetStreamInitializer(name string, initializer StreamInitializer)

type Options

type Options struct {
	InitCap     int
	MaxCap      int
	DialTimeout time.Duration
}

Options represent all of the available options when creating a connection pool with NewGRPCPool.

func NewOptions

func NewOptions() *Options

NewOptions creates a Options object.

type StreamHandler

type StreamHandler func(interface{}) error

type StreamInitializer

type StreamInitializer func(*grpc.ClientConn) (interface{}, error)