go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

How can i set grpc message size

Open dyjfighting opened this issue 1 year ago • 4 comments
trafficstars

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)

dyjfighting avatar Jun 16 '24 03:06 dyjfighting

Set the option in both server and client sides.

kevwan avatar Jun 22 '24 11:06 kevwan

How To Set the option in both server and client sides?would you please give an example?

liuwen766 avatar Nov 25 '24 09:11 liuwen766

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:

  1. Using AddOptions after server creation (as you're doing):
s := zrpc.MustNewServer(c.RpcServerConf, ...)
s.AddOptions(
    grpc.MaxRecvMsgSize(maxMessageSize),
    grpc.MaxSendMsgSize(maxMessageSize)
)
  1. 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 maxMessageSize that 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.

kevwan avatar Jan 28 '25 08:01 kevwan

how to set msg size at client side? example only shows the way on server

KirigiriSuzumiya avatar Jun 07 '25 00:06 KirigiriSuzumiya