[Rust] Cannot set AppData

Hey,

I am newbie for rust and mediasoup.

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.

May be this is related:

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?

1 Like

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.

1 Like

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.

Thank you for your time and helps

You can only modify the object, but cannot replace it with another (the setter method will throw an exception).

There is a function app_data().

As i noticed im newbie and got error for my every tries.
app_data field takes &self as input and it seems immutable according to documentation.

I asked some rust users they say same thing you cannot mutate it.

If you have a method for this one i can try it asap

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.

#[derive(Debug)]
struct TestStruct {
    a: i32,
    b: i32,
}

impl TestStruct {
    fn new() -> Self {
        Self { a: 0, b: 0 }
    }
}

worker_settings.app_data = AppData::new(Mutex::new(TestStruct::new()));

            worker
                .app_data()
                .downcast_ref::<Mutex<TestStruct>>()
                .expect("downcast")
                .lock()
                .expect("lock")
                .a = 5;

Here is the output:

worker appdata Mutex {
    data: TestStruct {
        a: 5,
        b: 0,
    },
    poisoned: false,
    ..
}

Thanks for everyone times.

1 Like