Fix the leak count for a health check fan-out
Your task
A Go function checks a list of URLs at once, one goroutine per URL, and returns the first healthy one. Each healthy worker sends its URL on a results channel. The caller receives from that channel exactly once and returns. A send with nowhere to go blocks forever, so that goroutine is parked: it leaks, along with its open response body.
An assistant wrote parked_goroutine_count(worker_outcomes, channel_buffer) to count how many goroutines one call leaks. It is in the editor. It has a bug. Find it and fix it.
Input and output:
worker_outcomes: one entry per worker, such as"healthy","unhealthy"or"error".channel_buffer: the results channel's buffer size.- Return how many goroutines are left parked.
Rules the tests check:
- Only a worker whose outcome is exactly
"healthy"sends. Any other value,"HEALTHY"included, returns without sending and never parks. - A buffer below 0 counts as 0, an unbuffered channel.
- The single receive takes one send, and the buffer holds up to
channel_buffermore. Every other send parks. - The result is never below 0.
Examples
parked_goroutine_count(worker_outcomes: list[str], channel_buffer: int) -> intExample 1
Input
parked_goroutine_count(["healthy", "healthy", "healthy"], 0)Output
2Example 2
Input
parked_goroutine_count(["healthy", "healthy", "healthy"], 3)Output
0Example 3
Input
parked_goroutine_count(["unhealthy", "error", "unhealthy"], 0)Output
0