Skip to content

Commit

Permalink
docs: update example
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Aug 21, 2021
1 parent 8416a18 commit 21ac0e2
Showing 1 changed file with 54 additions and 140 deletions.
194 changes: 54 additions & 140 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,125 +27,55 @@ go get github.com/golang-queue/queue@master

## Usage

The first step to create a new job as `QueueMessage` interface:
### Basic usage of Pool (use Task function)

```go
type job struct {
Message string
}

func (j *job) Bytes() []byte {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
return b
}
```

The second step to create the new worker, use the buffered channel as an example.
By calling `QueueTask()` method, it schedules the task executed by worker (goroutines) in the Pool.

```go
// define the worker
w := simple.NewWorker(
simple.WithQueueNum(taskN),
simple.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
v, ok := m.(*job)
if !ok {
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
return err
}
}

rets <- v.Message
return nil
}),
)
```

or use the [NSQ](https://nsq.io/) as backend, see the worker example:

```go
// define the worker
w := nsq.NewWorker(
nsq.WithAddr("127.0.0.1:4150"),
nsq.WithTopic("example"),
nsq.WithChannel("foobar"),
// concurrent job number
nsq.WithMaxInFlight(10),
nsq.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
v, ok := m.(*job)
if !ok {
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
return err
}
}

rets <- v.Message
return nil
}),
)
```

or use the [NATS](https://nats.io/) as backend, see the worker example:
package main

```go
w := nats.NewWorker(
nats.WithAddr("127.0.0.1:4222"),
nats.WithSubj("example"),
nats.WithQueue("foobar"),
nats.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
v, ok := m.(*job)
if !ok {
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
return err
}
}
import (
"context"
"fmt"
"time"

rets <- v.Message
return nil
}),
"github.com/golang-queue/queue"
)
```

The third step to create a queue and initialize multiple workers, receive all job messages:
func main() {
taskN := 100
rets := make(chan string, taskN)

```go
// define the queue
q, err := queue.NewQueue(
queue.WithWorkerCount(5),
queue.WithWorker(w),
)
if err != nil {
log.Fatal(err)
}
// initial queue pool
q := queue.NewPool(5)
// shutdown the service and notify all the worker
// wait all jobs are complete.
defer q.Release()

// start the five worker
q.Start()

// assign tasks in queue
for i := 0; i < taskN; i++ {
go func(i int) {
q.Queue(&job{
Name: "foobar",
Message: fmt.Sprintf("handle the job: %d", i+1),
})
}(i)
}
// assign tasks in queue
for i := 0; i < taskN; i++ {
go func(i int) {
if err := q.QueueTask(func(ctx context.Context) error {
rets <- fmt.Sprintf("Hi Gopher, handle the job: %02d", +i)
return nil
}); err != nil {
panic(err)
}
}(i)
}

// wait until all tasks done
for i := 0; i < taskN; i++ {
fmt.Println("message:", <-rets)
time.Sleep(50 * time.Millisecond)
// wait until all tasks done
for i := 0; i < taskN; i++ {
fmt.Println("message:", <-rets)
time.Sleep(20 * time.Millisecond)
}
}

// shutdown the service and notify all the worker
q.Shutdown()
// wait all jobs are complete.
q.Wait()
```

Full example code as below or [try it in playground](https://play.golang.org/p/77PtkZRaPE-).
### Basic usage of Pool (use message queue)

Define the new message struct and implement the `Bytes()` func to encode message. Give the `WithFn` func
to handle the message from Queue.

```go
package main
Expand All @@ -158,7 +88,6 @@ import (
"time"

"github.com/golang-queue/queue"
"github.com/golang-queue/queue/simple"
)

type job struct {
Expand All @@ -178,41 +107,31 @@ func main() {
taskN := 100
rets := make(chan string, taskN)

// define the worker
w := simple.NewWorker(
simple.WithQueueNum(taskN),
simple.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
v, ok := m.(*job)
if !ok {
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
return err
}
// initial queue pool
q := queue.NewPool(5, queue.WithFn(func(ctx context.Context, m queue.QueuedMessage) error {
v, ok := m.(*job)
if !ok {
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
return err
}
}

rets <- "Hi, " + v.Name + ", " + v.Message
return nil
}),
)

// define the queue
q, err := queue.NewQueue(
queue.WithWorkerCount(5),
queue.WithWorker(w),
)
if err != nil {
log.Fatal(err)
}

// start the five worker
q.Start()
rets <- "Hi, " + v.Name + ", " + v.Message
return nil
}))
// shutdown the service and notify all the worker
// wait all jobs are complete.
defer q.Release()

// assign tasks in queue
for i := 0; i < taskN; i++ {
go func(i int) {
q.Queue(&job{
Name: "foobar",
if err := q.Queue(&job{
Name: "Gopher",
Message: fmt.Sprintf("handle the job: %d", i+1),
})
}); err != nil {
log.Println(err)
}
}(i)
}

Expand All @@ -221,10 +140,5 @@ func main() {
fmt.Println("message:", <-rets)
time.Sleep(50 * time.Millisecond)
}

// shutdown the service and notify all the worker
q.Shutdown()
// wait all jobs are complete.
q.Wait()
}
```

0 comments on commit 21ac0e2

Please sign in to comment.