Skip to content

Commit

Permalink
fix: [torrust#918] revision for UDP active reqeust buffer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 authored and josecelano committed Jun 28, 2024
1 parent a897de4 commit edbf0d4
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/servers/udp/server/request_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ impl ActiveRequests {
///
/// * `abort_handle` - The `AbortHandle` for the UDP request processor task.
/// * `local_addr` - A string slice representing the local address for logging.
pub async fn force_push(&mut self, abort_handle: AbortHandle, local_addr: &str) {
pub async fn force_push(&mut self, new_task: AbortHandle, local_addr: &str) {
// Attempt to add the new handle to the buffer.
match self.rb.try_push(abort_handle) {
match self.rb.try_push(new_task) {
Ok(()) => {
// Successfully added the task, no further action needed.
}
Err(abort_handle) => {
Err(new_task) => {
// Buffer is full, attempt to make space.

let mut finished: u64 = 0;
let mut unfinished_task = None;

for removed_abort_handle in self.rb.pop_iter() {
for old_task in self.rb.pop_iter() {
// We found a finished tasks ... increase the counter and
// continue searching for more and ...
if removed_abort_handle.is_finished() {
if old_task.is_finished() {
finished += 1;
continue;
}
Expand All @@ -76,7 +76,7 @@ impl ActiveRequests {

// Recheck if it finished ... increase the counter and
// continue searching for more and ...
if removed_abort_handle.is_finished() {
if old_task.is_finished() {
finished += 1;
continue;
}
Expand All @@ -95,7 +95,7 @@ impl ActiveRequests {
// unfinished task.
if finished == 0 {
// We make place aborting this task.
removed_abort_handle.abort();
old_task.abort();

tracing::warn!(
target: UDP_TRACKER_LOG_TARGET,
Expand All @@ -111,7 +111,7 @@ impl ActiveRequests {
// buffer, so we need to re-insert in in the buffer.

// Save the unfinished task for re-entry.
unfinished_task = Some(removed_abort_handle);
unfinished_task = Some(old_task);
}

// After this point there can't be a race condition because only
Expand All @@ -125,10 +125,12 @@ impl ActiveRequests {
}

// Insert the new task, ensuring there's space.
if !abort_handle.is_finished() {
self.rb
.try_push(abort_handle)
.expect("it should remove at least one element.");
// Space has already been made for this new task in the buffer.
// One or many old task have already been finished or yielded,
// freeing space in the buffer. Or a single unfinished task has
// been aborted to make space for this new task.
if !new_task.is_finished() {
self.rb.try_push(new_task).expect("it should have space for this new task.");
}
}
};
Expand Down

0 comments on commit edbf0d4

Please sign in to comment.