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
- Host: GitHub
- URL: https://github.com/haormj/dispatcher
- Owner: haormj
- License: apache-2.0
- Created: 2018-12-26T21:34:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-08T06:42:01.000Z (about 1 year ago)
- Last Synced: 2025-03-24T10:06:47.668Z (2 months ago)
- Topics: dispatcher, golang, routine
- Language: Go
- Homepage:
- Size: 72.3 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- License: LICENSE
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 mainimport (
"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