Implement timeouts during read and proper retry behavior
This commit is contained in:
1 parent
8908c7f62e
commit
7fab45d5a9
3 files changed
+33
-21
No files matched your search
+13
-6
@@ -6,6 +6,7 @@ use md5;
|
||||
use self::connection::BcConnection;
|
||||
use std::io::Write;
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::time::Duration;
|
||||
|
||||
use Md5Trunc::*;
|
||||
|
||||
@@ -15,6 +16,7 @@ pub struct BcCamera {
|
||||
address: SocketAddr,
|
||||
connection: Option<BcConnection>,
|
||||
logged_in: bool,
|
||||
rx_timeout: Duration,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -67,9 +69,14 @@ impl BcCamera {
|
||||
address,
|
||||
connection: None,
|
||||
logged_in: false,
|
||||
rx_timeout: Duration::from_secs(1),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_rx_timeout(&mut self, timeout: Duration) {
|
||||
self.rx_timeout = timeout;
|
||||
}
|
||||
|
||||
pub fn connect(&mut self) -> Result<()> {
|
||||
self.connection = Some(BcConnection::new(self.address)?);
|
||||
Ok(())
|
||||
@@ -113,7 +120,7 @@ impl BcCamera {
|
||||
|
||||
sub_login.send(legacy_login)?;
|
||||
|
||||
let legacy_reply = sub_login.rx.recv()?;
|
||||
let legacy_reply = sub_login.rx.recv_timeout(self.rx_timeout)?;
|
||||
let nonce;
|
||||
match legacy_reply.body {
|
||||
BcBody::ModernMsg(ModernMsg {
|
||||
@@ -159,7 +166,7 @@ impl BcCamera {
|
||||
});
|
||||
|
||||
sub_login.send(modern_login)?;
|
||||
let modern_reply = sub_login.rx.recv()?;
|
||||
let modern_reply = sub_login.rx.recv_timeout(self.rx_timeout)?;
|
||||
|
||||
let device_info;
|
||||
match modern_reply.body {
|
||||
@@ -238,9 +245,11 @@ impl BcCamera {
|
||||
|
||||
sub_video.send(start_video)?;
|
||||
|
||||
for msg in sub_video.rx.iter() {
|
||||
loop {
|
||||
trace!("Getting video message...");
|
||||
let msg = sub_video.rx.recv_timeout(self.rx_timeout)?;
|
||||
if let Bc { body: BcBody::ModernMsg(ModernMsg { binary: Some(binary), .. }), .. } = msg {
|
||||
debug!("Got {} bytes of video data", binary.len());
|
||||
trace!("Got {} bytes of video data", binary.len());
|
||||
data_out.write_all(binary.as_slice())?;
|
||||
}
|
||||
else {
|
||||
@@ -248,8 +257,6 @@ impl BcCamera {
|
||||
debug!("Contents: {:?}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::Other("Video stream stopped"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use serde::Deserialize;
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
@@ -18,6 +19,8 @@ pub struct CameraConfig {
|
||||
|
||||
pub username: String,
|
||||
pub password: Option<String>,
|
||||
|
||||
pub timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
fn default_bind_addr() -> String {
|
||||
|
||||
+17
-15
@@ -12,7 +12,7 @@ use config::{Config, CameraConfig};
|
||||
use cmdline::Opt;
|
||||
use err_derive::Error;
|
||||
use gst::RtspServer;
|
||||
use log::error;
|
||||
use log::*;
|
||||
use std::fs;
|
||||
use std::time::Duration;
|
||||
use std::io::Write;
|
||||
@@ -52,29 +52,31 @@ fn main() -> Result<(), Error> {
|
||||
|
||||
fn camera_loop(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), Error> {
|
||||
let mut backoff = ExponentialBackoff {
|
||||
initial_interval: Duration::from_secs(5),
|
||||
max_interval: Duration::from_secs(60),
|
||||
initial_interval: Duration::from_secs(1),
|
||||
max_interval: Duration::from_secs(10),
|
||||
..Default::default()
|
||||
};
|
||||
let mut op = || {
|
||||
camera_main(camera_config, output)
|
||||
};
|
||||
op.retry(&mut backoff).map_err(|backoff_err| -> Error {
|
||||
match backoff_err {
|
||||
Transient(e) => {
|
||||
error!("Error streaming from camera {}, will retry: {}", camera_config.name, e);
|
||||
e.into()
|
||||
}
|
||||
Permanent(e) => {
|
||||
error!("Permanent error sreaming from camera {}: {}", camera_config.name, e);
|
||||
e.into()
|
||||
}
|
||||
}
|
||||
})
|
||||
let res = op.retry_notify(&mut backoff, |err, next: Duration| {
|
||||
error!("Error streaming from camera {}, will retry in {}s: {}", camera_config.name, next.as_secs(), err);
|
||||
});
|
||||
|
||||
let err = match res {
|
||||
Ok(_) => unreachable!("should never return on success"),
|
||||
Err(backoff::Error::Transient(e)) |
|
||||
Err(backoff::Error::Permanent(e)) => e,
|
||||
};
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
|
||||
fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), backoff::Error<bc_protocol::Error>> {
|
||||
let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?;
|
||||
if let Some(timeout) = camera_config.timeout {
|
||||
camera.set_rx_timeout(timeout);
|
||||
}
|
||||
|
||||
println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr);
|
||||
camera.connect()?;
|
||||
|
||||
Reference in new issue
Block a user