The Flaky Sensor
Stardate 55164.8 ยท UES Magnanimous
Briefing
The dorsal sensor times out about a third of the time and answers perfectly the rest. Lt. Skree does not want it replaced; Skree wants it retried, up to a set number of attempts, and then given up on honestly. A timeout is worth retrying. A sensor reporting that it is physically broken is not, and retrying it three times just wastes three tries.
Commander Raghunathan adds one thing: the retry must not call the sensor more times than it needs to. If the first read works, that is one call. Not two "to be sure". This is a mission about restraint, and about telling two kinds of failure apart.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขA read that succeeds first time is called exactly once
- โขFails twice then succeeds, with three attempts: the value, in three calls
- โขAlways times out, with three attempts: nothing, in exactly three calls
- โขOne attempt and a timeout: nothing, in one call
- โขA broken sensor is not retried
Objective
Write read_with_retry(read, attempts): read is a function of
no arguments that returns a value or raises. Call it up to attempts times,
retrying only on TimeoutError, and return the first value it gives. If every
attempt times out, return None. Any other exception must
propagate immediately, not be retried.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Keep the enum as given. Write read_with_retry(read: &mut dyn FnMut() ->
Result<u32, SensorError>, attempts: u32) -> Option<u32>: call
read up to attempts times, retrying only on
SensorError::Timeout. Return Some(value) on the first success.
If every attempt times out, return None. On SensorError::Broken,
stop immediately and return None without retrying.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Loop up to attempts times. Return the value on success. On a timeout, continue to the next attempt. On any other failure, stop immediately. If the loop ends without a value, return nothing.
Debrief
The dorsal sensor answers, on average, in 1.4 attempts. It has been pointed at the other ship, and it reports that the other ship's dorsal sensor is pointed at us. Skree has said "of course it is" and gone very still again.