Skip to content

Commit dcbcb6a

Browse files
committed
feat: optimize message handling and improve tests
- Change message references to pointers in various functions - Add dependency: github.com/appleboy/com v0.1.7 - Add new benchmark test: BenchmarkNewMessage - Update string to byte conversion using bytesconv.StrToBytes from appleboy/com package - Update comments in queue.go for better clarity Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent 0340628 commit dcbcb6a

8 files changed

+35
-14
lines changed

benchmark_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func testQueue(b *testing.B, pool testqueue) {
3131
b.ResetTimer()
3232
for n := 0; n < b.N; n++ {
3333
for i := 0; i < count; i++ {
34-
_ = pool.Queue(message)
34+
_ = pool.Queue(&message)
3535
_, _ = pool.Request()
3636
}
3737
}
@@ -60,7 +60,7 @@ func BenchmarkQueueTask(b *testing.B) {
6060
})
6161

6262
for n := 0; n < b.N; n++ {
63-
if err := q.queue(m); err != nil {
63+
if err := q.queue(&m); err != nil {
6464
b.Fatal(err)
6565
}
6666
}
@@ -81,7 +81,7 @@ func BenchmarkQueue(b *testing.B) {
8181
m.Encode()
8282

8383
for n := 0; n < b.N; n++ {
84-
if err := q.queue(m); err != nil {
84+
if err := q.queue(&m); err != nil {
8585
b.Fatal(err)
8686
}
8787
}
@@ -113,7 +113,7 @@ func BenchmarkQueue(b *testing.B) {
113113
func BenchmarkRingWithTask(b *testing.B) {
114114
b.ReportAllocs()
115115

116-
task := &job.Message{
116+
task := job.Message{
117117
Timeout: 100 * time.Millisecond,
118118
Task: func(_ context.Context) error {
119119
return nil
@@ -131,6 +131,6 @@ func BenchmarkRingWithTask(b *testing.B) {
131131
)
132132

133133
for n := 0; n < b.N; n++ {
134-
_ = q.run(task)
134+
_ = q.run(&task)
135135
}
136136
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/golang-queue/queue
33
go 1.18
44

55
require (
6+
github.com/appleboy/com v0.1.7
67
github.com/golang/mock v1.6.0
78
github.com/jpillora/backoff v1.0.0
89
github.com/stretchr/testify v1.8.4

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/appleboy/com v0.1.7 h1:4lYTFNoMAAXGGIC8lDxVg/NY+1aXbYqfAWN05cZhd0M=
2+
github.com/appleboy/com v0.1.7/go.mod h1:JUK+oH0SXCLRH57pDMJx6VWVsm8CPdajalmRSWwamBE=
13
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

job/benchmark_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ func BenchmarkNewTask(b *testing.B) {
2121
}
2222
}
2323

24+
func BenchmarkNewMessage(b *testing.B) {
25+
b.ReportAllocs()
26+
for i := 0; i < b.N; i++ {
27+
_ = NewMessage(mockMessage{
28+
message: "foo",
29+
},
30+
AllowOption{
31+
RetryCount: Int64(100),
32+
RetryDelay: Time(30 * time.Millisecond),
33+
Timeout: Time(3 * time.Millisecond),
34+
},
35+
)
36+
}
37+
}
38+
2439
func BenchmarkNewOption(b *testing.B) {
2540
b.ReportAllocs()
2641
for i := 0; i < b.N; i++ {

job/job.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func (m *Message) Encode() {
6868
}
6969

7070
// NewMessage create new message
71-
func NewMessage(m core.QueuedMessage, opts ...AllowOption) *Message {
71+
func NewMessage(m core.QueuedMessage, opts ...AllowOption) Message {
7272
o := NewOptions(opts...)
7373

74-
return &Message{
74+
return Message{
7575
RetryCount: o.retryCount,
7676
RetryDelay: o.retryDelay,
7777
RetryFactor: o.retryFactor,
@@ -83,10 +83,10 @@ func NewMessage(m core.QueuedMessage, opts ...AllowOption) *Message {
8383
}
8484

8585
// NewTask create new task
86-
func NewTask(task TaskFunc, opts ...AllowOption) *Message {
86+
func NewTask(task TaskFunc, opts ...AllowOption) Message {
8787
o := NewOptions(opts...)
8888

89-
return &Message{
89+
return Message{
9090
Timeout: o.timeout,
9191
RetryCount: o.retryCount,
9292
RetryDelay: o.retryDelay,

job/job_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/appleboy/com/bytesconv"
78
"github.com/stretchr/testify/assert"
89
)
910

@@ -12,7 +13,7 @@ type mockMessage struct {
1213
}
1314

1415
func (m mockMessage) Bytes() []byte {
15-
return []byte(m.message)
16+
return bytesconv.StrToBytes(m.message)
1617
}
1718

1819
func TestMessageEncodeDecode(t *testing.T) {

queue.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ func (q *Queue) Queue(message core.QueuedMessage, opts ...job.AllowOption) error
119119
data := job.NewMessage(message, opts...)
120120
data.Encode()
121121

122-
return q.queue(data)
122+
return q.queue(&data)
123123
}
124124

125125
// QueueTask to queue single task
126126
func (q *Queue) QueueTask(task job.TaskFunc, opts ...job.AllowOption) error {
127127
data := job.NewTask(task, opts...)
128-
return q.queue(data)
128+
return q.queue(&data)
129129
}
130130

131131
func (q *Queue) queue(m *job.Message) error {
@@ -266,6 +266,7 @@ func (q *Queue) UpdateWorkerCount(num int) {
266266
q.schedule()
267267
}
268268

269+
// schedule to check worker number
269270
func (q *Queue) schedule() {
270271
q.Lock()
271272
defer q.Unlock()
@@ -279,7 +280,7 @@ func (q *Queue) schedule() {
279280
}
280281
}
281282

282-
// start handle job
283+
// start to start all worker
283284
func (q *Queue) start() {
284285
tasks := make(chan core.QueuedMessage, 1)
285286

queue_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/appleboy/com/bytesconv"
910
"github.com/golang-queue/queue/core"
1011
"github.com/golang-queue/queue/job"
1112
"github.com/golang-queue/queue/mocks"
@@ -24,7 +25,7 @@ type mockMessage struct {
2425
}
2526

2627
func (m mockMessage) Bytes() []byte {
27-
return []byte(m.message)
28+
return bytesconv.StrToBytes(m.message)
2829
}
2930

3031
func TestNewQueueWithZeroWorker(t *testing.T) {

0 commit comments

Comments
 (0)