Hi Ilya Pokolev, .
Yes, the goroutines are cheap to create, but it is not free.
For some projects, when we can't predict how many goroutine will be spawned by a process, it make sense to use worker pool. The memory consumption can be limited by not allowing the number of goroutines to exceed the maximum pool size.
Here is the example for my use case on HTTP handler:
func createPostHandler(w http.ResponseWriter, req *http.Request) {
// Validate param
// Create post
// etc etc
go needToBeExecutedAfterPostCreated()
fmt.Fprintf(w, "OK")
}
func needToBeExecutedAfterPostCreated() { // Some heavy process }
First, we can spawn goroutine to executing needToBeExecutedAfterPostCreated function, but when there are
so many request to createPostHandler at the same time, the number of goroutines keep increasing. By using Worker Pool, we can limit the amount of gourotines to N size.
I tried the Semaphore approach, but unfortunately the process will be blocked when the buffered channel capacity for Semaphore is full.
Thanks for your response, feel free if you have any idea for further discussion.
:D