Skip to content

Commit 8416a18

Browse files
authored
chore: update example (#34)
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 278ee81 commit 8416a18

File tree

6 files changed

+54
-82
lines changed

6 files changed

+54
-82
lines changed

_example/example01/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module example
33
go 1.16
44

55
require (
6-
github.com/golang-queue/queue v0.0.7-0.20210820051048-3cc52a575c1c // indirect
6+
github.com/golang-queue/queue v0.0.7-0.20210820234255-278ee8112584
77
github.com/golang/protobuf v1.5.2 // indirect
88
github.com/nats-io/nats-server/v2 v2.3.2 // indirect
99
github.com/nsqio/go-nsq v1.0.8 // indirect

_example/example01/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
12
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/golang-queue/queue v0.0.6 h1:TLd0lSM7uNgXXj7SXSMfyaZUwRgbOu9tq6UfZXXELnA=
34
github.com/golang-queue/queue v0.0.6/go.mod h1:IeIGBO1largDrFEaxDgIckoAFIUTn0eolTQris8bm08=
45
github.com/golang-queue/queue v0.0.7-0.20210820051048-3cc52a575c1c h1:RX+BTPGEITf2Jy/B1X5Qu+l8yGdGR3Z/Cx/9IM3MffI=
56
github.com/golang-queue/queue v0.0.7-0.20210820051048-3cc52a575c1c/go.mod h1:JS5tYJacahCjafcplU5idNLX2vkYioqh6wEDX5o9Nms=
7+
github.com/golang-queue/queue v0.0.7-0.20210820234255-278ee8112584 h1:dI8egsUCqUwlw3/Z65Eo2oIS2sPK+k42pa+csLKmRLk=
8+
github.com/golang-queue/queue v0.0.7-0.20210820234255-278ee8112584/go.mod h1:JS5tYJacahCjafcplU5idNLX2vkYioqh6wEDX5o9Nms=
69
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
710
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
811
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
@@ -26,8 +29,10 @@ github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1t
2629
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
2730
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
2831
github.com/nsqio/go-nsq v1.0.8/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY=
32+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2933
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3034
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
35+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3136
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3237
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3338
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@@ -56,4 +61,5 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
5661
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
5762
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
5863
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
64+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
5965
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

_example/example01/main.go

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,37 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
7-
"log"
86
"time"
97

108
"github.com/golang-queue/queue"
119
)
1210

13-
type job struct {
14-
Name string
15-
Message string
16-
}
17-
18-
func (j *job) Bytes() []byte {
19-
b, err := json.Marshal(j)
20-
if err != nil {
21-
panic(err)
22-
}
23-
return b
24-
}
25-
2611
func main() {
2712
taskN := 100
2813
rets := make(chan string, taskN)
2914

30-
// define the worker
31-
w := queue.NewConsumer(
32-
queue.WithQueueSize(taskN),
33-
queue.WithFn(func(ctx context.Context, m queue.QueuedMessage) error {
34-
v, ok := m.(*job)
35-
if !ok {
36-
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
37-
return err
38-
}
39-
}
40-
41-
rets <- "Hi, " + v.Name + ", " + v.Message
42-
return nil
43-
}),
44-
)
45-
46-
// define the queue
47-
q, err := queue.NewQueue(
48-
queue.WithWorkerCount(5),
49-
queue.WithWorker(w),
50-
)
51-
if err != nil {
52-
log.Fatal(err)
53-
}
54-
55-
// start the five worker
56-
q.Start()
15+
// initial queue pool
16+
q := queue.NewPool(5)
17+
// shutdown the service and notify all the worker
18+
// wait all jobs are complete.
19+
defer q.Release()
5720

5821
// assign tasks in queue
5922
for i := 0; i < taskN; i++ {
6023
go func(i int) {
61-
q.Queue(&job{
62-
Name: "foobar",
63-
Message: fmt.Sprintf("handle the job: %d", i+1),
64-
})
24+
if err := q.QueueTask(func(ctx context.Context) error {
25+
rets <- fmt.Sprintf("Hi Gopher, handle the job: %02d", +i)
26+
return nil
27+
}); err != nil {
28+
panic(err)
29+
}
6530
}(i)
6631
}
6732

6833
// wait until all tasks done
6934
for i := 0; i < taskN; i++ {
7035
fmt.Println("message:", <-rets)
71-
time.Sleep(50 * time.Millisecond)
36+
time.Sleep(20 * time.Millisecond)
7237
}
73-
74-
// shutdown the service and notify all the worker
75-
q.Shutdown()
76-
// wait all jobs are complete.
77-
q.Wait()
7838
}

_example/example02/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module example
33
go 1.16
44

55
require (
6-
github.com/golang-queue/queue v0.0.7-0.20210820051048-3cc52a575c1c
6+
github.com/golang-queue/queue v0.0.7-0.20210820234255-278ee8112584 // indirect
77
github.com/golang/protobuf v1.5.2 // indirect
88
github.com/nats-io/nats-server/v2 v2.3.2 // indirect
99
github.com/nsqio/go-nsq v1.0.8 // indirect

_example/example02/go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
21
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
32
github.com/golang-queue/queue v0.0.6 h1:TLd0lSM7uNgXXj7SXSMfyaZUwRgbOu9tq6UfZXXELnA=
43
github.com/golang-queue/queue v0.0.6/go.mod h1:IeIGBO1largDrFEaxDgIckoAFIUTn0eolTQris8bm08=
54
github.com/golang-queue/queue v0.0.7-0.20210820051048-3cc52a575c1c h1:RX+BTPGEITf2Jy/B1X5Qu+l8yGdGR3Z/Cx/9IM3MffI=
65
github.com/golang-queue/queue v0.0.7-0.20210820051048-3cc52a575c1c/go.mod h1:JS5tYJacahCjafcplU5idNLX2vkYioqh6wEDX5o9Nms=
6+
github.com/golang-queue/queue v0.0.7-0.20210820234255-278ee8112584 h1:dI8egsUCqUwlw3/Z65Eo2oIS2sPK+k42pa+csLKmRLk=
7+
github.com/golang-queue/queue v0.0.7-0.20210820234255-278ee8112584/go.mod h1:JS5tYJacahCjafcplU5idNLX2vkYioqh6wEDX5o9Nms=
78
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
89
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
910
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
@@ -27,10 +28,8 @@ github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1t
2728
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
2829
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
2930
github.com/nsqio/go-nsq v1.0.8/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY=
30-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3131
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3232
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3433
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3534
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3635
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@@ -59,5 +58,4 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
5958
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
6059
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
6160
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
62-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
6361
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

_example/example02/main.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,55 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"log"
78
"time"
89

910
"github.com/golang-queue/queue"
1011
)
1112

13+
type job struct {
14+
Name string
15+
Message string
16+
}
17+
18+
func (j *job) Bytes() []byte {
19+
b, err := json.Marshal(j)
20+
if err != nil {
21+
panic(err)
22+
}
23+
return b
24+
}
25+
1226
func main() {
1327
taskN := 100
1428
rets := make(chan string, taskN)
1529

16-
// define the worker
17-
w := queue.NewConsumer(
18-
queue.WithQueueSize(taskN),
19-
)
20-
21-
// define the queue
22-
q, err := queue.NewQueue(
23-
queue.WithWorkerCount(5),
24-
queue.WithWorker(w),
25-
)
26-
if err != nil {
27-
log.Fatal(err)
28-
}
30+
// initial queue pool
31+
q := queue.NewPool(5, queue.WithFn(func(ctx context.Context, m queue.QueuedMessage) error {
32+
v, ok := m.(*job)
33+
if !ok {
34+
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
35+
return err
36+
}
37+
}
2938

30-
// start the five worker
31-
q.Start()
39+
rets <- "Hi, " + v.Name + ", " + v.Message
40+
return nil
41+
}))
42+
// shutdown the service and notify all the worker
43+
// wait all jobs are complete.
44+
defer q.Release()
3245

3346
// assign tasks in queue
3447
for i := 0; i < taskN; i++ {
3548
go func(i int) {
36-
if err := q.QueueTask(func(ctx context.Context) error {
37-
rets <- fmt.Sprintf("Hi appleboy, handle the job: %02d", +i)
38-
return nil
49+
if err := q.Queue(&job{
50+
Name: "Gopher",
51+
Message: fmt.Sprintf("handle the job: %d", i+1),
3952
}); err != nil {
40-
panic(err)
53+
log.Println(err)
4154
}
4255
}(i)
4356
}
@@ -47,9 +60,4 @@ func main() {
4760
fmt.Println("message:", <-rets)
4861
time.Sleep(50 * time.Millisecond)
4962
}
50-
51-
// shutdown the service and notify all the worker
52-
q.Shutdown()
53-
// wait all jobs are complete.
54-
q.Wait()
5563
}

0 commit comments

Comments
 (0)