From d5a54ecfa4ec069ba2ec0b7830b5ef41259843c6 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 --- grpc_pool.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/grpc_pool.go b/grpc_pool.go index 340aecc..0eec469 100644 --- a/grpc_pool.go +++ b/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