Skip to content

Commit

Permalink
Iotedge Check: Timeout for aka.ms latest version http request (#6937)
Browse files Browse the repository at this point in the history
  • Loading branch information
and-rewsmith authored Mar 7, 2023
1 parent 82ca5e8 commit f7dd1aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 19 additions & 6 deletions edgelet/iotedge/src/check/checks/aziot_edged_version.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::time::Duration;

use anyhow::{anyhow, Context};
use regex::Regex;

use crate::check::{Check, CheckResult, Checker, CheckerMeta};
use crate::error::{Error, FetchLatestVersionsReason};

const AKA_MS_HTTP_REQUEST_TIMEOUT: Duration = Duration::from_secs(300);

#[derive(Default, serde::Serialize)]
pub(crate) struct AziotEdgedVersion {
actual_version: Option<String>,
Expand Down Expand Up @@ -53,12 +57,21 @@ impl AziotEdgedVersion {
req
};

let res = client
.request(req)
.await
.context(Error::FetchLatestVersions(
FetchLatestVersionsReason::GetResponse,
))?;
let res = tokio::time::timeout(AKA_MS_HTTP_REQUEST_TIMEOUT, client.request(req)).await;
let res = match res {
Ok(Ok(res)) => res,
Ok(Err(e)) => {
return Err(e).context(Error::FetchLatestVersions(
FetchLatestVersionsReason::GetResponse,
));
}
Err(e) => {
return Err(e).context(Error::FetchLatestVersions(
FetchLatestVersionsReason::RequestTimeout,
));
}
};

match res.status() {
status_code if status_code.is_redirection() => {
uri = res
Expand Down
2 changes: 2 additions & 0 deletions edgelet/iotedge/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub enum Error {

#[derive(Clone, Copy, Debug)]
pub enum FetchLatestVersionsReason {
RequestTimeout,
CreateClient,
GetResponse,
InvalidOrMissingLocationHeader,
Expand All @@ -72,6 +73,7 @@ impl fmt::Display for FetchLatestVersionsReason {
match self {
FetchLatestVersionsReason::CreateClient => write!(f, "could not create HTTP client"),
FetchLatestVersionsReason::GetResponse => write!(f, "could not send HTTP request"),
FetchLatestVersionsReason::RequestTimeout => write!(f, "HTTP request timed out"),
FetchLatestVersionsReason::InvalidOrMissingLocationHeader => write!(
f,
"redirect response has invalid or missing location header"
Expand Down

0 comments on commit f7dd1aa

Please sign in to comment.