Had a similar problem in Elixir - spawn a pool of HTTP requests and notify immediately of the first one that completes with a status of "ok".
Task.Supervisor.async_stream(
My.TaskSupervisor,
my_list_of_urls,
&assess_url_exists/1,
ordered: false,
max_concurrency: 100,
timeout: 30_000
)
|> Enum.find(fn
{:ok, {:ok, _url}} -> true
_ -> false
end)
And - that's it. Do I want it ordered? just change the "ordered" flag. Tweaking concurrency? it's there.But the really best part is that any pending computation at the point when the correct result is found, it is automatically cancelled. No waiting for its completion. No memory leaks, no file descriptors left open. It just goes away. Boom.
Just sayin'...