球:类型不匹配:没有字段与编译解码器匹配-Golang

时间:2019-04-22 20:21:26

标签: go google-cloud-pubsub

我在两个不同的流上使用PubSub,我们从一个流中接收消息,运行一些逻辑,并且如果符合某些条件,我们将其发布到第二个流。还在goroutine中接收第二个流。

现在,我有两个主要功能HandleMessageHandleRetry,其中前一个来自第一个流,第二个用于第二个流。

HandleMessage的相关代码如下:

    if c.handler.ShouldProcess(tx) {
        err := c.handler.Process(tx)
        if err != nil {
            c.log.
                WithError(err).
                WithField("tx_hash", tx.TxHash.String()).
                Error("failed to process")

            retryMsg := RetryMessage{
                Transaction:                 tx,
                RemainingProcessingAttempts: c.config.MaxProcessingAttempts,
                LastAttempt:                 time.Now(),
            }

            data, err := pubsub.EncodeMessage(retryMsg)
            if err != nil {
                c.log.WithError(err).Error("failed to convert retry msg to byte slice")
            }

            id, err := c.retryQueue.Publish(context.Background(), &pubsub.Message{Data: data})
            if err != nil {
                c.log.WithError(err).
                    WithField("id", id).
                    Error("failed to publish message to retry queue")
            }
        }
    }

HandleRetry中,该函数以

打开
    retryTx := new(RetryMessage)
    err := pubsub.DecodeMessage(msg.Data, retryTx)
    if err != nil {
        c.log.WithError(err).
            Error("failed to decode message: not a retry tx")
        msg.Ack()
        return
    }

对于由RetryQueue处理的HandleRetry来说,除了从HandleMessage发布的消息外,没有其他输入

但是,我不断收到gob解码错误,说

level=error msg="failed to decode message: not a retry tx" env=LOCAL error="gob: type mismatch: no fields matched compiling decoder for RetryMessage"

RetryMessage看起来像这样

type RetryMessage struct {
    Transaction                 *firehose.Transaction
    RemainingProcessingAttempts int
    LastAttempt                 time.Time
}

编码和解码功能如下

// EncodeMessage convert an arbitrary interface into a byte slice.
func EncodeMessage(data interface{}) ([]byte, error) {
    var buf bytes.Buffer

    enc := gob.NewEncoder(&buf)

    err := enc.Encode(data)
    if err != nil {
        return nil, err
    }

    return buf.Bytes(), nil
}

// DecodeMessage decodes message data into the provided interface.
func DecodeMessage(data []byte, dest interface{}) error {
    buf := bytes.NewBuffer(data)
    dec := gob.NewDecoder(buf)
    return dec.Decode(dest)
}

1 个答案:

答案 0 :(得分:0)

HandleMessage中的此代码中:

        data, err := pubsub.EncodeMessage(retryMsg)
        if err != nil {
            c.log.WithError(err).Error("failed to convert retry msg to byte slice")
        }

该错误将被忽略,并且已发布的邮件的data字段中填充了一个潜在的错误值。检查日志中是否显示“无法将重试msg转换为字节片”。

相关问题