2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-09-18 18:27:29 +03:00
|
|
|
"context"
|
2022-03-21 14:09:08 +03:00
|
|
|
"strings"
|
2019-06-03 18:44:43 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2020-03-28 16:48:25 +08:00
|
|
|
"google.golang.org/grpc/connectivity"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
2024-02-14 18:09:07 +03:00
|
|
|
type ConnPool struct {
|
2021-04-26 19:17:29 +03:00
|
|
|
conns map[string]*streamsPool
|
|
|
|
size int
|
|
|
|
ttl int64
|
2019-12-27 20:25:58 +08:00
|
|
|
maxStreams int
|
2021-04-26 19:17:29 +03:00
|
|
|
maxIdle int
|
2019-06-03 18:44:43 +01:00
|
|
|
sync.Mutex
|
2019-12-27 20:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type streamsPool struct {
|
|
|
|
// head of list
|
2024-02-14 19:14:56 +03:00
|
|
|
head *PoolConn
|
2019-12-27 20:25:58 +08:00
|
|
|
// busy conns list
|
2024-02-14 19:14:56 +03:00
|
|
|
busy *PoolConn
|
2019-12-27 20:25:58 +08:00
|
|
|
// the siza of list
|
2019-12-29 21:07:55 +00:00
|
|
|
count int
|
2019-12-27 20:25:58 +08:00
|
|
|
// idle conn
|
2019-12-29 21:07:55 +00:00
|
|
|
idle int
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 19:14:56 +03:00
|
|
|
type PoolConn struct {
|
2021-04-26 19:17:29 +03:00
|
|
|
err error
|
2019-06-03 18:44:43 +01:00
|
|
|
*grpc.ClientConn
|
2024-02-14 19:14:56 +03:00
|
|
|
next *PoolConn
|
2024-02-14 18:09:07 +03:00
|
|
|
pool *ConnPool
|
2019-12-27 20:25:58 +08:00
|
|
|
sp *streamsPool
|
2024-02-14 19:14:56 +03:00
|
|
|
pre *PoolConn
|
2021-04-26 19:17:29 +03:00
|
|
|
addr string
|
2019-12-27 20:25:58 +08:00
|
|
|
streams int
|
2019-06-03 18:44:43 +01:00
|
|
|
created int64
|
2021-04-26 19:17:29 +03:00
|
|
|
in bool
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 18:09:07 +03:00
|
|
|
func NewConnPool(size int, ttl time.Duration, idle int, ms int) *ConnPool {
|
2019-12-27 20:25:58 +08:00
|
|
|
if ms <= 0 {
|
|
|
|
ms = 1
|
|
|
|
}
|
|
|
|
if idle < 0 {
|
|
|
|
idle = 0
|
|
|
|
}
|
2024-02-14 18:09:07 +03:00
|
|
|
return &ConnPool{
|
2019-12-29 21:07:55 +00:00
|
|
|
size: size,
|
|
|
|
ttl: int64(ttl.Seconds()),
|
2019-12-27 20:25:58 +08:00
|
|
|
maxStreams: ms,
|
2019-12-29 21:07:55 +00:00
|
|
|
maxIdle: idle,
|
|
|
|
conns: make(map[string]*streamsPool),
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 19:14:56 +03:00
|
|
|
func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption) (*PoolConn, error) {
|
2022-03-21 14:09:08 +03:00
|
|
|
if strings.HasPrefix(addr, "http") {
|
|
|
|
addr = addr[strings.Index(addr, ":")+3:]
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
now := time.Now().Unix()
|
2019-12-27 20:25:58 +08:00
|
|
|
p.Lock()
|
|
|
|
sp, ok := p.conns[addr]
|
|
|
|
if !ok {
|
2024-02-14 19:14:56 +03:00
|
|
|
sp = &streamsPool{head: &PoolConn{}, busy: &PoolConn{}, count: 0, idle: 0}
|
2019-12-27 20:25:58 +08:00
|
|
|
p.conns[addr] = sp
|
|
|
|
}
|
|
|
|
// while we have conns check streams and then return one
|
|
|
|
// otherwise we'll create a new conn
|
|
|
|
conn := sp.head.next
|
|
|
|
for conn != nil {
|
2020-03-28 16:48:25 +08:00
|
|
|
// check conn state
|
|
|
|
// https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md
|
|
|
|
switch conn.GetState() {
|
|
|
|
case connectivity.Connecting:
|
|
|
|
conn = conn.next
|
|
|
|
continue
|
|
|
|
case connectivity.Shutdown:
|
|
|
|
next := conn.next
|
|
|
|
if conn.streams == 0 {
|
|
|
|
removeConn(conn)
|
|
|
|
sp.idle--
|
|
|
|
}
|
|
|
|
conn = next
|
|
|
|
continue
|
|
|
|
case connectivity.TransientFailure:
|
|
|
|
next := conn.next
|
|
|
|
if conn.streams == 0 {
|
|
|
|
removeConn(conn)
|
|
|
|
conn.ClientConn.Close()
|
|
|
|
sp.idle--
|
|
|
|
}
|
|
|
|
conn = next
|
|
|
|
continue
|
|
|
|
case connectivity.Ready:
|
|
|
|
case connectivity.Idle:
|
|
|
|
}
|
2019-12-27 20:25:58 +08:00
|
|
|
// a old conn
|
|
|
|
if now-conn.created > p.ttl {
|
|
|
|
next := conn.next
|
|
|
|
if conn.streams == 0 {
|
|
|
|
removeConn(conn)
|
|
|
|
conn.ClientConn.Close()
|
|
|
|
sp.idle--
|
|
|
|
}
|
|
|
|
conn = next
|
2019-06-03 18:44:43 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-12-27 20:25:58 +08:00
|
|
|
// a busy conn
|
|
|
|
if conn.streams >= p.maxStreams {
|
2019-12-29 21:07:55 +00:00
|
|
|
next := conn.next
|
|
|
|
removeConn(conn)
|
|
|
|
addConnAfter(conn, sp.busy)
|
|
|
|
conn = next
|
2019-12-27 20:25:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// a idle conn
|
|
|
|
if conn.streams == 0 {
|
|
|
|
sp.idle--
|
|
|
|
}
|
|
|
|
// a good conn
|
|
|
|
conn.streams++
|
2019-06-03 18:44:43 +01:00
|
|
|
p.Unlock()
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
p.Unlock()
|
|
|
|
|
2024-12-19 23:34:52 +03:00
|
|
|
// nolint (TODO need fix) create new conn)
|
2020-09-18 18:27:29 +03:00
|
|
|
cc, err := grpc.DialContext(ctx, addr, opts...)
|
2019-06-03 18:44:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-14 19:14:56 +03:00
|
|
|
conn = &PoolConn{ClientConn: cc, err: nil, addr: addr, pool: p, sp: sp, streams: 1, created: time.Now().Unix(), pre: nil, next: nil, in: false}
|
2019-12-27 20:25:58 +08:00
|
|
|
|
|
|
|
// add conn to streams pool
|
|
|
|
p.Lock()
|
|
|
|
if sp.count < p.size {
|
|
|
|
addConnAfter(conn, sp.head)
|
|
|
|
}
|
|
|
|
p.Unlock()
|
2019-06-03 18:44:43 +01:00
|
|
|
|
2019-12-27 20:25:58 +08:00
|
|
|
return conn, nil
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 19:14:56 +03:00
|
|
|
func (p *ConnPool) Put(conn *PoolConn, err error) {
|
2019-06-03 18:44:43 +01:00
|
|
|
p.Lock()
|
2019-12-27 20:25:58 +08:00
|
|
|
p, sp, created := conn.pool, conn.sp, conn.created
|
|
|
|
// try to add conn
|
|
|
|
if !conn.in && sp.count < p.size {
|
|
|
|
addConnAfter(conn, sp.head)
|
|
|
|
}
|
|
|
|
if !conn.in {
|
2019-06-03 18:44:43 +01:00
|
|
|
p.Unlock()
|
|
|
|
conn.ClientConn.Close()
|
|
|
|
return
|
|
|
|
}
|
2019-12-27 20:25:58 +08:00
|
|
|
// a busy conn
|
|
|
|
if conn.streams >= p.maxStreams {
|
|
|
|
removeConn(conn)
|
|
|
|
addConnAfter(conn, sp.head)
|
|
|
|
}
|
|
|
|
conn.streams--
|
|
|
|
// if streams == 0, we can do something
|
|
|
|
if conn.streams == 0 {
|
|
|
|
// 1. it has errored
|
|
|
|
// 2. too many idle conn or
|
|
|
|
// 3. conn is too old
|
|
|
|
now := time.Now().Unix()
|
2019-12-29 21:07:55 +00:00
|
|
|
if err != nil || sp.idle >= p.maxIdle || now-created > p.ttl {
|
2019-12-27 20:25:58 +08:00
|
|
|
removeConn(conn)
|
|
|
|
p.Unlock()
|
|
|
|
conn.ClientConn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sp.idle++
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
p.Unlock()
|
2019-12-27 20:25:58 +08:00
|
|
|
}
|
|
|
|
|
2024-02-14 19:14:56 +03:00
|
|
|
func (conn *PoolConn) Close() {
|
2024-02-14 18:09:07 +03:00
|
|
|
conn.pool.Put(conn, conn.err)
|
2019-12-27 20:25:58 +08:00
|
|
|
}
|
|
|
|
|
2024-02-14 19:14:56 +03:00
|
|
|
func removeConn(conn *PoolConn) {
|
2019-12-27 20:25:58 +08:00
|
|
|
if conn.pre != nil {
|
|
|
|
conn.pre.next = conn.next
|
|
|
|
}
|
|
|
|
if conn.next != nil {
|
|
|
|
conn.next.pre = conn.pre
|
|
|
|
}
|
|
|
|
conn.pre = nil
|
|
|
|
conn.next = nil
|
|
|
|
conn.in = false
|
|
|
|
conn.sp.count--
|
|
|
|
}
|
|
|
|
|
2024-02-14 19:14:56 +03:00
|
|
|
func addConnAfter(conn *PoolConn, after *PoolConn) {
|
2019-12-27 20:25:58 +08:00
|
|
|
conn.next = after.next
|
|
|
|
conn.pre = after
|
|
|
|
if after.next != nil {
|
|
|
|
after.next.pre = conn
|
|
|
|
}
|
|
|
|
after.next = conn
|
|
|
|
conn.in = true
|
|
|
|
conn.sp.count++
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|