fix: don't starve high priority requests (#2660)

This commit is contained in:
Matthias Seitz
2023-05-13 19:18:24 +02:00
committed by GitHub
parent 99e6ee7a86
commit a5dbbe4187
2 changed files with 26 additions and 1 deletions

View File

@@ -8,3 +8,15 @@ pub enum Priority {
/// Queued from the front for download requests.
High,
}
impl Priority {
/// Returns `true` if this is [Priority::High]
pub fn is_high(&self) -> bool {
matches!(self, Priority::High)
}
/// Returns `true` if this is [Priority::Normal]
pub fn is_normal(&self) -> bool {
matches!(self, Priority::Normal)
}
}

View File

@@ -158,7 +158,14 @@ impl StateFetcher {
match self.download_requests_rx.poll_next_unpin(cx) {
Poll::Ready(Some(request)) => match request.get_priority() {
Priority::High => {
self.queued_requests.push_front(request);
// find the first normal request and queue before, add this request to
// the back of the high-priority queue
let pos = self
.queued_requests
.iter()
.position(|req| req.is_normal_priority())
.unwrap_or(0);
self.queued_requests.insert(pos, request);
}
Priority::Normal => {
self.queued_requests.push_back(request);
@@ -377,12 +384,18 @@ impl DownloadRequest {
}
}
/// Returns the requested priority of this request
fn get_priority(&self) -> &Priority {
match self {
DownloadRequest::GetBlockHeaders { priority, .. } => priority,
DownloadRequest::GetBlockBodies { priority, .. } => priority,
}
}
/// Returns `true` if this request is normal priority.
fn is_normal_priority(&self) -> bool {
self.get_priority().is_normal()
}
}
/// An action the syncer can emit.