dongyun9120 2019-04-22 20:21
浏览 128

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

I'm using PubSub on two different streams where we receive messages from one stream, run some logic, and if it fits certain criteria, we publish it to the second stream. The second stream is also being received from in a goroutine.

Now, I have two main functions HandleMessage and HandleRetry where the former is from the first stream, and the second is for the second stream.

The relevant code for HandleMessage is as follows:

    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")
            }
        }
    }

and in HandleRetry, the function opens with

    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
    }

For the RetryQueue which is handled by HandleRetry -- there is no other input except the messages being published from HandleMessage

But, I keep getting a gob decoding error saying

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 looks like this

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

The encoding and decoding functions are as follows

// 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条回答 默认 最新

  • dpf25323 2019-05-01 16:07
    关注

    In this code in HandleMessage:

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

    The error is ignored and the published message’s data field is populated with a potentially bad value. Check whether you are seeing “failed to convert retry msg to byte slice” in the logs.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据