Skip to content

Use a single queue for message threads instead of round-robin #93

@vincentfretin

Description

@vincentfretin

I finished reading the Rust book! While reading the book I read again the code in janus-plugin-sfu and in janus-plugin-rs where you handle all the Rust - C/C++ conversion and incr/decr refcount for session/jansson/libcstring, I understand it all now, wow, beautiful :)

The last chapter Final Project: Building a Multithreaded Web Server is really interesting.
In the janus-plugin-sfu current implementation for message threads, we use a simple round-robin and a sender/receiver channel for each thread, relevant code here

janus-plugin-sfu/src/lib.rs

Lines 274 to 282 in ef815ec

thread::Builder::new()
.name(format!("sfu msg {}", i))
.spawn(move || {
for msg in messages_rx.iter() {
if let Err(e) = handle_message_async(msg) {
janus_err!("Error processing message: {}", e);
}
}
})

and here

janus-plugin-sfu/src/lib.rs

Lines 775 to 778 in ef815ec

let message_count = MESSAGE_COUNTER.fetch_add(1, Ordering::Relaxed);
let senders = MESSAGE_SENDERS.get().unwrap();
let sender = &senders[message_count % senders.len()];
sender.send(msg).ok();

While reading the chapter and the current janus-plugin-sfu code, I reached the same conclusion as
you wrote in #49 (comment)
If you're using the websocket transport in naf-janus-adapter you can indeed have lots of quick messages for process_data. Hubs didn't use this transport, so janus handled sdp offer/answer primarily.

In the Rust book chapter they use a single sender/receiver, and use an Arc<Mutex<receiver>> to really have a single queue of messages, full code at the end of the chapter

I may propose a PR to change the implementation to that, it will be a good exercise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions