Go 的 grpc 客户端的连接池,支持连接重用。
池提供附加功能:
Connection reuseFailure reconnection
package main import ( "fmt" "github.com/shimingyah/pool" "google.golang.org/grpc" "log" "sync" ) var ( grpcOnce sync.Once poolConn pool.Pool ) func main() { } //参考: https://github.com/shimingyah/pool func GetGrpcClient() *grpc.ClientConn { grpcOnce.Do(func() { var err error //grpc 服务器的地址或者 服务注册中心的地址 address :="" poolConn, err = pool.New(address, pool.DefaultOptions) if err != nil { log.Fatalf("failed to new pool: %v", err) } }) conn, err := poolConn.Get() if err != nil { log.Fatalf("failed to get conn: %v", err) } return conn.Value() }