RenameWithRetryStrategy

Struct RenameWithRetryStrategy 

Source
pub struct RenameWithRetryStrategy {}
Expand description

RenameWithRetryStrategy uses the Standard Library rename function to transition the working file to the target file and retries if that fails with a PermissionDenied error.

The other commit strategy available is SimpleRenameStrategy.

For POSIX systems and Windows systems in which there is no contention for the target file, SimpleRenameStrategy is a good choice. For Windows systems in which two or more threads are simultaneously trying to update the target, RenameWithRetryStrategy is a good choice.

This crate provides a ready-to-use RenameWithRetryStrategy instance named RENAME_WITH_RETRY_STRATEGY.

By default, the SimpleRenameStrategy commit strategy is used.

This retry strategy has been shown to work well with Windows 10 and Windows 11 on local SSD drives and with a NAS using as many as 10 threads contending for the target file…

  • Use a “jitter” between 0 and 15 to avoid threads being synchronized during contention
  • Calculate a “base sleep” value: 11 + (3 * jitter)
  • Try to commit
  • If that succeeds then we’re done
  • If that fails with any error except PermissionDenied then return that error
  • Otherwise sleep for the base sleep value multiplied by the try count. For example…
    • If the jitter is 1
    • Then the base sleep is 11 + (3 * 1) = 14
    • For the first try, this strategy would sleep for 14 * 1 milliseconds
    • For the second try, this strategy would sleep for 14 * 2 milleconds
  • Until 7 attempts have been made at which point the PermissionDenied error is returned.

In the worst case, this strategy sleeps for a total of (11 + (3 * 15)) * (7 * (7+1) / 2) = 1568 milliseconds.

§Example

use phazer::{PhazerBuilder, RENAME_WITH_RETRY_STRATEGY};

fn main() -> Result<(), Box<dyn std::error::Error>> {

    let phazer = PhazerBuilder::with_target("uses-rename-with-retry-strategy.txt")
        .commit_strategy(RENAME_WITH_RETRY_STRATEGY)
        .build();

    // Build the working file

    // `rename` is called to transition the working file to the target
    phazer.commit()?;

    Ok(())
}

Trait Implementations§

Source§

impl Default for RenameWithRetryStrategy

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.