Expand description
Throttle how often an application checks for updates.
Every call to update() (or is_update_available()) makes
a network request. An application that would otherwise check on every run can gate that behind
UpdateCheckGuard, a small timestamp-stamp-file guard: it records the time of the last check in a
file you nominate and reports whether enough time has passed to check again.
This is intentionally a guard, not a scheduler: it does not spawn threads or timers, and it stores
nothing but a single unix-epoch-seconds timestamp (no chrono/time dependency). It is also not a
preferences store; where the stamp file lives and what interval to use are the application’s
decisions.
use std::time::Duration;
use self_update::check_interval::UpdateCheckGuard;
fn maybe_update() -> Result<(), Box<dyn std::error::Error>> {
// The caller owns the path. A real app typically builds it from a per-user cache directory
// (e.g. the `dirs` crate's `dirs::cache_dir()`) added as the application's own dependency.
let stamp = std::env::temp_dir().join("myapp/update-check");
let guard = UpdateCheckGuard::new(stamp, Duration::from_secs(24 * 60 * 60));
if guard.should_check()? {
// ... run the self_update check/update here ...
guard.record_check()?;
}
Ok(())
}§Semantics
should_check returns true (a check is due) when:
- the stamp file does not exist yet (first run),
- its contents are not a valid timestamp (a corrupt stamp self-heals: it is treated as due, not as an error), or
- the recorded time is at least
intervalin the past.
A stamp dated in the future (clock skew, or a stamp copied from another machine) also counts as
due. Only a genuine IO error reading the file (e.g. a permissions failure) surfaces as Err; a
missing file does not.
record_check writes the current time by writing to a temporary
file in the same directory and renaming it over the stamp path, so a concurrent reader never
observes a half-written stamp.
Structs§
- Update
Check Guard - A timestamp-stamp-file guard that throttles how often an application checks for updates. See the module docs for the full model and an example.