From 6474f9242232fbaf5bfc07542d4a6cd668c9d8d0 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 9 Nov 2017 16:43:43 +0900 Subject: [PATCH] refactoring --- constants.go | 7 +++++-- pool.go | 4 ++-- pool_handler.go | 4 ++-- pool_handlers.go | 18 ++++++++++++------ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/constants.go b/constants.go index e9e1089..dc43ef3 100644 --- a/constants.go +++ b/constants.go @@ -1,8 +1,11 @@ package grpc_pool const ( + DefaultMaxCapacity = 1 + DefaultMaxIdle = 1 // DefaultWriteTimeout is default value of Write Timeout DefaultIdleTimeout = 0 - DefaultMaxIdle = 0 - DefaultMaxCapacity = 1 + // If Wait is true and the pool is at the MaxActive limit, then Get() waits + // for a connection to be returned to the pool before returning. + DefaultWait = false ) diff --git a/pool.go b/pool.go index e05e7c7..d51fbbc 100644 --- a/pool.go +++ b/pool.go @@ -41,12 +41,12 @@ type pool struct { stopWg sync.WaitGroup } -func New(ph PoolHandler) (Pool, error) { +func New(ph PoolHandler) Pool { p := &pool{ ph: ph, } - return p, nil + return p } func (p *pool) Start() error { diff --git a/pool_handler.go b/pool_handler.go index fe50fd8..bc2e771 100644 --- a/pool_handler.go +++ b/pool_handler.go @@ -9,8 +9,8 @@ import ( type PoolHandler interface { Dial() (conn *grpc.ClientConn, client interface{}, err error) - GetIdleTimeout() time.Duration - GetMaxIdle() int GetMaxCapacity() int + GetMaxIdle() int + GetIdleTimeout() time.Duration Validate() } diff --git a/pool_handlers.go b/pool_handlers.go index f44e7b8..539b0fd 100644 --- a/pool_handlers.go +++ b/pool_handlers.go @@ -9,23 +9,29 @@ import ( // Options is configuration of the Pool of GRpc client type PoolHandlers struct { - IdleTimeout time.Duration - MaxIdle int MaxCapacity int + MaxIdle int + IdleTimeout time.Duration + // If Wait is true and the pool is at the MaxActive limit, then Get() waits + // for a connection to be returned to the pool before returning. + Wait bool } func (ph *PoolHandlers) Dial() (*grpc.ClientConn, interface{}, error) { return nil, nil, fmt.Errorf("GRPC Pool: Dial method is not implemented") } -func (ph *PoolHandlers) GetIdleTimeout() time.Duration { - return ph.IdleTimeout +func (ph *PoolHandlers) GetMaxCapacity() int { + return ph.MaxCapacity } func (ph *PoolHandlers) GetMaxIdle() int { return ph.MaxIdle } -func (ph *PoolHandlers) GetMaxCapacity() int { - return ph.MaxCapacity +func (ph *PoolHandlers) GetIdleTimeout() time.Duration { + return ph.IdleTimeout +} +func (ph *PoolHandlers) IsWait() bool { + return ph.Wait } // Validate validates the configuration