F# retry with capped exponential backoff and jitter
F#
open System
open System.Threading
let retry (attempts: int) (op: unit -> 'T) : 'T =
if attempts < 1 then invalidArg "attempts" "must be at least 1"
let rnd = Random()
let mutable result : 'T option = None
let mutable attempt = 0
while result.IsNone do
try
result <- Some(op ())
with _ when attempt < attempts - 1 ->
let delay = min 1000.0 (100.0 * 2.0 ** float attempt)
Thread.Sleep(int (delay + delay * 0.5 * rnd.NextDouble()))
attempt <- attempt + 1
result.Value