4 Star 6 Fork 2

Gitee 极速下载 / taskq

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/vmihailenco/taskq
克隆/下载
message.go 1.97 KB
一键复制 编辑 原始数据 按行查看 历史
Vladimir Mihailenco 提交于 2018-08-24 13:52 . Upgrade to latest msgpack
package msgqueue
import (
"bytes"
"errors"
"fmt"
"hash/fnv"
"time"
"github.com/go-msgqueue/msgqueue/internal"
"github.com/vmihailenco/msgpack"
)
// ErrDuplicate is returned when adding duplicate message to the queue.
var ErrDuplicate = errors.New("queue: message with such name already exists")
// Message is used to create and retrieve messages from a queue.
type Message struct {
// SQS/IronMQ message id.
Id string
// Optional name for the message. Messages with the same name
// are processed only once.
Name string
// Delay specifies the duration the queue must wait
// before executing the message.
Delay time.Duration
// Function args passed to the handler.
Args []interface{}
// Text representation of the Args.
Body string
// SQS/IronMQ reservation id that is used to release/delete the message..
ReservationId string
// The number of times the message has been reserved or released.
ReservedCount int
Err error
}
func NewMessage(args ...interface{}) *Message {
return &Message{
Args: args,
}
}
func (m *Message) String() string {
return fmt.Sprintf("Message<Id=%q Name=%q ReservedCount=%d>",
m.Id, m.Name, m.ReservedCount)
}
// SetDelayName sets delay and generates message name from the args.
func (m *Message) SetDelayName(delay time.Duration, args ...interface{}) {
h := hashArgs(append(args, delay, timeSlot(delay)))
m.Name = string(h)
m.Delay = delay + 5*time.Second
}
func (m *Message) EncodeBody(compress bool) (string, error) {
if m.Body != "" {
return m.Body, nil
}
s, err := internal.EncodeArgs(m.Args, compress)
if err != nil {
return "", err
}
m.Body = s
return s, nil
}
func timeSlot(period time.Duration) int64 {
if period <= 0 {
return 0
}
return time.Now().UnixNano() / int64(period)
}
func hashArgs(args []interface{}) []byte {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
_ = enc.EncodeMulti(args...)
b := buf.Bytes()
if len(b) <= 64 {
return b
}
h := fnv.New128a()
_, _ = h.Write(b)
return h.Sum(nil)
}
1
https://gitee.com/mirrors/taskq.git
git@gitee.com:mirrors/taskq.git
mirrors
taskq
taskq
v1.8.2

搜索帮助