mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 04:17:10 +02:00
simplify and improve the http error
This commit is contained in:
parent
e433fd53e6
commit
e2a1bbae37
3 changed files with 17 additions and 16 deletions
|
@ -1,4 +1,4 @@
|
|||
use std::{num::NonZeroUsize, time::Duration};
|
||||
use std::num::NonZeroUsize;
|
||||
|
||||
use rand::{rngs::StdRng, Rng, SeedableRng};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
|
@ -25,10 +25,10 @@ impl Drop for Permit {
|
|||
|
||||
impl SearchQueue {
|
||||
pub fn new(capacity: usize, paralellism: NonZeroUsize) -> Self {
|
||||
// We can make the search requests wait until we're available.
|
||||
// they're going to wait anyway right after, so let's not allocate any
|
||||
// RAM by keeping a capacity of 1.
|
||||
// Search requests are going to wait until we're available anyway,
|
||||
// so let's not allocate any RAM and keep a capacity of 1.
|
||||
let (sender, receiver) = mpsc::channel(1);
|
||||
|
||||
tokio::task::spawn(Self::run(capacity, paralellism, receiver));
|
||||
Self { sender, capacity }
|
||||
}
|
||||
|
@ -48,27 +48,22 @@ impl SearchQueue {
|
|||
tokio::select! {
|
||||
search_request = receive_new_searches.recv() => {
|
||||
let search_request = search_request.unwrap();
|
||||
println!("queue contains {} elements and already running {}", queue.len(), searches_running);
|
||||
if searches_running < usize::from(parallelism) && queue.is_empty() {
|
||||
println!("We can process the search straight away");
|
||||
searches_running += 1;
|
||||
// if the search requests die it's not a hard error on our side
|
||||
let _ = search_request.send(Permit { sender: sender.clone() });
|
||||
continue;
|
||||
}
|
||||
if queue.len() >= capacity {
|
||||
println!("we're above capacity, dropping a random request");
|
||||
let remove = rng.gen_range(0..queue.len());
|
||||
let thing = queue.swap_remove(remove); // this will drop the channel and notify the search that it won't be processed
|
||||
drop(thing);
|
||||
}
|
||||
println!("pushed a new search request to the queue {}", queue.len());
|
||||
queue.push(search_request);
|
||||
},
|
||||
_ = search_finished.recv() => {
|
||||
searches_running = searches_running.saturating_sub(1);
|
||||
if !queue.is_empty() {
|
||||
println!("processed an element in the queue");
|
||||
let remove = rng.gen_range(0..queue.len());
|
||||
let channel = queue.swap_remove(remove);
|
||||
let _ = channel.send(Permit { sender: sender.clone() });
|
||||
|
@ -81,8 +76,6 @@ impl SearchQueue {
|
|||
pub async fn try_get_search_permit(&self) -> Result<Permit, MeilisearchHttpError> {
|
||||
let (sender, receiver) = oneshot::channel();
|
||||
self.sender.send(sender).await.map_err(|_| MeilisearchHttpError::SearchLimiterIsDown)?;
|
||||
receiver.await.map_err(|_| {
|
||||
MeilisearchHttpError::TooManySearchRequests(self.capacity, Duration::from_secs(10))
|
||||
})
|
||||
receiver.await.map_err(|_| MeilisearchHttpError::TooManySearchRequests(self.capacity))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue