Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go gRPC Client Connection Scope and Pooling

Tags:

go

grpc

Considering the example from the Go gRPC code base:

func main() {
    // Set up a connection to the server.
    conn, err := grpc.Dial(address, grpc.WithInsecure())
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    c := pb.NewGreeterClient(conn)

    // Contact the server and print out its response.
    name := defaultName
    if len(os.Args) > 1 {
        name = os.Args[1]
    }
    r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }
    log.Printf("Greeting: %s", r.Message)
}

When consuming a gRPC service from another service what should the scope of the connection (conn) be? I assume it should have affinity with the scope of the request being handled be the consumer service, but I have yet to find any documentation around this. Should I be using a connection pool here?

E.G.

  1. gRPC consumer service receives request
  2. establish connection to gRPC service (either directly or via pool)
  3. make n requests to gRPC service
  4. close gRPC connection (or release back to the pool)
like image 368
Myles McDonnell Avatar asked Jan 25 '18 11:01

Myles McDonnell


People also ask

Does gRPC need connection pool?

gRPC builds on top of this foundation with connection pooling, health semantics, efficient use of data frames and multiplexing, and KeepAlive. Developers choosing protocols must choose those that meet today's demands as well as tomorrow's.

Can gRPC handle multiple clients?

Multiple gRPC clients can be created from a channel, including different types of clients. A channel and clients created from the channel can safely be used by multiple threads. Clients created from the channel can make multiple simultaneous calls.

Can a gRPC server also be a client?

Yes, you can definitely do that.

Can gRPC use http1?

grpc itself is a protocol that can be transported over basically anything, but http1.

How to pool a gRPC connection?

A gRPC connection pool on client side combined with a server side TCP (Layer 4) load balancer. This will create a pool of client connections initially, and re-use this pool of connections for subsequent gRPC requests.

How do you load balance a gRPC connection?

Load Balancing options include: A gRPC connection pool on client side combined with a server side TCP (Layer 4) load balancer. This will create a pool of client connections initially, and re-use this pool of connections for subsequent gRPC requests. This is the easier route to implement in my opinion.

How do I use the go gRPC API?

Use the Go gRPC API to write a simple client and server for your service. It assumes that you have read the Introduction to gRPC and are familiar with protocol buffers . Note that the example in this tutorial uses the proto3 version of the protocol buffers language: you can find out more in the proto3 language guide and the Go generated code guide.

Do I need gRPC-go-pool?

grpc-go-pool is a gRPC pool implementation. As a novice I look at it and think that's maybe what I want. But I also have a nagging doubt that it's not needed at all. Sorry, something went wrong. ClientConns can safely be accessed concurrently, and RPCs will be sent in parallel.


1 Answers

From experience, gRPC client connections should be re-used for the lifetime of the client application as they are safe for concurrent use. Furthermore, one of the key features of gRPC is rapid response from remote procedural calls, which would not be achieved if you have to reconnect on every request received.

Nonetheless, it is highly recommended to use some kind of gRPC load balancing along with these persistent connections. Otherwise, a lot of the load may end up on a few long-lived grpc client-server connections. Load Balancing options include:

  1. A gRPC connection pool on client side combined with a server side TCP (Layer 4) load balancer. This will create a pool of client connections initially, and re-use this pool of connections for subsequent gRPC requests. This is the easier route to implement in my opinion. See Pooling gRPC Connections for an example of grpc connection pooling on grpc client side which uses the grpc-go-pool library.
  2. HTTP/2(Layer 7) load balancer with gRPC support for load balancing requests. See gRPC Load Balancing which gives an overview of different grpc load balancing options. nginx recently added support for gRPC load balancing.
like image 136
Agrim Avatar answered Oct 17 '22 23:10

Agrim