From 1fb7d33c3d878c96cb0a99ef3b1ddffa56e95d10 Mon Sep 17 00:00:00 2001 From: linxin Date: Tue, 2 Jun 2020 15:46:53 +0800 Subject: [PATCH] set default of SendResult.Status and fix panic (#495) * 1. send the default value of SendResult.Status to SendUnknownError 2. fix panic: select queue with nil --- internal/trace.go | 2 +- primitive/result.go | 4 ++++ producer/producer.go | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/trace.go b/internal/trace.go index 321552fe..e4a8cdd8 100644 --- a/internal/trace.go +++ b/internal/trace.go @@ -452,7 +452,7 @@ func (td *traceDispatcher) sendTraceDataByMQ(keySet Keyset, regionID string, dat var req = td.buildSendRequest(mq, msg) ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) err := td.cli.InvokeAsync(ctx, addr, req, func(command *remote.RemotingCommand, e error) { - resp := new(primitive.SendResult) + resp := primitive.NewSendResult() if e != nil { rlog.Info("send trace data error.", map[string]interface{}{ "traceData": data, diff --git a/primitive/result.go b/primitive/result.go index 0d964545..b3d6479d 100644 --- a/primitive/result.go +++ b/primitive/result.go @@ -50,6 +50,10 @@ type SendResult struct { TraceOn bool } +func NewSendResult() *SendResult { + return &SendResult{Status: SendUnknownError} +} + // SendResult send message result to string(detail result) func (result *SendResult) String() string { return fmt.Sprintf("SendResult [sendStatus=%d, msgIds=%s, offsetMsgId=%s, queueOffset=%d, messageQueue=%s]", diff --git a/producer/producer.go b/producer/producer.go index 7c5e0ebc..7b339608 100644 --- a/producer/producer.go +++ b/producer/producer.go @@ -145,7 +145,7 @@ func (p *defaultProducer) SendSync(ctx context.Context, msgs ...*primitive.Messa msg := p.encodeBatch(msgs...) - resp := new(primitive.SendResult) + resp := primitive.NewSendResult() if p.interceptor != nil { primitive.WithMethod(ctx, primitive.SendSync) producerCtx := &primitive.ProducerCtx{ @@ -245,7 +245,7 @@ func (p *defaultProducer) sendAsync(ctx context.Context, msg *primitive.Message, ctx, _ = context.WithTimeout(ctx, 3*time.Second) return p.client.InvokeAsync(ctx, addr, p.buildSendRequest(mq, msg), func(command *remote.RemotingCommand, err error) { - resp := new(primitive.SendResult) + resp := primitive.NewSendResult() if err != nil { h(ctx, nil, err) } else { @@ -366,7 +366,7 @@ func (p *defaultProducer) selectMessageQueue(msg *primitive.Message) *primitive. return nil } - if result.MqList != nil && len(result.MqList) <= 0 { + if len(result.MqList) <= 0 { rlog.Error("can not find proper message queue", nil) return nil }