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