Round robin concurrent algorithm with F# and Task<T>

Viewed 232

I have a C# API like this:

Task<T> Foo(serverUri)

Let's say I have 4 possible serverUris. I want to implement a function that will return DiscUnionBar type:

type DiscUnionBar of T =
    Safe of T | Weak of T | ConnectionError

The implementation will have the following requirements:

  1. Do 3 (max) concurrent calls to Foo() with 3 different serverUris.
  2. Pick the 2 fastest successful responses. If they give same result T1 and T2 (being T1==T2), stop doing concurrent requests and ignore/cancel requests that are in progress and return Safe of T. If T1!=T2, keep doing more requests (or looking at responses) until two equal responses are found.
  3. If any of the requests fails (throws ServerException), try with a serverUri that has not been requested before.
  4. If all requests to all 4 servers fail, return ConnectionError.
  5. If only 1 request succeeds, return Weak of T.

Is this easy to do given that I cannot use F#'s Async and have to stick with C#'s Task usage? I'm a bit lost on this one.

1 Answers
Related