Skip to content

Instantly share code, notes, and snippets.

@eiriktsarpalis
Last active December 17, 2015 22:49
Show Gist options
  • Save eiriktsarpalis/5684335 to your computer and use it in GitHub Desktop.
Save eiriktsarpalis/5684335 to your computer and use it in GitHub Desktop.
retry async
open System
type RetryPolicy = Policy of (int -> exn -> TimeSpan option)
/// retries given action based on given policy
let retryAsync (Policy p) (f : Async<'T>) =
let rec aux retries =
async {
let! result = Async.Catch f
match result with
| Choice1Of2 t -> return t
| Choice2Of2 e ->
match p (retries + 1) e with
| None -> return raise e
| Some interval ->
do! Async.Sleep (int interval.TotalMilliseconds)
return! aux (retries + 1)
}
aux 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment