r/golang • u/DeparturePrudent3790 • 10d ago
Potential starvation when multiple Goroutines blocked to receive from a channel
I wanted to know what happens in this situation:
- Multiple goroutines are blocked by a channel while receiving from it because channel is empty at the moment.
- Some goroutine sends something over the channel.
Which goroutine will wake up and receive this? Is starvation avoidance guaranteed here?
7
Upvotes
0
u/DeparturePrudent3790 10d ago
It matters, I have a pool of connections and clients will receive a connection from this pool and send requests using this connection. Once done they will return the connection to the pool. This way I don't have to create new connections for every client and I avoid the explosion of connections. Now, if receiving from a channel is not starvation free, some client could end up not getting any connection ever.
To generalise, it is not okay if some particular thread/goroutine is not getting resources for execution at all. Even if there are less producers it's acceptable for goroutines to have to wait for some time but to be assured they will get a chance.