ing
This commit is contained in:
parent
15fe1864b3
commit
4f77a6e15d
46
options.go
46
options.go
@ -1,46 +0,0 @@
|
|||||||
package overflow_grpc_pool
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// DefaultWriteTimeout is default value of Write Timeout
|
|
||||||
DefaultIdleTimeout = 0
|
|
||||||
DefaultMaxCapacity = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
CreateFunc func() (*grpc.ClientConn, interface{}, error)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Options is configuration of the Pool of GRpc client
|
|
||||||
type Options struct {
|
|
||||||
Creators CreateFunc
|
|
||||||
|
|
||||||
IdleTimeout time.Duration
|
|
||||||
MaxIdle int
|
|
||||||
MaxCapacity int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate validates the configuration
|
|
||||||
func (o *Options) Validate() *Options {
|
|
||||||
if o.IdleTimeout < 0 {
|
|
||||||
o.IdleTimeout = DefaultIdleTimeout
|
|
||||||
}
|
|
||||||
if o.MaxIdle < 0 {
|
|
||||||
o.MaxIdle = 0
|
|
||||||
}
|
|
||||||
if o.MaxCapacity <= 0 {
|
|
||||||
o.MaxCapacity = DefaultMaxCapacity
|
|
||||||
}
|
|
||||||
if o.Creators == nil {
|
|
||||||
o.Creators = func() (*grpc.ClientConn, interface{}, error) {
|
|
||||||
return nil, nil, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return o
|
|
||||||
}
|
|
20
pool.go
20
pool.go
@ -30,22 +30,22 @@ type Pool interface {
|
|||||||
|
|
||||||
type pool struct {
|
type pool struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
options *Options
|
handler PoolHandler
|
||||||
instances map[interface{}]*pooledInstance
|
instances map[interface{}]*pooledInstance
|
||||||
instancesCh chan *pooledInstance
|
instancesCh chan *pooledInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, o *Options) (Pool, error) {
|
func New(ctx context.Context, handler PoolHandler) (Pool, error) {
|
||||||
var err error
|
var err error
|
||||||
p := &pool{
|
p := &pool{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
options: o.Validate(),
|
handler: handler,
|
||||||
instances: make(map[interface{}]*pooledInstance, o.MaxCapacity),
|
instances: make(map[interface{}]*pooledInstance, handler.GetMaxCapacity()),
|
||||||
instancesCh: make(chan *pooledInstance, o.MaxCapacity),
|
instancesCh: make(chan *pooledInstance, handler.GetMaxCapacity()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if 0 < o.MaxIdle {
|
if 0 < handler.GetMaxIdle() {
|
||||||
for i := 0; i < o.MaxIdle; i++ {
|
for i := 0; i < handler.GetMaxIdle(); i++ {
|
||||||
err = p.createInstance()
|
err = p.createInstance()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -57,13 +57,13 @@ func New(ctx context.Context, o *Options) (Pool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *pool) createInstance() error {
|
func (p *pool) createInstance() error {
|
||||||
maxCapacity := p.options.MaxCapacity
|
maxCapacity := p.handler.GetMaxCapacity()
|
||||||
currentInstanceCount := len(p.instances)
|
currentInstanceCount := len(p.instances)
|
||||||
if currentInstanceCount >= maxCapacity {
|
if currentInstanceCount >= maxCapacity {
|
||||||
return ErrPoolFull
|
return ErrPoolFull
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, i, err := p.options.Creators()
|
conn, i, err := p.handler.OnCreate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ func (p *pool) get() (*pooledInstance, error) {
|
|||||||
// All good
|
// All good
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
idleTimeout := p.options.IdleTimeout
|
idleTimeout := p.handler.GetIdleTimeout()
|
||||||
if 1 < len(p.instancesCh) && 0 < idleTimeout && pi.idleTime.Add(idleTimeout).Before(time.Now()) {
|
if 1 < len(p.instancesCh) && 0 < idleTimeout && pi.idleTime.Add(idleTimeout).Before(time.Now()) {
|
||||||
go p.destroyInstance(pi.instance)
|
go p.destroyInstance(pi.instance)
|
||||||
continue
|
continue
|
||||||
|
23
pool_handler.go
Normal file
23
pool_handler.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package overflow_grpc_pool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DefaultWriteTimeout is default value of Write Timeout
|
||||||
|
DefaultIdleTimeout = 0
|
||||||
|
DefaultMaxIdle = 0
|
||||||
|
DefaultMaxCapacity = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
type PoolHandler interface {
|
||||||
|
GetIdleTimeout() time.Duration
|
||||||
|
GetMaxIdle() int
|
||||||
|
GetMaxCapacity() int
|
||||||
|
|
||||||
|
OnCreate() (*grpc.ClientConn, interface{}, error)
|
||||||
|
Validate()
|
||||||
|
}
|
41
pool_handlers.go
Normal file
41
pool_handlers.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package overflow_grpc_pool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options is configuration of the Pool of GRpc client
|
||||||
|
type PoolHandlers struct {
|
||||||
|
IdleTimeout time.Duration
|
||||||
|
MaxIdle int
|
||||||
|
MaxCapacity int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *PoolHandlers) GetIdleTimeout() time.Duration {
|
||||||
|
return h.IdleTimeout
|
||||||
|
}
|
||||||
|
func (h *PoolHandlers) GetMaxIdle() int {
|
||||||
|
return h.MaxIdle
|
||||||
|
}
|
||||||
|
func (h *PoolHandlers) GetMaxCapacity() int {
|
||||||
|
return h.MaxCapacity
|
||||||
|
}
|
||||||
|
func (h *PoolHandlers) OnCreate() (*grpc.ClientConn, interface{}, error) {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate validates the configuration
|
||||||
|
func (o *PoolHandlers) Validate() {
|
||||||
|
if o.IdleTimeout < 0 {
|
||||||
|
o.IdleTimeout = DefaultIdleTimeout
|
||||||
|
}
|
||||||
|
if o.MaxIdle < 0 {
|
||||||
|
o.MaxIdle = DefaultMaxIdle
|
||||||
|
}
|
||||||
|
if o.MaxCapacity <= 0 {
|
||||||
|
o.MaxCapacity = DefaultMaxCapacity
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user