4
4
[ ![ Run Tests] ( https://github.com/golang-queue/queue/actions/workflows/go.yml/badge.svg )] ( https://github.com/golang-queue/queue/actions/workflows/go.yml )
5
5
[ ![ codecov] ( https://codecov.io/gh/golang-queue/queue/branch/master/graph/badge.svg?token=SSo3mHejOE )] ( https://codecov.io/gh/golang-queue/queue )
6
6
7
- Queue is a Golang library for spawning and managing a Goroutine pool, allowing you to create multiple workers based on the CPU capacity of the machine.
7
+ Queue is a Golang library that helps you create and manage a pool of Goroutines (lightweight threads). It allows you to efficiently run multiple tasks in parallel, utilizing the CPU capacity of your machine.
8
8
9
9
## Features
10
10
11
11
- [x] Supports [ Circular buffer] ( https://en.wikipedia.org/wiki/Circular_buffer ) queues.
12
- - [x] Supports [ NSQ] ( https://nsq.io/ ) (a real-time distributed messaging platform) as a backend .
13
- - [x] Supports [ NATS] ( https://nats.io/ ) (connective technology for adaptive edge and distributed systems) as a backend .
14
- - [x] Supports [ Redis Pub/Sub] ( https://redis.io/docs/manual/pubsub/ ) as a backend .
15
- - [x] Supports [ Redis Streams] ( https://redis.io/docs/manual/data-types/streams/ ) as a backend .
16
- - [x] Supports [ RabbitMQ] ( https://www.rabbitmq.com/ ) as a backend .
12
+ - [x] Integrates with [ NSQ] ( https://nsq.io/ ) for real-time distributed messaging.
13
+ - [x] Integrates with [ NATS] ( https://nats.io/ ) for adaptive edge and distributed systems.
14
+ - [x] Integrates with [ Redis Pub/Sub] ( https://redis.io/docs/manual/pubsub/ ) .
15
+ - [x] Integrates with [ Redis Streams] ( https://redis.io/docs/manual/data-types/streams/ ) .
16
+ - [x] Integrates with [ RabbitMQ] ( https://www.rabbitmq.com/ ) .
17
17
18
18
## Queue Scenario
19
19
@@ -35,13 +35,13 @@ Go version **1.22** or above
35
35
36
36
## Installation
37
37
38
- Install the stable version:
38
+ To install the stable version:
39
39
40
40
``` sh
41
41
go get github.com/golang-queue/queue
42
42
```
43
43
44
- Install the latest version:
44
+ To install the latest version:
45
45
46
46
``` sh
47
47
go get github.com/golang-queue/queue@master
@@ -51,7 +51,7 @@ go get github.com/golang-queue/queue@master
51
51
52
52
### Basic Usage of Pool (using the Task function)
53
53
54
- By calling the ` QueueTask() ` method, tasks are scheduled to be executed by workers (goroutines ) in the pool.
54
+ By calling the ` QueueTask() ` method, you can schedule tasks to be executed by workers (Goroutines ) in the pool.
55
55
56
56
``` go
57
57
package main
@@ -68,13 +68,13 @@ func main() {
68
68
taskN := 100
69
69
rets := make (chan string , taskN)
70
70
71
- // initial queue pool
71
+ // initialize the queue pool
72
72
q := queue.NewPool (5 )
73
- // shutdown the service and notify all the worker
74
- // wait all jobs are complete.
73
+ // shut down the service and notify all workers
74
+ // wait until all jobs are complete
75
75
defer q.Release ()
76
76
77
- // assign tasks in queue
77
+ // assign tasks to the queue
78
78
for i := 0 ; i < taskN; i++ {
79
79
go func (i int ) {
80
80
if err := q.QueueTask (func (ctx context.Context ) error {
@@ -86,7 +86,7 @@ func main() {
86
86
}(i)
87
87
}
88
88
89
- // wait until all tasks done
89
+ // wait until all tasks are done
90
90
for i := 0 ; i < taskN; i++ {
91
91
fmt.Println (" message:" , <- rets)
92
92
time.Sleep (20 * time.Millisecond )
@@ -96,7 +96,7 @@ func main() {
96
96
97
97
### Basic Usage of Pool (using a message queue)
98
98
99
- Define a new message struct and implement the ` Bytes() ` function to encode the message. Use the ` WithFn ` function to handle the message from the queue.
99
+ Define a new message struct and implement the ` Bytes() ` function to encode the message. Use the ` WithFn ` function to handle messages from the queue.
100
100
101
101
``` go
102
102
package main
@@ -129,7 +129,7 @@ func main() {
129
129
taskN := 100
130
130
rets := make (chan string , taskN)
131
131
132
- // initial queue pool
132
+ // initialize the queue pool
133
133
q := queue.NewPool (5 , queue.WithFn (func (ctx context.Context , m core.TaskMessage ) error {
134
134
var v job
135
135
if err := json.Unmarshal (m.Payload (), &v); err != nil {
@@ -139,11 +139,11 @@ func main() {
139
139
rets <- " Hi, " + v.Name + " , " + v.Message
140
140
return nil
141
141
}))
142
- // shutdown the service and notify all the worker
143
- // wait all jobs are complete.
142
+ // shut down the service and notify all workers
143
+ // wait until all jobs are complete
144
144
defer q.Release ()
145
145
146
- // assign tasks in queue
146
+ // assign tasks to the queue
147
147
for i := 0 ; i < taskN; i++ {
148
148
go func (i int ) {
149
149
if err := q.Queue (&job{
@@ -155,7 +155,7 @@ func main() {
155
155
}(i)
156
156
}
157
157
158
- // wait until all tasks done
158
+ // wait until all tasks are done
159
159
for i := 0 ; i < taskN; i++ {
160
160
fmt.Println (" message:" , <- rets)
161
161
time.Sleep (50 * time.Millisecond )
@@ -165,7 +165,7 @@ func main() {
165
165
166
166
## Using NSQ as a Queue
167
167
168
- Refer to the [ NSQ documentation] ( https://github.com/golang-queue/nsq ) .
168
+ Refer to the [ NSQ documentation] ( https://github.com/golang-queue/nsq ) for more details .
169
169
170
170
``` go
171
171
package main
@@ -222,7 +222,7 @@ func main() {
222
222
queue.WithWorker (w),
223
223
)
224
224
225
- // assign tasks in queue
225
+ // assign tasks to the queue
226
226
for i := 0 ; i < taskN; i++ {
227
227
go func (i int ) {
228
228
q.Queue (&job{
@@ -231,20 +231,20 @@ func main() {
231
231
}(i)
232
232
}
233
233
234
- // wait until all tasks done
234
+ // wait until all tasks are done
235
235
for i := 0 ; i < taskN; i++ {
236
236
fmt.Println (" message:" , <- rets)
237
237
time.Sleep (50 * time.Millisecond )
238
238
}
239
239
240
- // shutdown the service and notify all the worker
240
+ // shut down the service and notify all workers
241
241
q.Release ()
242
242
}
243
243
```
244
244
245
245
## Using NATS as a Queue
246
246
247
- Refer to the [ NATS documentation] ( https://github.com/golang-queue/nats ) .
247
+ Refer to the [ NATS documentation] ( https://github.com/golang-queue/nats ) for more details .
248
248
249
249
``` go
250
250
package main
@@ -302,10 +302,10 @@ func main() {
302
302
log.Fatal (err)
303
303
}
304
304
305
- // start the five worker
305
+ // start the workers
306
306
q.Start ()
307
307
308
- // assign tasks in queue
308
+ // assign tasks to the queue
309
309
for i := 0 ; i < taskN; i++ {
310
310
go func (i int ) {
311
311
q.Queue (&job{
@@ -314,20 +314,20 @@ func main() {
314
314
}(i)
315
315
}
316
316
317
- // wait until all tasks done
317
+ // wait until all tasks are done
318
318
for i := 0 ; i < taskN; i++ {
319
319
fmt.Println (" message:" , <- rets)
320
320
time.Sleep (50 * time.Millisecond )
321
321
}
322
322
323
- // shutdown the service and notify all the worker
323
+ // shut down the service and notify all workers
324
324
q.Release ()
325
325
}
326
326
```
327
327
328
328
## Using Redis (Pub/Sub) as a Queue
329
329
330
- Refer to the [ Redis documentation] ( https://github.com/golang-queue/redisdb ) .
330
+ Refer to the [ Redis documentation] ( https://github.com/golang-queue/redisdb ) for more details .
331
331
332
332
``` go
333
333
package main
@@ -384,10 +384,10 @@ func main() {
384
384
log.Fatal (err)
385
385
}
386
386
387
- // start the five worker
387
+ // start the workers
388
388
q.Start ()
389
389
390
- // assign tasks in queue
390
+ // assign tasks to the queue
391
391
for i := 0 ; i < taskN; i++ {
392
392
go func (i int ) {
393
393
q.Queue (&job{
@@ -396,13 +396,13 @@ func main() {
396
396
}(i)
397
397
}
398
398
399
- // wait until all tasks done
399
+ // wait until all tasks are done
400
400
for i := 0 ; i < taskN; i++ {
401
401
fmt.Println (" message:" , <- rets)
402
402
time.Sleep (50 * time.Millisecond )
403
403
}
404
404
405
- // shutdown the service and notify all the worker
405
+ // shut down the service and notify all workers
406
406
q.Release ()
407
407
}
408
408
```
0 commit comments