1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-18 22:17:44 +02:00

Moving to gRPC by default (#1069)

* Step 1

* Fix the test panics
This commit is contained in:
Asim Aslam
2019-12-29 21:07:55 +00:00
committed by GitHub
parent 943445270f
commit c145f355dd
16 changed files with 273 additions and 259 deletions

View File

@ -10,11 +10,11 @@ import (
type pool struct {
size int
ttl int64
// max streams on a *poolConn
maxStreams int
// max idle conns
maxIdle int
maxIdle int
sync.Mutex
conns map[string]*streamsPool
@ -22,20 +22,20 @@ type pool struct {
type streamsPool struct {
// head of list
head *poolConn
head *poolConn
// busy conns list
busy *poolConn
busy *poolConn
// the siza of list
count int
count int
// idle conn
idle int
idle int
}
type poolConn struct {
// grpc conn
*grpc.ClientConn
err error
addr string
err error
addr string
// pool and streams pool
pool *pool
@ -44,9 +44,9 @@ type poolConn struct {
created int64
// list
pre *poolConn
next *poolConn
in bool
pre *poolConn
next *poolConn
in bool
}
func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
@ -57,11 +57,11 @@ func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
idle = 0
}
return &pool{
size: size,
ttl: int64(ttl.Seconds()),
size: size,
ttl: int64(ttl.Seconds()),
maxStreams: ms,
maxIdle: idle,
conns: make(map[string]*streamsPool),
maxIdle: idle,
conns: make(map[string]*streamsPool),
}
}
@ -70,7 +70,7 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
p.Lock()
sp, ok := p.conns[addr]
if !ok {
sp = &streamsPool{head:&poolConn{}, busy:&poolConn{}, count:0, idle:0}
sp = &streamsPool{head: &poolConn{}, busy: &poolConn{}, count: 0, idle: 0}
p.conns[addr] = sp
}
// while we have conns check streams and then return one
@ -90,11 +90,11 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
}
// a busy conn
if conn.streams >= p.maxStreams {
next := conn.next
removeConn(conn)
addConnAfter(conn, sp.busy)
conn = next
continue
next := conn.next
removeConn(conn)
addConnAfter(conn, sp.busy)
conn = next
continue
}
// a idle conn
if conn.streams == 0 {
@ -112,7 +112,7 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
if err != nil {
return nil, err
}
conn = &poolConn{cc,nil,addr,p,sp,1,time.Now().Unix(), nil, nil, false}
conn = &poolConn{cc, nil, addr, p, sp, 1, time.Now().Unix(), nil, nil, false}
// add conn to streams pool
p.Lock()
@ -148,7 +148,7 @@ func (p *pool) release(addr string, conn *poolConn, err error) {
// 2. too many idle conn or
// 3. conn is too old
now := time.Now().Unix()
if err != nil || sp.idle >= p.maxIdle || now-created > p.ttl {
if err != nil || sp.idle >= p.maxIdle || now-created > p.ttl {
removeConn(conn)
p.Unlock()
conn.ClientConn.Close()
@ -160,11 +160,11 @@ func (p *pool) release(addr string, conn *poolConn, err error) {
return
}
func (conn *poolConn)Close() {
func (conn *poolConn) Close() {
conn.pool.release(conn.addr, conn, conn.err)
}
func removeConn(conn *poolConn) {
func removeConn(conn *poolConn) {
if conn.pre != nil {
conn.pre.next = conn.next
}
@ -178,7 +178,7 @@ func removeConn(conn *poolConn) {
return
}
func addConnAfter(conn *poolConn, after *poolConn) {
func addConnAfter(conn *poolConn, after *poolConn) {
conn.next = after.next
conn.pre = after
if after.next != nil {