Skip to content

Commit 5119786

Browse files
authored
docs: add simple example (#9)
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 4ef0081 commit 5119786

File tree

4 files changed

+223
-1
lines changed

4 files changed

+223
-1
lines changed

README.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing
88
## Features
99

1010
* [x] Support [buffered channel](https://gobyexample.com/channel-buffering) queue.
11-
* [x] Support [NSQ](https://nsq.io/) (A realtime distributed messaging platform)
11+
* [x] Support [NSQ](https://nsq.io/) (A realtime distributed messaging platform) as backend.
1212

1313
## Installation
1414

@@ -17,3 +17,140 @@ go get github.com/appleboy/queue
1717
```
1818

1919
## Usage
20+
21+
First to create new job as `QueueMessage` interface:
22+
23+
```go
24+
type job struct {
25+
message string
26+
}
27+
28+
func (j *job) Bytes() []byte {
29+
return []byte(j.message)
30+
}
31+
```
32+
33+
Second to create the new worker, use buffered channel as example:
34+
35+
```go
36+
// define the worker
37+
w := simple.NewWorker(
38+
simple.WithQueueNum(taskN),
39+
simple.WithRunFunc(func(m queue.QueuedMessage) error {
40+
j, ok := m.(*job)
41+
if !ok {
42+
return errors.New("message is not job type")
43+
}
44+
45+
rets <- j.message
46+
return nil
47+
}),
48+
)
49+
```
50+
51+
Third to create queue and initialize multiple worker, receive all job message:
52+
53+
```go
54+
// define the queue
55+
q, err := queue.NewQueue(
56+
queue.WithWorkerCount(5),
57+
queue.WithWorker(w),
58+
)
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
63+
// start the five worker
64+
q.Start()
65+
66+
// assign tasks in queue
67+
for i := 0; i < taskN; i++ {
68+
go func(i int) {
69+
q.Queue(&job{
70+
message: fmt.Sprintf("handle the job: %d", i+1),
71+
})
72+
}(i)
73+
}
74+
75+
// wait until all tasks done
76+
for i := 0; i < taskN; i++ {
77+
fmt.Println("message:", <-rets)
78+
time.Sleep(50 * time.Millisecond)
79+
}
80+
81+
q.Shutdown()
82+
q.Wait()
83+
```
84+
85+
Full example code as below or [try it in playground](https://play.golang.org/p/DlhCQgZZ1Bb).
86+
87+
```go
88+
package main
89+
90+
import (
91+
"errors"
92+
"fmt"
93+
"log"
94+
"time"
95+
96+
"github.com/appleboy/queue"
97+
"github.com/appleboy/queue/simple"
98+
)
99+
100+
type job struct {
101+
message string
102+
}
103+
104+
func (j *job) Bytes() []byte {
105+
return []byte(j.message)
106+
}
107+
108+
func main() {
109+
taskN := 100
110+
rets := make(chan string, taskN)
111+
112+
// define the worker
113+
w := simple.NewWorker(
114+
simple.WithQueueNum(taskN),
115+
simple.WithRunFunc(func(m queue.QueuedMessage) error {
116+
j, ok := m.(*job)
117+
if !ok {
118+
return errors.New("message is not job type")
119+
}
120+
121+
rets <- j.message
122+
return nil
123+
}),
124+
)
125+
126+
// define the queue
127+
q, err := queue.NewQueue(
128+
queue.WithWorkerCount(5),
129+
queue.WithWorker(w),
130+
)
131+
if err != nil {
132+
log.Fatal(err)
133+
}
134+
135+
// start the five worker
136+
q.Start()
137+
138+
// assign tasks in queue
139+
for i := 0; i < taskN; i++ {
140+
go func(i int) {
141+
q.Queue(&job{
142+
message: fmt.Sprintf("handle the job: %d", i+1),
143+
})
144+
}(i)
145+
}
146+
147+
// wait until all tasks done
148+
for i := 0; i < taskN; i++ {
149+
fmt.Println("message:", <-rets)
150+
time.Sleep(50 * time.Millisecond)
151+
}
152+
153+
q.Shutdown()
154+
q.Wait()
155+
}
156+
```

_example/simple/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module example
2+
3+
go 1.16
4+
5+
require github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945 // indirect

_example/simple/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/appleboy/queue v0.0.3 h1:rntqVTm6ilh80VCVQjwA0vDMCl1cfveq6GS6X98fKwE=
2+
github.com/appleboy/queue v0.0.3/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
3+
github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945 h1:Mec41QJ0hGDtbyfWgHfuXKqTu0a8hSIIBhL3sNbNjqQ=
4+
github.com/appleboy/queue v0.0.4-0.20210725052905-4ef008157945/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
7+
github.com/nsqio/go-nsq v1.0.8/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

_example/simple/main.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/appleboy/queue"
10+
"github.com/appleboy/queue/simple"
11+
)
12+
13+
type job struct {
14+
message string
15+
}
16+
17+
func (j *job) Bytes() []byte {
18+
return []byte(j.message)
19+
}
20+
21+
func main() {
22+
taskN := 100
23+
rets := make(chan string, taskN)
24+
25+
// define the worker
26+
w := simple.NewWorker(
27+
simple.WithQueueNum(taskN),
28+
simple.WithRunFunc(func(m queue.QueuedMessage) error {
29+
j, ok := m.(*job)
30+
if !ok {
31+
return errors.New("message is not job type")
32+
}
33+
34+
rets <- j.message
35+
return nil
36+
}),
37+
)
38+
39+
// define the queue
40+
q, err := queue.NewQueue(
41+
queue.WithWorkerCount(5),
42+
queue.WithWorker(w),
43+
)
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
48+
// start the five worker
49+
q.Start()
50+
51+
// assign tasks in queue
52+
for i := 0; i < taskN; i++ {
53+
go func(i int) {
54+
q.Queue(&job{
55+
message: fmt.Sprintf("handle the job: %d", i+1),
56+
})
57+
}(i)
58+
}
59+
60+
// wait until all tasks done
61+
for i := 0; i < taskN; i++ {
62+
fmt.Println("message:", <-rets)
63+
time.Sleep(50 * time.Millisecond)
64+
}
65+
66+
q.Shutdown()
67+
q.Wait()
68+
}

0 commit comments

Comments
 (0)