Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(*): add automatic retries to AtatClient::send implementation #124

Merged
merged 6 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract retries to a new given send_retry function in AtatClient
  • Loading branch information
MathiasKoch committed Apr 8, 2022
commit d95b1f23ac681405d17a2a1d598037c617e0c2b3
25 changes: 6 additions & 19 deletions atat/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,14 @@ where
return cmd.parse(Ok(&[])).map_err(nb::Error::Other);
}

let mut error = Err(nb::Error::Other(Error::Error));
for _ in 0..(A::RETRY_ATTEMPTS + 1) {
let result = match self.config.mode {
Mode::Blocking => Ok(nb::block!(self.check_response(cmd))?),
Mode::NonBlocking => self.check_response(cmd),
Mode::Timeout => {
self.timer.start(A::MAX_TIMEOUT_MS.millis()).ok();
Ok(nb::block!(self.check_response(cmd))?)
}
};

match result {
e @ Err(nb::Error::Other(Error::Timeout))
| e @ Err(nb::Error::Other(Error::Parse)) => {
error = e;
}
r => return r,
match self.config.mode {
Mode::Blocking => Ok(nb::block!(self.check_response(cmd))?),
Mode::NonBlocking => self.check_response(cmd),
Mode::Timeout => {
self.timer.start(A::MAX_TIMEOUT_MS.millis()).ok();
Ok(nb::block!(self.check_response(cmd))?)
}
}

return error;
}

fn peek_urc_with<URC: AtatUrc, F: FnOnce(URC::Response) -> bool>(&mut self, f: F) {
Expand Down
27 changes: 25 additions & 2 deletions atat/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ pub trait AtatCmd<const LEN: usize> {
/// The max timeout in milliseconds.
const MAX_TIMEOUT_MS: u32 = 1000;

/// The max number of times to automatically retry a command
const RETRY_ATTEMPTS: u8 = 2;
/// The max number of times to attempt a command with automatic retries if
/// using `send_retry`.
const ATTEMPTS: u8 = 3;

/// Force client to look for a response.
/// Empty slice is then passed to parse by client.
Expand Down Expand Up @@ -101,6 +102,28 @@ pub trait AtatClient {
cmd: &A,
) -> nb::Result<A::Response, Error>;

fn send_retry<A: AtatCmd<LEN>, const LEN: usize>(
&mut self,
cmd: &A,
) -> nb::Result<A::Response, Error> {
let mut error = Err(nb::Error::Other(Error::Error));

for attempt in 0..A::ATTEMPTS {
if attempt > 0 {
debug!("Attempt {}:", attempt);
}

match self.send(cmd) {
e @ Err(nb::Error::Other(Error::Timeout))
| e @ Err(nb::Error::Other(Error::Parse)) => {
error = e;
}
r => return r,
}
}
error
}

/// Checks if there are any URC's (Unsolicited Response Code) in
/// queue from the ingress manager.
///
Expand Down