Skip to content

Commit

Permalink
[RSDK-9196] add init connection retry (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjperez authored Dec 7, 2024
1 parent 3b16336 commit a476856
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions micro-rdk/src/common/ota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ use crate::esp32::esp_idf_svc::{
ota::{EspFirmwareInfoLoader, EspOta},
sys::EspError,
};
use async_io::Timer;
use http_body_util::{BodyExt, Empty};
use hyper::{body::Bytes, client::conn::http2, Request};
use once_cell::sync::Lazy;
use std::time::Duration;
use thiserror::Error;
#[cfg(not(feature = "esp32"))]
use {bincode::Decode, futures_lite::AsyncWriteExt};

// TODO(RSDK-9200): set according to active partition scheme
const OTA_MAX_IMAGE_SIZE: usize = 1024 * 1024 * 4; // 4MB
const CONN_RETRY_SECS: u64 = 60;
const SIZEOF_APPDESC: usize = 256;
const MAX_VER_LEN: usize = 128;
pub const OTA_MODEL_TYPE: &str = "ota_service";
Expand Down Expand Up @@ -271,20 +274,39 @@ impl<S: OtaMetadataStorage> OtaService<S> {
uri = hyper::Uri::from_parts(parts).map_err(|e| OtaError::Other(e.to_string()))?;
};

let io = self
.connector
.connect_to(&uri)
.map_err(|e| OtaError::Other(e.to_string()))?
.await
.map_err(|e| OtaError::Other(e.to_string()))?;
let (mut sender, conn) = loop {
match self.connector.connect_to(&uri) {
Ok(connection) => {
match connection.await {
Ok(io) => {
match http2::Builder::new(self.exec.clone())
.max_frame_size(16_384) // lowest configurable
.timer(H2Timer)
.handshake(io)
.await
{
Ok(pair) => break pair,
Err(e) => {
log::error!("failed to build http request: {}", e);
}
}
}
Err(e) => {
log::error!("failed to create tcp stream: {}", e);
}
}
}
Err(e) => {
log::error!("failed to create http connection: {}", e);
}
};

let (mut sender, conn) = {
http2::Builder::new(self.exec.clone())
.max_frame_size(16_384) // lowest configurable value
.timer(H2Timer)
.handshake(io)
.await
.map_err(|e| OtaError::Other(e.to_string()))?
log::debug!(
"retry establishing http connection to {} in {} seconds",
&uri,
CONN_RETRY_SECS
);
Timer::after(Duration::from_secs(CONN_RETRY_SECS)).await;
};

let conn = Box::new(self.exec.spawn(async move {
Expand Down

0 comments on commit a476856

Please sign in to comment.