Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

producer check msg body #1142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
8 changes: 8 additions & 0 deletions producer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,6 +52,7 @@ type producerOptions struct {
CompressMsgBodyOverHowmuch int
CompressLevel int
TraceDispatcher internal.TraceDispatcher
MaxMessageSize int
}

type Option func(*producerOptions)
Expand Down Expand Up @@ -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
}
}
8 changes: 8 additions & 0 deletions producer/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 65 additions & 1 deletion producer/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}