Skip to content

Commit da26ae2

Browse files
committed
refactor: switch to msgpack for message encoding/decoding
- Add `github.com/vmihailenco/msgpack/v5` and `github.com/vmihailenco/tagparser/v2` dependencies to `go.mod` - Remove `unsafe` import from `job.go` - Add `msgpack` tags to `Message` struct fields - Remove `movementSize` constant and its usage - Replace unsafe pointer encoding/decoding with `msgpack` encoding/decoding in `Encode` and `Decode` functions Signed-off-by: appleboy <[email protected]>
1 parent 5b2de1f commit da26ae2

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/appleboy/com v0.2.1
77
github.com/jpillora/backoff v1.0.0
88
github.com/stretchr/testify v1.10.0
9+
github.com/vmihailenco/msgpack/v5 v5.4.1
910
go.uber.org/goleak v1.2.1
1011
go.uber.org/mock v0.5.0
1112
)
@@ -14,5 +15,6 @@ require (
1415
github.com/davecgh/go-spew v1.1.1 // indirect
1516
github.com/kr/text v0.2.0 // indirect
1617
github.com/pmezard/go-difflib v1.0.0 // indirect
18+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
1719
gopkg.in/yaml.v3 v3.0.1 // indirect
1820
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1313
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1414
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
1515
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
16+
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
17+
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
18+
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
19+
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
1620
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
1721
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
1822
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=

job/job.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,56 @@ package job
33
import (
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
1212
type TaskFunc func(context.Context) error
1313

1414
// Message describes a task and its metadata.
1515
type 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
6157
func (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
10197
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
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

Comments
 (0)