go-zero
go-zero copied to clipboard
How can i set grpc message size
in rpc
s := zrpc.MustNewServer(c.RpcServerConf,
func(grpcServer *grpc.Server) {
//grpcServer.SetOption(grpc.MaxSendMsgSize(maxMessageSize))
userCenterClient.RegisterUserServiceServer(grpcServer, userserviceServer.NewUserServiceServer(ctx))
userCenterClient.RegisterShopServiceServer(grpcServer, shopserviceServer.NewShopServiceServer(ctx))
userCenterClient.RegisterSubmitTaskTempServiceServer(grpcServer, submittasktempserviceServer.NewSubmitTaskTempServiceServer(ctx))
userCenterClient.RegisterSubmitTaskServiceServer(grpcServer, submittaskserviceServer.NewSubmitTaskServiceServer(ctx))
userCenterClient.RegisterShopGoodsLibraryServiceServer(grpcServer, shopgoodslibraryserviceServer.NewShopGoodsLibraryServiceServer(ctx))
userCenterClient.RegisterFilterFilesServiceServer(grpcServer, filterfilesserviceServer.NewFilterFilesServiceServer(ctx))
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
//s.AddOptions(serverOpts...)
s.AddOptions(grpc.MaxRecvMsgSize(maxMessageSize), grpc.MaxSendMsgSize(maxMessageSize))
defer s.Stop() rpc error: code = ResourceExhausted desc = grpc: received message larger than max (10591746 vs. 4194304)
Set the option in both server and client sides.
How To Set the option in both server and client sides?would you please give an example?
I see you're trying to set the gRPC message size limits in go-zero. The error you're getting (grpc: received message larger than max (10591746 vs. 4194304)) indicates that your message size exceeds the default 4MB limit.
You're on the right track with using MaxRecvMsgSize and MaxSendMsgSize, but there are a few ways to set these options in go-zero:
- Using
AddOptionsafter server creation (as you're doing):
s := zrpc.MustNewServer(c.RpcServerConf, ...)
s.AddOptions(
grpc.MaxRecvMsgSize(maxMessageSize),
grpc.MaxSendMsgSize(maxMessageSize)
)
- Alternatively, you can set these options directly in your RpcServerConf configuration:
type Config struct {
zrpc.RpcServerConf
}
c.RpcServerConf.MaxMessageSize = 20 * 1024 * 1024 // Set to 20MB for example
A few important notes:
- Make sure to set an appropriate value for
maxMessageSizethat meets your needs but doesn't consume too much memory - Both client and server need to be configured with compatible message size limits
- Consider if you really need such large messages, as they might impact performance
For reference, here's a complete example:
const maxMessageSize = 20 * 1024 * 1024 // 20MB
s := zrpc.MustNewServer(c.RpcServerConf,
func(grpcServer *grpc.Server) {
// Your service registrations...
userCenterClient.RegisterUserServiceServer(grpcServer, userserviceServer.NewUserServiceServer(ctx))
// ... other registrations ...
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
// Set both MaxRecvMsgSize and MaxSendMsgSize
s.AddOptions(
grpc.MaxRecvMsgSize(maxMessageSize),
grpc.MaxSendMsgSize(maxMessageSize)
)
This should resolve your "ResourceExhausted" error while maintaining the rest of your server configuration.
how to set msg size at client side? example only shows the way on server