@@ -3,60 +3,56 @@ package job
33import (
44 "context"
55 "time"
6- "unsafe"
76
87 "github.com/golang-queue/queue/core"
8+ "github.com/vmihailenco/msgpack/v5"
99)
1010
1111// TaskFunc is the task function
1212type TaskFunc func (context.Context ) error
1313
1414// Message describes a task and its metadata.
1515type Message struct {
16- Task TaskFunc `json:"-"`
16+ Task TaskFunc `json:"-" msgpack:"-" `
1717
1818 // Timeout is the duration the task can be processed by Handler.
1919 // zero if not specified
2020 // default is 60 time.Minute
21- Timeout time.Duration `json:"timeout"`
21+ Timeout time.Duration `json:"timeout" msgpack:"timeout" `
2222
2323 // Payload is the payload data of the task.
24- Payload []byte `json:"body"`
24+ Payload []byte `json:"body" msgpack:"body" `
2525
2626 // RetryCount set count of retry
2727 // default is 0, no retry.
28- RetryCount int64 `json:"retry_count"`
28+ RetryCount int64 `json:"retry_count" msgpack:"retry_count" `
2929
3030 // RetryDelay set delay between retry
3131 // default is 100ms
32- RetryDelay time.Duration `json:"retry_delay"`
32+ RetryDelay time.Duration `json:"retry_delay" msgpack:"retry_delay" `
3333
3434 // RetryFactor is the multiplying factor for each increment step.
3535 //
3636 // Defaults to 2.
37- RetryFactor float64 `json:"retry_factor"`
37+ RetryFactor float64 `json:"retry_factor" msgpack:"retry_factor" `
3838
3939 // Minimum value of the counter.
4040 //
4141 // Defaults to 100 milliseconds.
42- RetryMin time.Duration `json:"retry_min"`
42+ RetryMin time.Duration `json:"retry_min" msgpack:"retry_min" `
4343
4444 // Maximum value of the counter.
4545 //
4646 // Defaults to 10 seconds.
47- RetryMax time.Duration `json:"retry_max"`
47+ RetryMax time.Duration `json:"retry_max" msgpack:"retry_max" `
4848
4949 // Jitter eases contention by randomizing backoff steps
50- Jitter bool `json:"jitter"`
50+ Jitter bool `json:"jitter" msgpack:"jitter" `
5151
5252 // Data to save Unsafe cast
5353 Data []byte
5454}
5555
56- const (
57- movementSize = int (unsafe .Sizeof (Message {}))
58- )
59-
6056// Bytes get internal data
6157func (m * Message ) Bytes () []byte {
6258 return m .Data
@@ -99,10 +95,21 @@ func NewTask(task TaskFunc, opts ...AllowOption) Message {
9995
10096// Encode for encoding the structure
10197func Encode (m * Message ) []byte {
102- return (* [movementSize ]byte )(unsafe .Pointer (m ))[:]
98+ b , err := msgpack .Marshal (m )
99+ if err != nil {
100+ panic (err )
101+ }
102+
103+ return b
103104}
104105
105106// Decode for decoding the structure
106- func Decode (m []byte ) * Message {
107- return (* Message )(unsafe .Pointer (& m [0 ]))
107+ func Decode (b []byte ) * Message {
108+ var msg Message
109+ err := msgpack .Unmarshal (b , & msg )
110+ if err != nil {
111+ panic (err )
112+ }
113+
114+ return & msg
108115}
0 commit comments