Reputation: 496
Since it is generally very little overhead in terms of memory requirement and setup/tear down cost of a Go routine. Is it relevant even to implement a thread(go routine) worker pool? When would you consider using a thread pool instead of 'spawning' a go routine per request?
Upvotes: 0
Views: 2604
Reputation: 8490
Yes, it's relevant. db/sql uses pool of connections to database, because of establishing new connection take time.
Upvotes: 1
Reputation: 288
Spawning and keeping lots of goroutines in golang is cheap but it's not free.
Also you should remember that goroutine themselves may be very cheap, but at the same time a lot of memory can be allocated inside of goroutine code. So you may want to limit number of concurrently running goroutines.
You may use semaphore to limit resources. Another approach (more idiomatic for go) is to use executions pipelines with worker pools. This pattern is very well described in golang blog.
Upvotes: 2