Skip to content

Commit

Permalink
feat: external relay, add different (greater) cancellation timer for …
Browse files Browse the repository at this point in the history
…no data on first chunk (#104)
  • Loading branch information
ramiroaisen authored Jul 4, 2023
2 parents ee56b9a + 4a88544 commit 9aff18c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion defs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const EMAIL_VERIFICATION_VALIDITY_SECS = 3600;

export const EXTERNAL_RELAY_NO_DATA_SHUTDOWN_SECS = 10;

export const EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS = 30;
export const EXTERNAL_RELAY_NO_DATA_START_SHUTDOWN_SECS = 30;

export const EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS = 60;

export const FORWARD_IP_HEADER = "x-openstream-forwarded-ip";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
flex: none;
width: min(30%, 8rem);
aspect-ratio: 1;
margin: 1rem 2rem 1rem 1rem;
margin: 1rem 1.5rem 1rem 1rem;
border-radius: 0.5rem;;
background-size: contain;
background-position: center;
Expand Down
6 changes: 5 additions & 1 deletion rs/config/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub const STREAM_IP_CONNECTIONS_LIMIT: u64 = 8;
pub const PLAYLIST_NO_LISTENERS_SHUTDOWN_DELAY_SECS: u64 = 10;

#[const_register]
pub const EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS: u64 = 30;
pub const EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS: u64 = 60;

/// delay to shutdown a relay session when it run out of listeners
#[const_register]
Expand All @@ -53,6 +53,10 @@ pub const RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS: u64 = 10;
#[const_register]
pub const EXTERNAL_RELAY_NO_DATA_SHUTDOWN_SECS: u64 = 10;

/// delay of which if external doesn't produce first data chunk, it will be cancelled
#[const_register]
pub const EXTERNAL_RELAY_NO_DATA_START_SHUTDOWN_SECS: u64 = 30;

/// limit of authotization, or other sensible api endpoints requests from the same ip
#[const_register]
pub const API_IP_LIMIT: usize = 60;
Expand Down
31 changes: 24 additions & 7 deletions rs/packages/media-sessions/src/external_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Instant;

use crate::{SendError, Transmitter};
use constants::{
EXTERNAL_RELAY_NO_DATA_SHUTDOWN_SECS, EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS,
/*STREAM_BURST_LENGTH,*/
EXTERNAL_RELAY_NO_DATA_SHUTDOWN_SECS, EXTERNAL_RELAY_NO_DATA_START_SHUTDOWN_SECS,
EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS, /*STREAM_BURST_LENGTH,*/
STREAM_CHUNK_SIZE, STREAM_KBITRATE,
};
use db::media_session::MediaSession;
Expand Down Expand Up @@ -188,11 +188,26 @@ pub fn run_external_relay_session(
// let chunks = chunks.rated(STREAM_KBITRATE * 1000);
// tokio::pin!(chunks);

async fn chunk_timeout(first_chunk: bool) {
if first_chunk {
tokio::time::sleep(tokio::time::Duration::from_secs(
EXTERNAL_RELAY_NO_DATA_START_SHUTDOWN_SECS,
))
.await
} else {
tokio::time::sleep(tokio::time::Duration::from_secs(
EXTERNAL_RELAY_NO_DATA_SHUTDOWN_SECS,
))
.await
}
}

let mut first_chunk = true;
loop {
let chunk = tokio::select! {
_ = tokio::time::sleep(tokio::time::Duration::from_secs(EXTERNAL_RELAY_NO_DATA_SHUTDOWN_SECS)) => {
_ = chunk_timeout(first_chunk) => {
info!(
"shutting down external-relay for station {} (no data received in specified window)",
"shutting down external-relay for station {} (no data received in specified start window)",
station_id
);
break;
Expand All @@ -201,6 +216,8 @@ pub fn run_external_relay_session(
chunk = chunks.next() => chunk
};

first_chunk = false;

match chunk {
None => break,
Some(Err(_e)) => break,
Expand All @@ -221,9 +238,9 @@ pub fn run_external_relay_session(
> EXTERNAL_RELAY_NO_LISTENERS_SHUTDOWN_DELAY_SECS
{
info!(
"shutting down external-relay for station {} (no listeners shutdown delay elapsed)",
station_id
);
"shutting down external-relay for station {} (no listeners shutdown delay elapsed)",
station_id
);
break;
} else {
continue;
Expand Down

0 comments on commit 9aff18c

Please sign in to comment.