@@ -3,60 +3,56 @@ package job
3
3
import (
4
4
"context"
5
5
"time"
6
- "unsafe"
7
6
8
7
"github.com/golang-queue/queue/core"
8
+ "github.com/vmihailenco/msgpack/v5"
9
9
)
10
10
11
11
// TaskFunc is the task function
12
12
type TaskFunc func (context.Context ) error
13
13
14
14
// Message describes a task and its metadata.
15
15
type Message struct {
16
- Task TaskFunc `json:"-"`
16
+ Task TaskFunc `json:"-" msgpack:"-" `
17
17
18
18
// Timeout is the duration the task can be processed by Handler.
19
19
// zero if not specified
20
20
// default is 60 time.Minute
21
- Timeout time.Duration `json:"timeout"`
21
+ Timeout time.Duration `json:"timeout" msgpack:"timeout" `
22
22
23
23
// Payload is the payload data of the task.
24
- Payload []byte `json:"body"`
24
+ Payload []byte `json:"body" msgpack:"body" `
25
25
26
26
// RetryCount set count of retry
27
27
// default is 0, no retry.
28
- RetryCount int64 `json:"retry_count"`
28
+ RetryCount int64 `json:"retry_count" msgpack:"retry_count" `
29
29
30
30
// RetryDelay set delay between retry
31
31
// default is 100ms
32
- RetryDelay time.Duration `json:"retry_delay"`
32
+ RetryDelay time.Duration `json:"retry_delay" msgpack:"retry_delay" `
33
33
34
34
// RetryFactor is the multiplying factor for each increment step.
35
35
//
36
36
// Defaults to 2.
37
- RetryFactor float64 `json:"retry_factor"`
37
+ RetryFactor float64 `json:"retry_factor" msgpack:"retry_factor" `
38
38
39
39
// Minimum value of the counter.
40
40
//
41
41
// Defaults to 100 milliseconds.
42
- RetryMin time.Duration `json:"retry_min"`
42
+ RetryMin time.Duration `json:"retry_min" msgpack:"retry_min" `
43
43
44
44
// Maximum value of the counter.
45
45
//
46
46
// Defaults to 10 seconds.
47
- RetryMax time.Duration `json:"retry_max"`
47
+ RetryMax time.Duration `json:"retry_max" msgpack:"retry_max" `
48
48
49
49
// Jitter eases contention by randomizing backoff steps
50
- Jitter bool `json:"jitter"`
50
+ Jitter bool `json:"jitter" msgpack:"jitter" `
51
51
52
52
// Data to save Unsafe cast
53
53
Data []byte
54
54
}
55
55
56
- const (
57
- movementSize = int (unsafe .Sizeof (Message {}))
58
- )
59
-
60
56
// Bytes get internal data
61
57
func (m * Message ) Bytes () []byte {
62
58
return m .Data
@@ -99,10 +95,21 @@ func NewTask(task TaskFunc, opts ...AllowOption) Message {
99
95
100
96
// Encode for encoding the structure
101
97
func 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
103
104
}
104
105
105
106
// 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
108
115
}
0 commit comments