2 people like it.

Retry loop, no tricks

Less-nonsense 8-line retry function that will retry a function a specified up to `maxRetries` times while it throws. After the retries, any remaining exception is allowed to propagate. Accepts a before function to allow you to wait/report when a retry is taking place

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let retry maxRetries before f =
    let rec loop retriesRemaining =
        try
            f ()
        with _ when retriesRemaining > 0 ->
            before ()
            loop (retriesRemaining-1)
    loop maxRetries
val retry : maxRetries:int -> before:(unit -> unit) -> f:(unit -> 'a) -> 'a

Full name: Script.retry
val maxRetries : int
val before : (unit -> unit)
val f : (unit -> 'a)
val loop : (int -> 'a)
val retriesRemaining : int

More information

Link:http://fssnip.net/o0
Posted:10 years ago
Author:Ruben Bartelink
Tags: retry , sleep , recursion , exceptions