Drop locks as soon as possible

This commit is contained in:
Roderick van Domburg 2021-12-27 00:11:07 +01:00
parent 059e17dca5
commit 8aa23ed0c6
No known key found for this signature in database
GPG key ID: A9EF5222A26F0451
2 changed files with 21 additions and 18 deletions

View file

@ -106,13 +106,13 @@ async fn receive_data(
drop(request.streamer); drop(request.streamer);
if request_length > 0 { if request_length > 0 {
{
let missing_range = Range::new(data_offset, request_length); let missing_range = Range::new(data_offset, request_length);
let mut download_status = shared.download_status.lock(); let mut download_status = shared.download_status.lock();
download_status.requested.subtract_range(&missing_range); download_status.requested.subtract_range(&missing_range);
shared.cond.notify_all(); shared.cond.notify_all();
} }
}
shared shared
.number_of_open_requests .number_of_open_requests
@ -172,14 +172,18 @@ impl AudioFileFetch {
let mut ranges_to_request = RangeSet::new(); let mut ranges_to_request = RangeSet::new();
ranges_to_request.add_range(&Range::new(offset, length)); ranges_to_request.add_range(&Range::new(offset, length));
// The iteration that follows spawns streamers fast, without awaiting them,
// so holding the lock for the entire scope of this function should be faster
// then locking and unlocking multiple times.
let mut download_status = self.shared.download_status.lock(); let mut download_status = self.shared.download_status.lock();
ranges_to_request.subtract_range_set(&download_status.downloaded); ranges_to_request.subtract_range_set(&download_status.downloaded);
ranges_to_request.subtract_range_set(&download_status.requested); ranges_to_request.subtract_range_set(&download_status.requested);
for range in ranges_to_request.iter() { // Likewise, checking for the URL expiry once will guarantee validity long enough.
let url = self.shared.cdn_url.try_get_url()?; let url = self.shared.cdn_url.try_get_url()?;
for range in ranges_to_request.iter() {
let streamer = self let streamer = self
.session .session
.spclient() .spclient()
@ -219,7 +223,6 @@ impl AudioFileFetch {
missing_data.add_range(&Range::new(0, self.shared.file_size)); missing_data.add_range(&Range::new(0, self.shared.file_size));
{ {
let download_status = self.shared.download_status.lock(); let download_status = self.shared.download_status.lock();
missing_data.subtract_range_set(&download_status.downloaded); missing_data.subtract_range_set(&download_status.downloaded);
missing_data.subtract_range_set(&download_status.requested); missing_data.subtract_range_set(&download_status.requested);
} }
@ -306,16 +309,16 @@ impl AudioFileFetch {
None => return Err(AudioFileError::Output.into()), None => return Err(AudioFileError::Output.into()),
} }
let mut download_status = self.shared.download_status.lock();
let received_range = Range::new(data.offset, data.data.len()); let received_range = Range::new(data.offset, data.data.len());
let full = {
let mut download_status = self.shared.download_status.lock();
download_status.downloaded.add_range(&received_range); download_status.downloaded.add_range(&received_range);
self.shared.cond.notify_all(); self.shared.cond.notify_all();
let full = download_status.downloaded.contained_length_from_value(0) download_status.downloaded.contained_length_from_value(0)
>= self.shared.file_size; >= self.shared.file_size
};
drop(download_status);
if full { if full {
self.finish()?; self.finish()?;
@ -380,8 +383,8 @@ pub(super) async fn audio_file_fetch(
initial_request.offset, initial_request.offset,
initial_request.offset + initial_request.length, initial_request.offset + initial_request.length,
); );
let mut download_status = shared.download_status.lock();
let mut download_status = shared.download_status.lock();
download_status.requested.add_range(&requested_range); download_status.requested.add_range(&requested_range);
} }

View file

@ -189,7 +189,7 @@ impl FsSizeLimiter {
} }
fn add(&self, file: &Path, size: u64) { fn add(&self, file: &Path, size: u64) {
self.limiter.lock().add(file, size, SystemTime::now()); self.limiter.lock().add(file, size, SystemTime::now())
} }
fn touch(&self, file: &Path) -> bool { fn touch(&self, file: &Path) -> bool {