message.go

package nsqd

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "io"
    "time"
)

const (
    MsgIDLength       = 16
    minValidMsgLength = MsgIDLength + 8 + 2 // Timestamp + Attempts
)

type MessageID [MsgIDLength]byte

type Message struct {
    ID        MessageID
    Body      []byte
    Timestamp int64
    Attempts  uint16

    // for in-flight handling
    deliveryTS time.Time
    clientID   int64
    pri        int64
    index      int
    deferred   time.Duration
}

func NewMessage(id MessageID, body []byte) *Message {
    return &Message{
        ID:        id,
        Body:      body,
        Timestamp: time.Now().UnixNano(),
    }
}

func (m *Message) WriteTo(w io.Writer) (int64, error) {
    var buf [10]byte
    var total int64

    binary.BigEndian.PutUint64(buf[:8], uint64(m.Timestamp))
    binary.BigEndian.PutUint16(buf[8:10], uint16(m.Attempts))

    n, err := w.Write(buf[:])
    total += int64(n)
    if err != nil {
        return total, err
    }

    n, err = w.Write(m.ID[:])
    total += int64(n)
    if err != nil {
        return total, err
    }

    n, err = w.Write(m.Body)
    total += int64(n)
    if err != nil {
        return total, err
    }

    return total, nil
}

// decodeMessage deserializes data (as []byte) and creates a new Message
// message format:
// [x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x]...
// |       (int64)        ||    ||      (hex string encoded in ASCII)           || (binary)
// |       8-byte         ||    ||                 16-byte                      || N-byte
// ------------------------------------------------------------------------------------------...
//   nanosecond timestamp    ^^                   message ID                       message body
//                        (uint16)
//                         2-byte
//                        attempts
func decodeMessage(b []byte) (*Message, error) {
    var msg Message

    if len(b) < minValidMsgLength {
        return nil, fmt.Errorf("invalid message buffer size (%d)", len(b))
    }

    msg.Timestamp = int64(binary.BigEndian.Uint64(b[:8]))
    msg.Attempts = binary.BigEndian.Uint16(b[8:10])
    copy(msg.ID[:], b[10:10+MsgIDLength])
    msg.Body = b[10+MsgIDLength:]

    return &msg, nil
}

func writeMessageToBackend(buf *bytes.Buffer, msg *Message, bq BackendQueue) error {
    buf.Reset()
    _, err := msg.WriteTo(buf)
    if err != nil {
        return err
    }
    return bq.Put(buf.Bytes())
}

转载于:https://www.cnblogs.com/zhangboyu/p/7457343.html

goroutine 342 [running]: sync.fatal({0xbdff5b, 0x20}) D:/Program Files (x86)/Go/src/runtime/panic.go:1031 +0x29 sync.(*RWMutex).Unlock(0x19f96d0) D:/Program Files (x86)/Go/src/sync/rwmutex.go:209 +0x57 go-study/models.sendMsg(0x0, {0x135fe0f0, 0x15, 0x15}) D:/go/go-study/models/Message.go:193 +0x70 go-study/models.Chat({0x12fe4500, 0x13bec360}, 0x130d0380) D:/go/go-study/models/Message.go:82 +0x3a0 go-study/service.SendUserMsg(0x13bec360) D:/go/go-study/service/userBasicService.go:237 +0x54 github.com/gin-gonic/gin.(*Context).Next(...) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1(0x13bec360) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/recovery.go:102 +0x89 github.com/gin-gonic/gin.(*Context).Next(...) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 github.com/gin-gonic/gin.LoggerWithConfig.func1(0x13bec360) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/logger.go:240 +0xa7 github.com/gin-gonic/gin.(*Context).Next(...) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0x13be80e0, 0x13bec360) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:620 +0x51b github.com/gin-gonic/gin.(*Engine).ServeHTTP(0x13be80e0, {0xd04140, 0x13c060a0}, 0x130d0380) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:576 +0x1c9 net/http.serverHandler.ServeHTTP({0x13d46000}, {0xd04140, 0x13c060a0}, 0x130d0380) D:/Program Files (x86)/Go/src/net/http/server.go:2947 +0x285 net/http.(*conn).serve(0x132d8360, {0xd048a0, 0x132f2738}) D:/Program Files (x86)/Go/src/net/http/server.go:1991 +0x67d created by net/http.(*Server).Serve D:/Program Files (x86)/Go/src/net/http/server.go:3102 +0x498
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值