use deadlinepath; use statusretry; fn martin_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn martin_magnitudes(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value - value == 0.0) { return false; } i += 1; } true } fn martin_compute(budget: f64, stages: vector, wait: f64, minimum: f64, status: f64, idempotent: f64, attempts: f64, limit: f64) -> vector { let time = deadlinepath_remaining(budget, stages, wait); let policy = statusretry_classify(status, idempotent, attempts, limit); if len(time) == 0 || len(policy) == 0 { return []; } let mut retry = 0; if at(policy, 0) == 1.0 && at(time, 0) >= minimum { retry = 1; } [retry, at(time, 0), at(policy, 1)] } // Decide whether another RPC attempt fits the remaining end-to-end deadline. // Returns [retry now or after wait, attempt budget after wait, attempts left]. The minimum is the shortest useful child duration. pub fn martin_retry(budget: f64, stages: vector, wait: f64, minimum: f64, status: f64, idempotent: f64, attempts: f64, limit: f64) -> vector { if !(budget >= 0.0 && budget - budget == 0.0) { return []; } if !martin_magnitudes(stages) { return []; } if !(wait >= 0.0 && wait - wait == 0.0) { return []; } if !(minimum > 0.0 && minimum - minimum == 0.0) { return []; } if !(status >= 0.0 && status <= 9007199254740991.0 && status % 1.0 == 0.0) { return []; } if !(idempotent >= 0.0 && idempotent <= 9007199254740991.0 && idempotent % 1.0 == 0.0) { return []; } if !(attempts >= 0.0 && attempts <= 9007199254740991.0 && attempts % 1.0 == 0.0) { return []; } if !(limit >= 1.0 && limit <= 9007199254740991.0 && limit % 1.0 == 0.0) { return []; } let result = martin_compute(budget, stages, wait, minimum, status, idempotent, attempts, limit); if !martin_finite(result) { return []; } result }