Should we allow the Factory (function type creating a grpc client) to accept a context so that calls to Factory can timeout or be cancelled?
Proposed changes to pool.go.
Before:
// Factory is a function type creating a grpc client
type Factory func() (*grpc.ClientConn, error)
...
// Get will return the next available client. If capacity
// has not been reached, it will create a new one using the factory. Otherwise,
// it will wait till the next client becomes available or a timeout.
// A timeout of 0 is an indefinite wait
func (p *Pool) Get(ctx context.Context) (*ClientConn, error) {
...
var err error
if wrapper.ClientConn == nil {
wrapper.ClientConn, err = p.factory()
if err != nil {
// If there was an error, we want to put back a placeholder
// client in the channel
clients <- ClientConn{
pool: p,
}
}
// This is a new connection, reset its initiated time
wrapper.timeInitiated = time.Now()
}
return &wrapper, err
}
After:
// Factory is a function type creating a grpc client
type Factory func(context.Context) (*grpc.ClientConn, error)
...
// Get will return the next available client. If capacity
// has not been reached, it will create a new one using the factory. Otherwise,
// it will wait till the next client becomes available or a timeout.
// A timeout of 0 is an indefinite wait
func (p *Pool) Get(ctx context.Context) (*ClientConn, error) {
...
var err error
if wrapper.ClientConn == nil {
wrapper.ClientConn, err = p.factory(ctx)
if err != nil {
// If there was an error, we want to put back a placeholder
// client in the channel
clients <- ClientConn{
pool: p,
}
}
// This is a new connection, reset its initiated time
wrapper.timeInitiated = time.Now()
}
return &wrapper, err
}
Should we allow the
Factory(function type creating a grpc client) to accept a context so that calls toFactorycan timeout or be cancelled?Proposed changes to
pool.go.Before:
After: