Limit concurrency of torrent initialization

This commit is contained in:
Igor Katson 2024-08-15 18:55:17 +01:00
parent 37ee8b70ba
commit 726a5e14f9
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 36 additions and 3 deletions

View file

@ -208,6 +208,7 @@ impl ManagedTorrent {
peer_rx: Option<PeerStream>,
start_paused: bool,
live_cancellation_token: CancellationToken,
init_semaphore: Arc<tokio::sync::Semaphore>,
) -> anyhow::Result<()> {
let mut g = self.locked.write();
@ -283,10 +284,16 @@ impl ManagedTorrent {
let t = self.clone();
let span = self.info().span.clone();
let token = live_cancellation_token.clone();
spawn_with_cancel(
error_span!(parent: span.clone(), "initialize_and_start"),
token.clone(),
async move {
let _permit = init_semaphore
.acquire()
.await
.context("bug: concurrent init semaphore was closed")?;
match init.check().await {
Ok(paused) => {
let mut g = t.locked.write();
@ -344,7 +351,12 @@ impl ManagedTorrent {
drop(g);
// Recurse.
self.start(peer_rx, start_paused, live_cancellation_token)
self.start(
peer_rx,
start_paused,
live_cancellation_token,
init_semaphore,
)
}
ManagedTorrentState::None => bail!("bug: torrent is in empty state"),
}