Skip to content

Commit

Permalink
reset pool
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 3, 2020
1 parent d2a57a3 commit 502950a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
3 changes: 1 addition & 2 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"strconv"
"strings"
"sync"
"time"
)

var (
Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@ 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
}
if minOpen > maxOpen {
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++ {
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}))
}

0 comments on commit 502950a

Please sign in to comment.