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
diff --git a/internal/trace.go b/internal/trace.go
index 321552f..e4a8cdd 100644
--- a/internal/trace.go
+++ b/internal/trace.go
@@ -452,7 +452,7 @@
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 0d96454..b3d6479 100644
--- a/primitive/result.go
+++ b/primitive/result.go
@@ -50,6 +50,10 @@
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 7c5e0eb..7b33960 100644
--- a/producer/producer.go
+++ b/producer/producer.go
@@ -145,7 +145,7 @@
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 @@
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 @@
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
}