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

Finish tokio migration #606

Merged

Conversation

Johannesd3
Copy link
Contributor

@Johannesd3 Johannesd3 commented Feb 21, 2021

This PR finishes the tokio migration. The application is working again - after all it was easier than I thought -, but I'm pretty sure it's not perfect yet.

I had to figure out how to make the player blocking again because there were some mysterious bugs in the async version (now it's again unimportant whether the sinks are Send, but it's nice to have).

@sashahilton00 @ashthespy How do we proceed? Before merging it into dev it should be tested more thoroughly, and I would like to do some refactoring.

@sashahilton00
Copy link
Member

Leaving this up to ash - I haven't been following the tokio migration work closely.

@Johannesd3 Johannesd3 closed this Feb 22, 2021
@Johannesd3 Johannesd3 deleted the tokio_connect_migration branch February 22, 2021 11:12
@Johannesd3 Johannesd3 restored the tokio_connect_migration branch February 22, 2021 11:13
@Johannesd3 Johannesd3 reopened this Feb 22, 2021
@ashthespy
Copy link
Member

@Johannesd3 I'm going to merge this into tokio_migration and then try and merge dev in. But can test only in the weekend.

Thanks again! :-)

@ashthespy ashthespy merged commit c9b3b95 into librespot-org:tokio_migration Feb 23, 2021
@Johannesd3 Johannesd3 deleted the tokio_connect_migration branch February 23, 2021 08:32
@Johannesd3
Copy link
Contributor Author

Did you try it already? The rodio backend seems like a tough nut.

@Johannesd3
Copy link
Contributor Author

Or if you want, I could try to merge it this evening.

@ashthespy
Copy link
Member

Rodio back-end seems to compile, but had to leave to work before testing in earnest. Will have time only later this week for proper testing..

}
}

#[tokio::main]
Copy link
Contributor

@leshow leshow Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what we were doing before but maybe we should change this? This is going to start one OS thread per logical core for tokio. On my machine that'd be 16 threads. Seems like librespot probably doesn't need those kinds of resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do you think is a reasonable number of threads? I think with tokio-core it was only single-threaded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single threaded sounds good to me, you can do this with the current_thread builder. I'm not sure if there's a macro associated with this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thread count can be configured using #[tokio::main(flavor = "multi_thread", worker_threads = 10)]. To get the single thread behaviour back, #[tokio::main(flavor = "current_thread")] can be used. Would it be worth adding a CLI argument such as --multithread XX where XX is the thread count desired by the user, otherwise defaulting to single thread operation? We'd probably need to check user input didn't exceed the number of system cores if we opted to do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we anyway network bound? Not sure if having more threads is going to help much. Although would be great to have some numbers to compare. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many people try hard to use all cores in the best way, and encounter strange bugs while trying this. We don't have concurrency bugs, but still we're discussing whether to make it single-threaded. 😀

Copy link
Contributor

@leshow leshow Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow, the type of runtime you need will be dictated by your requirements. Nothing about the project README indicates that we want a high-throughput server capable of handling tens of thousands of req/s, so I think having the default configuration such that the footprint is minimal is good for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mainly joking. It would be interesting to see the differences between different numbers of threads, but I agree, 16 threads are not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you would like to create a PR? (the commit history of this branch looks too monotonous anyway)

Copy link
Contributor

@leshow leshow Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry my comment went to the wrong place, sarcasm is sometimes hard over the internet 😄 Do you want to just update this PR to use [tokio::main(flavor = "current_thread")] I think that would be sufficient if @sashahilton00 agrees

edit: Oh, I just realized this has all been merged now. Apologies.

audio_filter: audio_filter,
event_senders: [event_sender].to_vec(),
};
let handle = thread::spawn(move || {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with this code but why does this use thread::spawn as opposed to using a tokio task? Or rather, if it's using blocking code, should it not be spawned with spawn_blocking into the tokio blocking threadpool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely blocking (I tried to rewrite it as future, but encountered strange bugs). Also it's a long-running thread, powering the playback, so an own thread is ok, I think.


tokio::spawn(async move {
if let Some(data) = loader.load_track(spotify_id, position_ms).await {
std::thread::spawn(move || {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about thread::spawn here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants