Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: standlone server #454

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ mime_guess = { version = "2.0", optional = true }
sentry = "0.34"

[features]
default = ["console", "gateway", "media", "connector", "cert_utils"]
default = ["console", "gateway", "media", "connector", "standalone", "cert_utils"]
standalone = ["console", "gateway", "media", "connector"]
gateway = ["media-server-gateway", "media-server-connector", "quinn_vnet", "node_metrics", "maxminddb", "rust-embed", "media-server-multi-tenancy"]
media = ["media-server-runner", "media-server-record", "quinn_vnet", "node_metrics", "rtpengine-ngcontrol"]
console = []
Expand Down
11 changes: 11 additions & 0 deletions bin/media_single.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RUST_LOG=atm0s_sdn_network=error,info \
RUST_BACKTRACE=1 \
cargo run -- \
--sdn-zone-id 0 \
--sdn-zone-node-id 1 \
--workers 1 \
--http-port 3000 \
media \
--enable-token-api \
--disable-gateway-agent \
--disable-connector-agent
3 changes: 3 additions & 0 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

#[tokio::main(flavor = "current_thread")]
async fn main() {
rustls::crypto::ring::default_provider().install_default().expect("should install ring as default");

Check warning on line 73 in bin/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/main.rs#L73

Added line #L73 was not covered by tests
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "info");
}
Expand Down Expand Up @@ -147,6 +148,8 @@
log::error!("create cert error {:?}", e);
}
}
#[cfg(feature = "standalone")]
server::ServerType::Standalone(args) => server::run_standalone(workers, node, args).await,

Check warning on line 152 in bin/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/main.rs#L151-L152

Added lines #L151 - L152 were not covered by tests
}
})
.await;
Expand Down
14 changes: 10 additions & 4 deletions bin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use clap::Subcommand;
#[cfg(feature = "cert_utils")]
mod cert;
#[cfg(feature = "connector")]
mod connector;
pub mod connector;
#[cfg(feature = "console")]
mod console;
pub mod console;
#[cfg(feature = "gateway")]
mod gateway;
pub mod gateway;
#[cfg(feature = "media")]
mod media;
pub mod media;
#[cfg(feature = "standalone")]
pub mod standalone;

#[cfg(feature = "cert_utils")]
pub use cert::run_cert_utils;
Expand All @@ -21,6 +23,8 @@ pub use console::{run_console_server, storage as console_storage};
pub use gateway::run_media_gateway;
#[cfg(feature = "media")]
pub use media::run_media_server;
#[cfg(feature = "standalone")]
pub use standalone::run_standalone;

#[derive(Debug, Subcommand)]
pub enum ServerType {
Expand All @@ -34,4 +38,6 @@ pub enum ServerType {
Media(media::Args),
#[cfg(feature = "cert_utils")]
Cert(cert::Args),
#[cfg(feature = "standalone")]
Standalone(standalone::Args),
}
20 changes: 9 additions & 11 deletions bin/src/server/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,44 @@
pub struct Args {
/// DB Uri
#[arg(env, long, default_value = "sqlite://connector.db?mode=rwc")]
db_uri: String,
pub db_uri: String,

Check warning on line 47 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L47

Added line #L47 was not covered by tests

/// S3 Uri
#[arg(env, long, default_value = "http://user:pass@localhost:9000/bucket/path/?path_style=true")]
s3_uri: String,
pub s3_uri: String,

Check warning on line 51 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L51

Added line #L51 was not covered by tests

/// Hook Uri.
/// If set, will send hook event to this uri. example: http://localhost:8080/hook
#[arg(env, long)]
hook_uri: Option<String>,
pub hook_uri: Option<String>,

/// Hook workers
#[arg(env, long, default_value_t = 8)]
hook_workers: usize,
pub hook_workers: usize,

Check warning on line 60 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L60

Added line #L60 was not covered by tests

/// Hook body type
#[arg(env, long, default_value = "protobuf-json")]
hook_body_type: HookBodyType,
pub hook_body_type: HookBodyType,

Check warning on line 64 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L64

Added line #L64 was not covered by tests

/// Destroy room after no-one online, default is 2 minutes
#[arg(env, long, default_value_t = 120_000)]
destroy_room_after_ms: u64,
pub destroy_room_after_ms: u64,

Check warning on line 68 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L68

Added line #L68 was not covered by tests

/// Storage tick interval, default is 1 minute
/// This is used for clearing ended room
#[arg(env, long, default_value_t = 60_000)]
storage_tick_interval_ms: u64,
pub storage_tick_interval_ms: u64,

Check warning on line 73 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L73

Added line #L73 was not covered by tests

/// multi-tenancy sync endpoint
#[arg(env, long)]
multi_tenancy_sync: Option<String>,
pub multi_tenancy_sync: Option<String>,

/// multi-tenancy sync endpoint
#[arg(env, long, default_value_t = 30_000)]
multi_tenancy_sync_interval_ms: u64,
pub multi_tenancy_sync_interval_ms: u64,

Check warning on line 81 in bin/src/server/connector.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/connector.rs#L81

Added line #L81 was not covered by tests
}

pub async fn run_media_connector(workers: usize, node: NodeConfig, args: Args) {
rustls::crypto::ring::default_provider().install_default().expect("should install ring as default");

let app_storage = if let Some(url) = args.multi_tenancy_sync {
let app_storage = Arc::new(MultiTenancyStorage::new());
let mut app_sync = MultiTenancySync::new(app_storage.clone(), url, Duration::from_millis(args.multi_tenancy_sync_interval_ms));
Expand Down
2 changes: 0 additions & 2 deletions bin/src/server/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type TW = ();
pub struct Args {}

pub async fn run_console_server(workers: usize, http_port: Option<u16>, node: NodeConfig, _args: Args) {
rustls::crypto::ring::default_provider().install_default().expect("should install ring as default");

let storage = StorageShared::default();

let node_id = node.node_id;
Expand Down
30 changes: 15 additions & 15 deletions bin/src/server/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,42 @@
pub struct Args {
/// Location latitude.
#[arg(env, long, default_value_t = 0.0)]
lat: f32,
pub lat: f32,

Check warning on line 55 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L55

Added line #L55 was not covered by tests

/// Location longitude.
#[arg(env, long, default_value_t = 0.0)]
lon: f32,
pub lon: f32,

Check warning on line 59 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L59

Added line #L59 was not covered by tests

/// Path to the GeoIP database.
#[arg(env, long, default_value = "./maxminddb-data/GeoLite2-City.mmdb")]
geo_db: String,
pub geo_db: String,

Check warning on line 63 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L63

Added line #L63 was not covered by tests

/// Maximum CPU usage (in percent) allowed for routing to a media node or gateway node.
#[arg(env, long, default_value_t = 60)]
max_cpu: u8,
pub max_cpu: u8,

Check warning on line 67 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L67

Added line #L67 was not covered by tests

/// Maximum memory usage (in percent) allowed for routing to a media node or gateway node.
#[arg(env, long, default_value_t = 80)]
max_memory: u8,
pub max_memory: u8,

Check warning on line 71 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L71

Added line #L71 was not covered by tests

/// Maximum disk usage (in percent) allowed for routing to a media node or gateway node.
#[arg(env, long, default_value_t = 90)]
max_disk: u8,
pub max_disk: u8,

Check warning on line 75 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L75

Added line #L75 was not covered by tests

/// The port for binding the RTPengine command UDP socket.
#[arg(env, long)]
rtpengine_cmd_addr: Option<SocketAddr>,
pub rtpengine_cmd_addr: Option<SocketAddr>,

/// multi-tenancy sync endpoint
#[arg(env, long)]
multi_tenancy_sync: Option<String>,
pub multi_tenancy_sync: Option<String>,

/// multi-tenancy sync endpoint
#[arg(env, long, default_value_t = 30_000)]
multi_tenancy_sync_interval_ms: u64,
pub multi_tenancy_sync_interval_ms: u64,

Check warning on line 87 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L87

Added line #L87 was not covered by tests
}

pub async fn run_media_gateway(workers: usize, http_port: Option<u16>, node: NodeConfig, args: Args) {
rustls::crypto::ring::default_provider().install_default().expect("should install ring as default");

let default_cluster_cert_buf = include_bytes!("../../certs/cluster.cert");
let default_cluster_key_buf = include_bytes!("../../certs/cluster.key");
let default_cluster_cert = CertificateDer::from(default_cluster_cert_buf.to_vec());
Expand All @@ -101,13 +99,15 @@
let edge_secure = Arc::new(MediaEdgeSecureJwt::from(node.secret.as_bytes()));

let app_storage = if let Some(url) = args.multi_tenancy_sync {
log::info!("[MediaGateway] multi-tenancy sync is enabled, using url: {}", url);

Check warning on line 102 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L102

Added line #L102 was not covered by tests
let app_storage = Arc::new(MultiTenancyStorage::new());
let mut app_sync = MultiTenancySync::new(app_storage.clone(), url, Duration::from_millis(args.multi_tenancy_sync_interval_ms));
tokio::spawn(async move {
app_sync.run_loop().await;
});
app_storage
} else {
log::info!("[MediaGateway] multi-tenancy sync is disabled, using single tenant with secret: {}", node.secret);

Check warning on line 110 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L110

Added line #L110 was not covered by tests
Arc::new(MultiTenancyStorage::new_with_single(&node.secret, None))
};
let gateway_secure = MediaGatewaySecureJwt::new(node.secret.as_bytes(), app_storage.clone());
Expand All @@ -130,10 +130,10 @@
let rtpengine_udp = NgUdpTransport::new(ngproto_addr).await;
let secure2 = edge_secure.clone();
tokio::spawn(async move {
log::info!("[MediaServer] start ng_controller task");
log::info!("[MediaGateway] start ng_controller task");

Check warning on line 133 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L133

Added line #L133 was not covered by tests
let mut server = NgControllerServer::new(rtpengine_udp, secure2, req_tx);
while server.recv().await.is_some() {}
log::info!("[MediaServer] stop ng_controller task");
log::info!("[MediaGateway] stop ng_controller task");

Check warning on line 136 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L136

Added line #L136 was not covered by tests
});
}

Expand Down Expand Up @@ -248,7 +248,7 @@

tokio::spawn(async move {
let res = local_rpc_processor.process_req(conn_part, param).await;
res_tx.send(res).print_err2("answer http request error");
res_tx.send(res).print_err2("[MediaGateway] answer http request error");

Check warning on line 251 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L251

Added line #L251 was not covered by tests
});
}
while let Ok(control) = connector_agent_rx.try_recv() {
Expand All @@ -271,7 +271,7 @@
},
SdnExtOut::FeaturesEvent(_, FeaturesEvent::Socket(event)) => {
if let Err(e) = vnet_tx.try_send(event) {
log::error!("[MediaEdge] forward Sdn SocketEvent error {:?}", e);
log::error!("[MediaGateway] forward Sdn SocketEvent error {:?}", e);

Check warning on line 274 in bin/src/server/gateway.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/gateway.rs#L274

Added line #L274 was not covered by tests
}
}
_ => {}
Expand Down
30 changes: 19 additions & 11 deletions bin/src/server/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,53 @@
pub struct Args {
/// Enables the Token API, which allows token generation.
#[arg(env, long)]
enable_token_api: bool,
pub enable_token_api: bool,

Check warning on line 47 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L47

Added line #L47 was not covered by tests

/// Enables WebRTC ICE Lite mode.
#[arg(env, long)]
ice_lite: bool,
pub ice_lite: bool,

Check warning on line 51 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L51

Added line #L51 was not covered by tests

/// The seed port for binding the WebRTC UDP socket. The port will increment by one for each worker.
/// Default: 0, which assigns the port randomly.
/// If set to 20000, each worker will be assigned a unique port: worker0: 20000, worker1: 20001, worker2: 20002, ...
#[arg(env, long, default_value_t = 0)]
webrtc_port_seed: u16,
pub webrtc_port_seed: u16,

Check warning on line 57 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L57

Added line #L57 was not covered by tests

/// The port for binding the RTPengine command UDP socket.
#[arg(env, long)]
rtpengine_cmd_addr: Option<SocketAddr>,
pub rtpengine_cmd_addr: Option<SocketAddr>,

/// The IP address for RTPengine RTP listening.
/// Default: 127.0.0.1
#[arg(env, long, default_value = "127.0.0.1")]
rtpengine_rtp_ip: IpAddr,
pub rtpengine_rtp_ip: IpAddr,

Check warning on line 66 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L66

Added line #L66 was not covered by tests

/// Maximum concurrent connections per CPU core.
#[arg(env, long, default_value_t = 200)]
ccu_per_core: u32,
pub ccu_per_core: u32,

Check warning on line 70 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L70

Added line #L70 was not covered by tests

/// Directory for storing cached recordings.
#[arg(env, long, default_value = "./record_cache/")]
record_cache: String,
pub record_cache: String,

Check warning on line 74 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L74

Added line #L74 was not covered by tests

/// Maximum size of the recording cache in bytes.
#[arg(env, long, default_value_t = 100_000_000)]
record_mem_max_size: usize,
pub record_mem_max_size: usize,

Check warning on line 78 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L78

Added line #L78 was not covered by tests

/// Number of workers for uploading recordings.
#[arg(env, long, default_value_t = 5)]
record_upload_worker: usize,
pub record_upload_worker: usize,

Check warning on line 82 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L82

Added line #L82 was not covered by tests

/// Enables the Gateway Agent service.
#[arg(env, long)]
pub disable_gateway_agent: bool,

Check warning on line 86 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L86

Added line #L86 was not covered by tests

/// Enables the Connector Agent service.
#[arg(env, long)]
pub disable_connector_agent: bool,

Check warning on line 90 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L90

Added line #L90 was not covered by tests
}

pub async fn run_media_server(workers: usize, http_port: Option<u16>, node: NodeConfig, args: Args) {
rustls::crypto::ring::default_provider().install_default().expect("should install ring as default");

let default_cluster_cert_buf = include_bytes!("../../certs/cluster.cert");
let default_cluster_key_buf = include_bytes!("../../certs/cluster.key");
let default_cluster_cert = CertificateDer::from(default_cluster_cert_buf.to_vec());
Expand Down Expand Up @@ -147,6 +153,8 @@
ice_lite: args.ice_lite,
secure: secure.clone(),
max_live: HashMap::from([(ServiceKind::Webrtc, workers as u32 * args.ccu_per_core), (ServiceKind::RtpEngine, workers as u32 * args.ccu_per_core)]),
enable_gateway_agent: !args.disable_gateway_agent,
enable_connector_agent: !args.disable_connector_agent,

Check warning on line 157 in bin/src/server/media.rs

View check run for this annotation

Codecov / codecov/patch

bin/src/server/media.rs#L156-L157

Added lines #L156 - L157 were not covered by tests
},
};
controller.add_worker::<_, _, MediaRuntimeWorker<_>, PollingBackend<_, 128, 512>>(Duration::from_millis(1), cfg, None);
Expand Down
Loading
Loading