From 15fe1864b3b1fb3d166954ebe0fedeaf5ca32518 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 30 Aug 2017 15:32:53 +0900 Subject: [PATCH] ing --- options.go | 8 +++++--- pool.go | 14 +++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/options.go b/options.go index 4664e00..438131d 100644 --- a/options.go +++ b/options.go @@ -1,7 +1,6 @@ package overflow_grpc_pool import ( - "log" "time" "google.golang.org/grpc" @@ -10,6 +9,7 @@ import ( const ( // DefaultWriteTimeout is default value of Write Timeout DefaultIdleTimeout = 0 + DefaultMaxCapacity = 1 ) type ( @@ -34,10 +34,12 @@ func (o *Options) Validate() *Options { o.MaxIdle = 0 } if o.MaxCapacity <= 0 { - log.Panicln("Max capacity of Pool must be greater than 0.") + o.MaxCapacity = DefaultMaxCapacity } if o.Creators == nil { - log.Panicln("Creators of Pool must be specified.") + o.Creators = func() (*grpc.ClientConn, interface{}, error) { + return nil, nil, nil + } } return o diff --git a/pool.go b/pool.go index 4c4696d..cc9294d 100644 --- a/pool.go +++ b/pool.go @@ -1,8 +1,8 @@ package overflow_grpc_pool import ( + "context" "errors" - "log" "time" "google.golang.org/grpc" @@ -29,14 +29,16 @@ type Pool interface { } type pool struct { + ctx context.Context options *Options instances map[interface{}]*pooledInstance instancesCh chan *pooledInstance } -func New(o *Options) (Pool, error) { +func New(ctx context.Context, o *Options) (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), @@ -77,17 +79,19 @@ func (p *pool) createInstance() error { return nil } -func (p *pool) destroyInstance(i interface{}) { +func (p *pool) destroyInstance(i interface{}) error { var err error pi, ok := p.instances[i] if !ok { - return + return nil } err = pi.clientConn.Close() if nil != err { - log.Printf("pool: ClientConn close error: %v", err) + return err } delete(p.instances, i) + + return nil } func (p *pool) get() (*pooledInstance, error) {