Last active
December 17, 2015 22:49
-
-
Save eiriktsarpalis/5684335 to your computer and use it in GitHub Desktop.
retry async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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