diff --git a/options.go b/options.go deleted file mode 100644 index 438131d..0000000 --- a/options.go +++ /dev/null @@ -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 -} diff --git a/pool.go b/pool.go index cc9294d..392408c 100644 --- a/pool.go +++ b/pool.go @@ -30,22 +30,22 @@ type Pool interface { type pool struct { ctx context.Context - options *Options + handler PoolHandler instances map[interface{}]*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 p := &pool{ ctx: ctx, - options: o.Validate(), - instances: make(map[interface{}]*pooledInstance, o.MaxCapacity), - instancesCh: make(chan *pooledInstance, o.MaxCapacity), + handler: handler, + instances: make(map[interface{}]*pooledInstance, handler.GetMaxCapacity()), + instancesCh: make(chan *pooledInstance, handler.GetMaxCapacity()), } - if 0 < o.MaxIdle { - for i := 0; i < o.MaxIdle; i++ { + if 0 < handler.GetMaxIdle() { + for i := 0; i < handler.GetMaxIdle(); i++ { err = p.createInstance() if nil != err { return nil, err @@ -57,13 +57,13 @@ func New(ctx context.Context, o *Options) (Pool, error) { } func (p *pool) createInstance() error { - maxCapacity := p.options.MaxCapacity + maxCapacity := p.handler.GetMaxCapacity() currentInstanceCount := len(p.instances) if currentInstanceCount >= maxCapacity { return ErrPoolFull } - conn, i, err := p.options.Creators() + conn, i, err := p.handler.OnCreate() if err != nil { return err } @@ -111,7 +111,7 @@ func (p *pool) get() (*pooledInstance, error) { // All good default: } - idleTimeout := p.options.IdleTimeout + idleTimeout := p.handler.GetIdleTimeout() if 1 < len(p.instancesCh) && 0 < idleTimeout && pi.idleTime.Add(idleTimeout).Before(time.Now()) { go p.destroyInstance(pi.instance) continue diff --git a/pool_handler.go b/pool_handler.go new file mode 100644 index 0000000..72986c8 --- /dev/null +++ b/pool_handler.go @@ -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() +} diff --git a/pool_handlers.go b/pool_handlers.go new file mode 100644 index 0000000..79116e9 --- /dev/null +++ b/pool_handlers.go @@ -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 + } + +}