diff --git a/grpc.go b/grpc.go index 9a7d98f..379c414 100644 --- a/grpc.go +++ b/grpc.go @@ -24,7 +24,6 @@ import ( "strconv" "strings" "sync" - "time" ) var ( @@ -64,7 +63,7 @@ func GRPCRequestPools(url string, business Business) (interface{}, error) { ) if nil == pond { mu.Lock() - pond = NewPond(10, 100, 5*time.Second, func() (conn Conn, e error) { + pond = NewPond(1, 10, func() (conn Conn, e error) { return grpc.Dial(url, grpc.WithInsecure()) }) reqs[url] = pond diff --git a/pool.go b/pool.go index c9adbd1..ac88b48 100644 --- a/pool.go +++ b/pool.go @@ -37,10 +37,8 @@ type factory func() (Conn, error) // // maxOpen 池中最大资源数 // -// maxLifetime -// // factory -func NewPond(minOpen, maxOpen int, maxLifetime time.Duration, factory factory) *Pond { +func NewPond(minOpen, maxOpen int, factory factory) *Pond { if maxOpen <= 0 { maxOpen = 5 } @@ -48,11 +46,10 @@ func NewPond(minOpen, maxOpen int, maxLifetime time.Duration, factory factory) * maxOpen = minOpen + 1 } p := &Pond{ - maxOpen: maxOpen, - minOpen: minOpen, - maxLifetime: maxLifetime, - factory: factory, - conn: make(chan Conn, maxOpen), + maxOpen: maxOpen, + minOpen: minOpen, + factory: factory, + conn: make(chan Conn, maxOpen), } for i := 0; i < minOpen; i++ { @@ -122,7 +119,11 @@ func (p *Pond) Release(conn Conn) error { if p.closed { return errPoolClosed } - p.conn <- conn + if len(p.conn) < p.minOpen { + p.conn <- conn + } else { + p.Close(conn) + } return nil } diff --git a/pool_test.go b/pool_test.go index b4ceec4..c36d84d 100644 --- a/pool_test.go +++ b/pool_test.go @@ -17,11 +17,10 @@ package gnomon import ( "google.golang.org/grpc" "testing" - "time" ) func TestNewPond(t *testing.T) { - t.Log(NewPond(10, 100, 5*time.Second, func() (conn Conn, e error) { + t.Log(NewPond(10, 100, func() (conn Conn, e error) { return grpc.Dial("http://wwww.gnomon.com", grpc.WithInsecure()) })) }