1. Pull method

Introduce an infinite loop in program that fetch the file.

loop {
    let modified = fs::metadata("config.yaml")?.modified()?;
    if modified != last_modified {
        reload();
    }
    std::thread::sleep(Duration::from_secs(1));
}

This loop wakes up every second even if:

  • nothing changes
  • CPU is busy
  • disk is idle
  • the file hasn’t been touched for a long time

Most iterations do nothing, but they still cost:

  • syscalls
  • disk metadata reads
  • thread scheduling
  • CPU time
  • battery power (on laptops)

Therefore, polling scales linearly with number of watched files.

2. Push method

A file modification happens. It notifies an operation system kennel subsystem. Then this kennel subsystem triggers a callback function in program.

The subsystems that handle this kind of notification are different in each operation system:

  • In Linux, it is inotify.
  • In Window, it is ReadDirectoryChangesW.
  • In Macos, it is FSEvents.

These are event-driven kernel APIs, meaning:

  • OS monitors file system changes internally
  • OS sends an event only when a change occurs
  • Thread in our program sleeps 100% of the time
  • Zero CPU cycles are used until an actual change happens

Benefit of this approach is that Zero resources (CPU cycles, etc...) are used until an actual modification happens.