fix start without exisiting cache

This commit is contained in:
soruh 2023-06-09 20:23:33 +02:00
parent 904091c455
commit b5d2a63909

View File

@ -283,7 +283,7 @@ impl PortHandler {
self.last_update = Some(now); self.last_update = Some(now);
self.change_sender self.change_sender
.as_ref() .as_ref()
.expect("PortHandler is missing it's change_sender") .expect("PortHandler is missing its change_sender")
.send(now) .send(now)
.expect("failed to notify cache writer"); .expect("failed to notify cache writer");
} }
@ -301,15 +301,9 @@ impl PortHandler {
} }
#[allow(clippy::missing_errors_doc)] #[allow(clippy::missing_errors_doc)]
#[instrument(skip(change_sender))] pub fn load(cache: &Path) -> std::io::Result<Self> {
pub fn load(
cache: &Path,
change_sender: tokio::sync::watch::Sender<Instant>,
) -> std::io::Result<Self> {
info!("loading cache"); info!("loading cache");
let mut cache: Self = serde_json::from_reader(BufReader::new(File::open(cache)?))?; Ok(serde_json::from_reader(BufReader::new(File::open(cache)?))?)
cache.change_sender = Some(change_sender);
Ok(cache)
} }
#[must_use] #[must_use]
@ -318,10 +312,14 @@ impl PortHandler {
path: &Path, path: &Path,
change_sender: tokio::sync::watch::Sender<Instant>, change_sender: tokio::sync::watch::Sender<Instant>,
) -> Self { ) -> Self {
Self::load(path, change_sender).unwrap_or_else(|error| { let mut this = Self::load(path).unwrap_or_else(|error| {
error!(?path, %error, "failed to parse cache file"); error!(?path, %error, "failed to parse cache file");
Self::default() Self::default()
}) });
this.change_sender = Some(change_sender);
this
} }
pub fn update_allowed_ports(&mut self, allowed_ports: &AllowedList) { pub fn update_allowed_ports(&mut self, allowed_ports: &AllowedList) {