use leaseclock; use retrydelay; fn sandpiper_max2(a: f64, b: f64) -> f64 { if a > b { return a; } b } fn sandpiper_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 sandpiper_compute(issued: f64, ttl: f64, now: f64, base: f64, ceiling: f64, attempt: f64) -> vector { let clock = leaseclock_remaining(issued, ttl, now); let delay = retrydelay_capped(base, ceiling, attempt); if len(clock) == 0 || len(delay) == 0 { return []; } let wait = sandpiper_max2(0.0, at(clock, 1) + at(delay, 0) - at(clock, 2)); if wait == 0.0 { return [1, 0, at(delay, 0)]; } [0, wait, at(delay, 0)] } // Schedule a deterministic redelivery after both lease expiry and retry backoff. // Returns [ready, ticks to eligibility, retry delay]. Delay begins when the lease expires. The queue host owns acknowledgement and delivery. pub fn sandpiper_redeliver(issued: f64, ttl: f64, now: f64, base: f64, ceiling: f64, attempt: f64) -> vector { if !(issued >= 0.0 && issued <= 9007199254740991.0 && issued % 1.0 == 0.0) { return []; } if !(ttl >= 1.0 && ttl <= 9007199254740991.0 && ttl % 1.0 == 0.0) { return []; } if !(now >= 0.0 && now <= 9007199254740991.0 && now % 1.0 == 0.0) { return []; } if !(base >= 1.0 && base <= 9007199254740991.0 && base % 1.0 == 0.0) { return []; } if !(ceiling >= 1.0 && ceiling <= 9007199254740991.0 && ceiling % 1.0 == 0.0) { return []; } if !(attempt >= 0.0 && attempt <= 9007199254740991.0 && attempt % 1.0 == 0.0) { return []; } let result = sandpiper_compute(issued, ttl, now, base, ceiling, attempt); if !sandpiper_finite(result) { return []; } result }