An open API service indexing awesome lists of open source software.

https://github.com/haormj/dispatcher

control goroutine number
https://github.com/haormj/dispatcher

dispatcher golang routine

Last synced: about 2 months ago
JSON representation

control goroutine number

Awesome Lists containing this project

README

        

* Dispatcher
** What is Dispatcher?
Dispatcher can control you goroutine number.

The dispatcher contains a worker pool and a job queue. It will assign the job to the worker. After the task is completed, the worker will return it to the worker pool. When there is no worker in the worker pool, the job will be stored in the job queue. When the queue reaches its maximum value, the new job will be discarded directly.

[[./dispatcher.png]]
** How to use Dispatcher?
#+BEGIN_SRC go
package main

import (
"log"
"time"

"github.com/haormj/dispatcher"
)

type HelloJob struct{}

func (*HelloJob) Do() {
time.Sleep(time.Second * 4)
log.Println("done")
}

func main() {
// the number of goroutines
maxWorkers := 1
// the total of cache jobs
maxJobs := 2
d := dispatcher.NewDispatcher(maxWorkers, maxJobs)
d.Run()

for {
helloJob := HelloJob{}
if d.Add(&helloJob) {
log.Println("add job successfully")
} else {
log.Println("add job failed")
}
time.Sleep(time.Second)
}
}
#+END_SRC