Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from micro/pool
Connection Pooling
- Loading branch information
Showing
8 changed files
with
277 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package client | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/micro/go-micro/transport" | ||
) | ||
|
||
type pool struct { | ||
size int | ||
ttl int64 | ||
|
||
sync.Mutex | ||
conns map[string][]*poolConn | ||
} | ||
|
||
type poolConn struct { | ||
transport.Client | ||
created int64 | ||
} | ||
|
||
func newPool(size int, ttl time.Duration) *pool { | ||
return &pool{ | ||
size: size, | ||
ttl: int64(ttl.Seconds()), | ||
conns: make(map[string][]*poolConn), | ||
} | ||
} | ||
|
||
// NoOp the Close since we manage it | ||
func (p *poolConn) Close() error { | ||
return nil | ||
} | ||
|
||
func (p *pool) getConn(addr string, tr transport.Transport, opts ...transport.DialOption) (*poolConn, error) { | ||
p.Lock() | ||
conns := p.conns[addr] | ||
now := time.Now().Unix() | ||
|
||
// while we have conns check age and then return one | ||
// otherwise we'll create a new conn | ||
for len(conns) > 0 { | ||
conn := conns[len(conns)-1] | ||
conns = conns[:len(conns)-1] | ||
p.conns[addr] = conns | ||
|
||
// if conn is old kill it and move on | ||
if d := now - conn.created; d > p.ttl { | ||
conn.Client.Close() | ||
continue | ||
} | ||
|
||
// we got a good conn, lets unlock and return it | ||
p.Unlock() | ||
|
||
return conn, nil | ||
} | ||
|
||
p.Unlock() | ||
|
||
// create new conn | ||
c, err := tr.Dial(addr, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &poolConn{c, time.Now().Unix()}, nil | ||
} | ||
|
||
func (p *pool) release(addr string, conn *poolConn, err error) { | ||
// don't store the conn if it has errored | ||
if err != nil { | ||
conn.Client.Close() | ||
return | ||
} | ||
|
||
// otherwise put it back for reuse | ||
p.Lock() | ||
conns := p.conns[addr] | ||
if len(conns) >= p.size { | ||
p.Unlock() | ||
conn.Client.Close() | ||
return | ||
} | ||
p.conns[addr] = append(conns, conn) | ||
p.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package client | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/micro/go-micro/transport" | ||
"github.com/micro/go-micro/transport/mock" | ||
) | ||
|
||
func testPool(t *testing.T, size int, ttl time.Duration) { | ||
// zero pool | ||
p := newPool(size, ttl) | ||
|
||
// mock transport | ||
tr := mock.NewTransport() | ||
|
||
// listen | ||
l, err := tr.Listen(":0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer l.Close() | ||
|
||
// accept loop | ||
go func() { | ||
for { | ||
if err := l.Accept(func(s transport.Socket) { | ||
for { | ||
var msg transport.Message | ||
if err := s.Recv(&msg); err != nil { | ||
return | ||
} | ||
if err := s.Send(&msg); err != nil { | ||
return | ||
} | ||
} | ||
}); err != nil { | ||
return | ||
} | ||
} | ||
}() | ||
|
||
for i := 0; i < 10; i++ { | ||
// get a conn | ||
c, err := p.getConn(l.Addr(), tr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
msg := &transport.Message{ | ||
Body: []byte(`hello world`), | ||
} | ||
|
||
if err := c.Send(msg); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var rcv transport.Message | ||
|
||
if err := c.Recv(&rcv); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(rcv.Body) != string(msg.Body) { | ||
t.Fatalf("got %v, expected %v", rcv.Body, msg.Body) | ||
} | ||
|
||
// release the conn | ||
p.release(l.Addr(), c, nil) | ||
|
||
p.Lock() | ||
if i := len(p.conns[l.Addr()]); i > size { | ||
p.Unlock() | ||
t.Fatal("pool size %d is greater than expected %d", i, size) | ||
} | ||
p.Unlock() | ||
} | ||
} | ||
|
||
func TestRPCPool(t *testing.T) { | ||
testPool(t, 0, time.Minute) | ||
testPool(t, 2, time.Minute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.