Simple implement concurrent worker pool & queue in go. You can test it by simple command below.
$ git clone https://github.com/lequocbinh04/go-workerpool
$ cd go-workerpool
$ go build
$ ./worker-pool
Then you can go to https://localhost:8080/test?msg=Hello worker and check log.
You can copy workerpool folder to your project. Init worker pool:
dispatch := workerpool.NewDispatcher(10) // start 10 workers
dispatch.Run()
workerpool.InitJobQueue()
and add job to pool:
job := workerpool.NewJob(func(ctx context.Context) error {
// Simple job, replace it :D
time.Sleep(time.Second)
log.Println("I am job, message: ", msg)
return nil
})
workerpool.JobQueue <- job