Do we need to use different grpc connections for different streams for same client (specific to golang)?

huangapple go评论113阅读模式
英文:

Do we need to use different grpc connections for different streams for same client (specific to golang)?

问题

我的项目(使用golang)有一个使用案例,我需要从单个GRPC客户端打开多个流到单个/多个GRPC服务器。

假设我的proto文件如下:

  1. syntax = "proto3";
  2. package helloworld;
  3. // The greeting service definition.
  4. service Greeter {
  5. // Sends a greeting
  6. rpc SayHello (stream HelloRequest) returns (stream HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

现在对于我的客户端,我有两个选项:

  1. package main
  2. import (
  3. "google.golang.org/grpc"
  4. "hello/grpc/helloworld"
  5. )
  6. func newConn() helloworld.GreeterClient {
  7. conn, err := grpc.Dial("localhost:9009", grpc.WithInsecure())
  8. if err != nil {
  9. panic(err)
  10. }
  11. return helloworld.NewGreeterClient(conn)
  12. }
  13. func clientWork(client helloworld.GreeterClient) {
  14. // client work
  15. }
  16. func main() {
  17. // option 1 - new connection objects for all streams
  18. for i := 0; i < 1000; i++ {
  19. clientWork(newConn())
  20. }
  21. // option 2 - single connection object for all streams
  22. globalConn := newConn()
  23. for i := 0; i < 1000; i++ {
  24. clientWork(globalConn)
  25. }
  26. }

选项1是为流使用新的grpc.Conn

选项2是为所有流使用单个全局grpc.Conn

我的问题是 - 是否有推荐的方法来实现相同的功能(如果是特定情况,我可以得到一些示例)。

我阅读了https://github.com/grpc/grpc-go/issues/2086#issuecomment-389947160,其中建议释放底层缓冲区/内存有几个选项,其中之一包括关闭grpc.Conn,这让我思考在我的情况下应该选择哪个选项。

谢谢。

英文:

My project (in golang) has a use case where I need to open several streams from single GRPC client to single/multiple GRPC servers.

Say my proto file is -

  1. syntax = &quot;proto3&quot;;
  2. package helloworld;
  3. // The greeting service definition.
  4. service Greeter {
  5. // Sends a greeting
  6. rpc SayHello (stream HelloRequest) returns (stream HelloReply) {}
  7. }
  8. // The request message containing the user&#39;s name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Now for my client I have 2 options -

  1. package main
  2. import (
  3. &quot;google.golang.org/grpc&quot;
  4. &quot;hello/grpc/helloworld&quot;
  5. )
  6. func newConn() helloworld.GreeterClient {
  7. conn, err := grpc.Dial(&quot;localhost:9009&quot;, grpc.WithInsecure())
  8. if err != nil {
  9. panic(err)
  10. }
  11. return helloworld.NewGreeterClient(conn)
  12. }
  13. func clientWork(client helloworld.GreeterClient) {
  14. // client work
  15. }
  16. func main() {
  17. // option 1 - new connection objects for all streams
  18. for i:=0; i&lt;1000; i++ {
  19. clientWork(newConn())
  20. }
  21. // option 2 - single connection object for all streams
  22. globalConn := newConn()
  23. for i:=0; i&lt;1000; i++ {
  24. clientWork(globalConn)
  25. }
  26. }

Option 1 is using new grpc.Conn for streams

Option 2 is using single global grpc.Conn for all streams

My question - is their any recommended way of achieving the same (if it is case specific, can I get some exmaples)

I went through https://github.com/grpc/grpc-go/issues/2086#issuecomment-389947160 that suggests that to release underlying buffers/memory I have few options one of which includes closing the grpc.Conn that got me thinking which option should be more suitable in my case.

Thanks

答案1

得分: 1

使用gRPC时,通常建议您对同一服务器的所有流使用单个连接。主要的例外情况是对于每个连接并发流的数量有限制的服务器。

英文:

With gRPC it is generally recommended that you use a single connection for all streams to the same server. The main exceptions are for servers that have limits on the number of concurrent streams per connection.

huangapple
  • 本文由 发表于 2021年6月21日 22:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68069476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定