My app working well enough with node.js, thanks to mediasoup team.
Currently, trying to rewrite backend in rust but cannot mutate AppData or AppData on Worker initialization. Is there any best practices to use AppData in rust.
When you say âyou canât mutate AppDataâ you mean that you are able to set it initially but arenât able to modify it later on right? if yes what is the error it is throwing?
I looked at that thread it helped me to get AppData with downcast() method.
I can set initially as shown the below. The compiler just warns me as invalid left-hand side of assignment. The method might not be correct but i couldnât find any way to update AppData.
let number_of_workers = available_parallelism().unwrap_or(NonZeroUsize::new(1).unwrap());
let worker_manager = worker_manager::WorkerManager::new();
let port_interval = (RTC_PORT_RANGE.1 - RTC_PORT_RANGE.0) / number_of_workers.get() as u16;
let mut workers: Vec<Worker> = vec![];
for n in 0..number_of_workers.get() {
let mut worker_settings = WorkerSettings::default();
worker_settings.log_level = WorkerLogLevel::Debug;
worker_settings.log_tags = vec![
WorkerLogTag::Info,
WorkerLogTag::Ice,
WorkerLogTag::Dtls,
WorkerLogTag::Rtp,
WorkerLogTag::Rtcp,
WorkerLogTag::Rtx,
WorkerLogTag::Bwe,
WorkerLogTag::Score,
WorkerLogTag::Simulcast,
WorkerLogTag::Svc,
WorkerLogTag::Sctp,
WorkerLogTag::Rtx,
];
let port_range = RangeInclusive::new(
RTC_PORT_RANGE.0 + port_interval * n as u16,
RTC_PORT_RANGE.0 + port_interval * (n + 1) as u16,
);
worker_settings.rtc_ports_range = port_range;
worker_settings.app_data = AppData::new("test".to_string());
let worker = worker_manager.create_worker(worker_settings).await.unwrap();
You can change appData using worker object anytime as mentioned here:
So I will do something like this in node:
var worker = someworker;
worker.appData = {new_app_data: true}
As for this warning this may be âappDataâ instead of âapp_dataâ atleast in node, so do verify this.
If you think this still doesnât work for you then you can go to some custom solution like saving the app_data somewhere else like redis or in local variable w.r.t each worker. But the best solution is the above one if it works.
In nodejs yes i can change it anytime i want but there is no appData as field in rust. Worker struct is non-exhaustive and constructing settings as inner. I just can set it once.
I was thinking about storing as local but app_data could be better for consumers.
Ok i figured it out but seems a bit nasty.
The problem is AppData constructor wraps the generic with Arc to mutate it with memory safety way needed wrap the value with mutex at initializing worker.