Lines
100 %
Functions
Branches
//! Why one DB-transaction attempt of a lock-protocol writer did not commit,
//! and the bound on how often it runs again.
use crate::db::DBError;
use crate::error::ServerError;
use crate::tag::lock::{ATTEMPTS, KeyChanged};
pub(crate) enum Attempt<E> {
Retry,
Failed(E),
}
impl<E> Attempt<E> {
/// A tag-lock outcome as an attempt: a changed key and a `retryable`
/// failure run again, any other failure is `fail`ed.
pub(crate) fn of<T, X>(
outcome: Result<Result<T, KeyChanged>, X>,
retryable: fn(&X) -> bool,
fail: fn(X) -> E,
) -> Result<T, Self> {
match outcome {
Ok(Ok(done)) => Ok(done),
Ok(Err(KeyChanged)) => Err(Self::Retry),
Err(err) if retryable(&err) => Err(Self::Retry),
Err(err) => Err(Self::Failed(fail(err))),
/// The attempts a writer may still make.
pub(crate) struct Tries(usize);
impl Default for Tries {
fn default() -> Self {
Self(ATTEMPTS)
impl Tries {
/// `Some` once the attempt committed, `None` to try again.
pub(crate) fn step<T, E: From<ServerError>>(
&mut self,
outcome: Result<T, Attempt<E>>,
) -> Result<Option<T>, E> {
self.0 = self.0.saturating_sub(1);
match (outcome, self.0) {
(Ok(done), _) => Ok(Some(done)),
(Err(Attempt::Failed(err)), _) => Err(err),
(Err(Attempt::Retry), 0) => Err(ServerError::DB(DBError::WriteConflict).into()),
(Err(Attempt::Retry), _) => Ok(None),
#[cfg(test)]
#[path = "attempt_tests.rs"]
mod tests;