pub struct FetchPool { /* private fields */ }Expand description
A concurrency-limited, priority-ordered download scheduler.
Wraps an HttpClient and adds a bounded queue that dispatches
the most important requests first.
§Example
use rustial_engine::{FetchPool, HttpClient};
let pool = FetchPool::new(my_http_client, 6);
// Batch enqueue -- no requests are sent yet.
pool.enqueue("https://tile.example.com/10/512/340.png".into(), 0.0);
pool.enqueue("https://tile.example.com/10/513/340.png".into(), 1.0);
// Dispatch the highest-priority requests up to the limit.
pool.flush();
// Later, in the frame loop:
let completed = pool.poll(); // also flushes freed slots
for (url, result) in completed {
// handle response
}Implementations§
Source§impl FetchPool
impl FetchPool
Sourcepub fn new(client: Box<dyn HttpClient>, max_concurrent: usize) -> Self
pub fn new(client: Box<dyn HttpClient>, max_concurrent: usize) -> Self
Create a new pool wrapping client with at most max_concurrent
simultaneous downloads.
A max_concurrent of 0 is silently clamped to 1.
Sourcepub fn enqueue(&self, request: HttpRequest, priority: f64)
pub fn enqueue(&self, request: HttpRequest, priority: f64)
Add a URL to the priority queue.
Lower priority = dispatched sooner. Duplicate URLs (already
queued or in-flight) are silently ignored.
The request is not sent until flush is called
(or implicitly via poll). This allows the caller
to batch-enqueue a frame’s worth of tiles before dispatching, so
the priority heap can sort them correctly.
Sourcepub fn flush(&self)
pub fn flush(&self)
Dispatch queued requests up to the concurrency limit.
The highest-priority (lowest score) requests are sent first.
Call this once per frame after all enqueue
calls for that frame.
Sourcepub fn cancel(&self, url: &str) -> bool
pub fn cancel(&self, url: &str) -> bool
Remove a URL from the pending queue if it has not been sent yet.
Returns true if the URL was found and removed. Already in-flight
requests are not affected.
Sourcepub fn force_cancel(&self, url: &str)
pub fn force_cancel(&self, url: &str)
Remove a URL from the dedup set regardless of whether it is queued or in-flight.
If the URL is still in the queue it is also removed from the
heap. If it is already in-flight, the concurrency slot is
reclaimed immediately so that new requests can be dispatched
without waiting for the ghost HTTP response to arrive. The
URL is recorded in an internal set so that poll
does not double-decrement in_flight when the ghost response
eventually completes.
This allows the same URL to be re-enqueued immediately, which is essential when the tile manager cancels a pending tile and then re-requests it on a subsequent frame.
Sourcepub fn clear_queue(&self)
pub fn clear_queue(&self)
Discard all queued (not yet sent) requests.
In-flight requests are unaffected – they will still appear in
future poll results.
Sourcepub fn queued_count(&self) -> usize
pub fn queued_count(&self) -> usize
Number of requests waiting in the queue (not yet sent).
Sourcepub fn max_concurrent(&self) -> usize
pub fn max_concurrent(&self) -> usize
Maximum number of concurrent in-flight requests.
Sourcepub fn in_flight_count(&self) -> usize
pub fn in_flight_count(&self) -> usize
Number of requests currently in-flight (sent, awaiting response).
Sourcepub fn known_count(&self) -> usize
pub fn known_count(&self) -> usize
Number of URLs currently tracked by the dedup set.
Sourcepub fn cancelled_in_flight_count(&self) -> usize
pub fn cancelled_in_flight_count(&self) -> usize
Number of URLs recorded as force-cancelled while in-flight.
Sourcepub fn is_known(&self, url: &str) -> bool
pub fn is_known(&self, url: &str) -> bool
Returns true if url is known to the pool (queued or in-flight).
Sourcepub fn poll(&self) -> Vec<(String, Result<HttpResponse, String>)>
pub fn poll(&self) -> Vec<(String, Result<HttpResponse, String>)>
Poll the underlying HttpClient for completed responses and
release their concurrency slots.
Any freed slots are immediately filled from the priority queue (implicit flush).
Returns (url, Result<HttpResponse, String>) for each completed
request.