Fix the leak count for a health check fan-out

Review the AI's codeEasy10 min

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_buffer more. Every other send parks.
  • The result is never below 0.

Examples

parked_goroutine_count(worker_outcomes: list[str], channel_buffer: int) -> int
Example 1
Inputparked_goroutine_count(["healthy", "healthy", "healthy"], 0)
Output2
Example 2
Inputparked_goroutine_count(["healthy", "healthy", "healthy"], 3)
Output0
Example 3
Inputparked_goroutine_count(["unhealthy", "error", "unhealthy"], 0)
Output0