diff --git a/errors/errors.go b/errors/errors.go index 28995068..1e233994 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -49,4 +49,5 @@ var ( ErrPullConsumer = errors.New("pull consumer has not supported") ErrProducerCreated = errors.New("producer group has been created") ErrMultipleTopics = errors.New("the topic of the messages in one batch should be the same") + ErrMessageIllegal = errors.New("the message is illegal") ) diff --git a/producer/option.go b/producer/option.go index 72af3c6a..cd43a389 100644 --- a/producer/option.go +++ b/producer/option.go @@ -35,6 +35,7 @@ func defaultProducerOptions() producerOptions { Resolver: primitive.NewHttpResolver("DEFAULT"), CompressMsgBodyOverHowmuch: 4096, CompressLevel: 5, + MaxMessageSize: 1024 * 1024 * 4, // 4M } opts.ClientOptions.GroupName = "DEFAULT_PRODUCER" return opts @@ -51,6 +52,7 @@ type producerOptions struct { CompressMsgBodyOverHowmuch int CompressLevel int TraceDispatcher internal.TraceDispatcher + MaxMessageSize int } type Option func(*producerOptions) @@ -193,3 +195,9 @@ func WithTls(useTls bool) Option { opts.ClientOptions.RemotingClientConfig.UseTls = useTls } } + +func WithMaxMessageSize(maxMessageSize int) Option { + return func(opts *producerOptions) { + opts.MaxMessageSize = maxMessageSize + } +} diff --git a/producer/producer.go b/producer/producer.go index eb3cd2e9..b3570c4b 100644 --- a/producer/producer.go +++ b/producer/producer.go @@ -128,6 +128,14 @@ func (p *defaultProducer) checkMsg(msgs ...*primitive.Message) error { if msg.Topic != topic { return errors2.ErrMultipleTopics } + + if msg.Body == nil || len(msg.Body) == 0 { + return errors2.ErrMessageIllegal + } + + if len(msg.Body) > p.options.MaxMessageSize { + return errors2.ErrMessageIllegal + } } return nil diff --git a/producer/producer_test.go b/producer/producer_test.go index cbe74266..b36637c2 100644 --- a/producer/producer_test.go +++ b/producer/producer_test.go @@ -19,9 +19,11 @@ package producer import ( "context" - "github.com/apache/rocketmq-client-go/v2/errors" "testing" + "github.com/apache/rocketmq-client-go/v2/errors" + errors2 "github.com/apache/rocketmq-client-go/v2/errors" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" @@ -364,3 +366,65 @@ func TestBatchSendDifferentTopics(t *testing.T) { assert.NotNil(t, err) assert.Equal(t, err, errors.ErrMultipleTopics) } + +func TestCheckMessageIllgal(t *testing.T) { + p, _ := NewDefaultProducer( + WithNsResolver(primitive.NewPassthroughResolver([]string{"127.0.0.1:9876"})), + WithRetry(2), + WithQueueSelector(NewManualQueueSelector()), + WithMaxMessageSize(100), + ) + p.state = int32(internal.StateRunning) + + // test msg body is null + msg := &primitive.Message{ + Topic: topic, + Body: nil, + Queue: &primitive.MessageQueue{ + Topic: topic, + BrokerName: "aa", + QueueId: 0, + }, + } + err := p.checkMsg(msg) + assert.Equal(t, errors2.ErrMessageIllegal, err) + + // test msg body length empty + msg = &primitive.Message{ + Topic: topic, + Body: []byte{}, + Queue: &primitive.MessageQueue{ + Topic: topic, + BrokerName: "aa", + QueueId: 0, + }, + } + err = p.checkMsg(msg) + assert.Equal(t, errors2.ErrMessageIllegal, err) + + // test msg body length valid + msg = &primitive.Message{ + Topic: topic, + Body: make([]byte, 50), + Queue: &primitive.MessageQueue{ + Topic: topic, + BrokerName: "aa", + QueueId: 0, + }, + } + err = p.checkMsg(msg) + assert.Nil(t, err) + + // test msg body length invalid + msg = &primitive.Message{ + Topic: topic, + Body: make([]byte, 101), + Queue: &primitive.MessageQueue{ + Topic: topic, + BrokerName: "aa", + QueueId: 0, + }, + } + err = p.checkMsg(msg) + assert.Equal(t, errors2.ErrMessageIllegal, err) +}