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

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

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

问题

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

假设我的proto文件如下:

syntax = "proto3";
package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (stream HelloRequest) returns (stream HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

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

package main

import (
	"google.golang.org/grpc"
	"hello/grpc/helloworld"
)

func newConn() helloworld.GreeterClient {
	conn, err := grpc.Dial("localhost:9009", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	return helloworld.NewGreeterClient(conn)
}

func clientWork(client helloworld.GreeterClient) {
	// client work
}

func main() {
	// option 1 - new connection objects for all streams
	for i := 0; i < 1000; i++ {
		clientWork(newConn())
	}

	// option 2 - single connection object for all streams
	globalConn := newConn()
	for i := 0; i < 1000; i++ {
		clientWork(globalConn)
	}
}

选项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 -

syntax = &quot;proto3&quot;;
package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (stream HelloRequest) returns (stream HelloReply) {}
}

// The request message containing the user&#39;s name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}  

Now for my client I have 2 options -

package main

import (
	&quot;google.golang.org/grpc&quot;
	&quot;hello/grpc/helloworld&quot;
)

func newConn() helloworld.GreeterClient {
	conn, err := grpc.Dial(&quot;localhost:9009&quot;, grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	return helloworld.NewGreeterClient(conn)
}

func clientWork(client helloworld.GreeterClient) {
	// client work
}

func main() {
	// option 1 - new connection objects for all streams
	for i:=0; i&lt;1000; i++ {
		clientWork(newConn())
	}
	
	// option 2 - single connection object for all streams
	globalConn := newConn()
	for i:=0; i&lt;1000; i++ {
		clientWork(globalConn)
	}
}

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:

确定