grpc 使用壓縮器 compressor wsarecv: An existing connection was forcibly closed by the remote host.


 wsarecv: An existing connection was forcibly closed by the remote host.

 

優化方案:

1)客戶端連接復用

2)壓縮傳輸數據

 

rpc error: code = Unimplemented desc = grpc: Decompressor is not installed for grpc-encoding "gzip"
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x28 pc=0x9c0a1a]

 抓包分析,壓縮前后數據字節數對比

 

 

// NewGRPCServer new a gRPC server.
func NewGRPCServer() *grpc.Server {
var opts = []grpc.ServerOption{}
/*
rpc error: code = Unimplemented desc = grpc: Decompressor is not installed for grpc-encoding "gzip"
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x28 pc=0x9c0a1a]
*/
//grpc.UseCompressor(gzip.Name)

//encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))
srv := grpc.NewServer(opts...)
pb.RegisterReportCenterServer(srv, &server{})
return srv
}

 

func NewClient() (c pb.ReportCenterClient, conn *grpc.ClientConn) {
bytes := 1024 * 1024 * 4 * 4
cp := grpc.ConnectParams{}
cp.MinConnectTimeout = 16 * time.Second
co := []grpc.CallOption{grpc.UseCompressor(gzip.Name)}
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(bytes)),
grpc.WithConnectParams(cp),
grpc.WithDefaultCallOptions(co...),
}
//encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))

conn, err := grpc.Dial("120.24.184.164:12345", opts...)
if err != nil {
fmt.Println("err-NewClient", err)
//log.SugarLogger.Errorf("grpc", err)
return
}
return pb.NewReportCenterClient(conn), conn
}


encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))
1)
Svr
encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))
Cli
encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))
當發送的字節數多時,少時可以正常收發
rpc error: code = Unavailable desc = error reading from server: read tcp 10.128.1.192:49347->10.4.84.14:12345: wsarecv: An existing connection was forcibly closed by the remote host.

修改Cli,添加CallOption即可
co := []grpc.CallOption{grpc.UseCompressor(gzip.Name)}
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(bytes)),
grpc.WithConnectParams(cp),
grpc.WithDefaultCallOptions(co...),
}
之后修改Svr
encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))

grpc.UseCompressor(gzip.Name)
均可
2)
Cli
encoding.RegisterCompressor(encoding.GetCompressor(gzip.Name))
Svr
grpc.UseCompressor(gzip.Name)
也報錯

3)Cli

//co := []grpc.CallOption{grpc.UseCompressor(gzip.Name)}
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(bytes)),
grpc.WithConnectParams(cp),
//grpc.WithDefaultCallOptions(co...),
}
grpc.UseCompressor(gzip.Name)

這樣寫,也報錯

以上版本
google.golang.org/grpc v1.43.0
google.golang.org/protobuf v1.27.1

 

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM