From 8282e781e46062c98c9cf8feaf1c015d4259d026 Mon Sep 17 00:00:00 2001 From: Socket <30768657+Socketsj@users.noreply.github.com> Date: Sat, 28 Mar 2020 16:48:25 +0800 Subject: [PATCH] grpc pool should check state (#1435) Co-authored-by: huangshaojie --- client/grpc/grpc_pool.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/client/grpc/grpc_pool.go b/client/grpc/grpc_pool.go index 340aecce..0eec4698 100644 --- a/client/grpc/grpc_pool.go +++ b/client/grpc/grpc_pool.go @@ -5,6 +5,7 @@ import ( "time" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" ) type pool struct { @@ -77,6 +78,32 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error) // otherwise we'll create a new conn conn := sp.head.next for conn != nil { + // 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: + } // a old conn if now-conn.created > p.ttl { next := conn.next