semaphores
This commit is contained in:
parent
9bad158f91
commit
118e82d432
1 changed files with 10 additions and 0 deletions
|
@ -6,6 +6,8 @@ pub use album_list::AlbumListType;
|
|||
use adw::glib;
|
||||
use bytes::Bytes;
|
||||
use rand::Rng;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Semaphore;
|
||||
use tracing::{event, Level};
|
||||
|
||||
fn runtime() -> &'static tokio::runtime::Runtime {
|
||||
|
@ -56,6 +58,7 @@ impl From<reqwest::Error> for Error {
|
|||
pub struct Client {
|
||||
client: reqwest::Client,
|
||||
base_url: reqwest::Url,
|
||||
sem: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
fn random_salt(length: usize) -> String {
|
||||
|
@ -106,6 +109,7 @@ impl Client {
|
|||
.user_agent(crate::USER_AGENT)
|
||||
.build()?,
|
||||
base_url,
|
||||
sem: Arc::new(Semaphore::new(20)), // 20 requests at one time
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -121,7 +125,10 @@ impl Client {
|
|||
// note that this is why those silly bounds on T are needed, because we're
|
||||
// sending back the result of the query from another thread
|
||||
let future = request.send();
|
||||
let sem = Arc::clone(&self.sem);
|
||||
runtime().spawn(async move {
|
||||
let _permit = sem.acquire().await.unwrap();
|
||||
|
||||
// wrap this logic in a fn so we can use ?
|
||||
async fn perform<T: serde::de::DeserializeOwned + Send + 'static>(
|
||||
response: Result<reqwest::Response, reqwest::Error>,
|
||||
|
@ -195,7 +202,10 @@ impl Client {
|
|||
let (sender, receiver) = async_channel::bounded(1);
|
||||
|
||||
let future = self.client.get(self.cover_art_url(id, size)).send();
|
||||
let sem = Arc::clone(&self.sem);
|
||||
runtime().spawn(async move {
|
||||
let _permit = sem.acquire().await.unwrap();
|
||||
|
||||
async fn perform(
|
||||
response: Result<reqwest::Response, reqwest::Error>,
|
||||
) -> Result<Bytes, Error> {
|
||||
|
|
Loading…
Reference in a new issue