diff --git a/src/downloader/downloader.rs b/src/downloader/downloader.rs index 10fc9d9..e9a7027 100644 --- a/src/downloader/downloader.rs +++ b/src/downloader/downloader.rs @@ -735,8 +735,7 @@ async fn test_failed_downloader() { let mut retry_interval = NonTailList::::default(); retry_interval += Duration::new(0, 0); client - .aget_retry_interval_as_mut() - .await + .get_retry_interval_as_mut() .replace(retry_interval.clone()); client.set_retry(1); let downloader = diff --git a/src/ext/rw_lock.rs b/src/ext/rw_lock.rs index 617e366..63a974f 100644 --- a/src/ext/rw_lock.rs +++ b/src/ext/rw_lock.rs @@ -1,7 +1,7 @@ -use spin_on::spin_on; use std::sync::RwLock; use std::sync::RwLockReadGuard; use std::sync::RwLockWriteGuard; +use std::thread::sleep; use std::time::Duration; pub trait GetRwLock { @@ -19,7 +19,7 @@ impl GetRwLock for RwLock { return f; } Err(_) => { - spin_on(tokio::time::sleep(Duration::new(0, 1_000_000))); + sleep(Duration::new(0, 1_000_000)); } } } @@ -31,7 +31,7 @@ impl GetRwLock for RwLock { return f; } Err(_) => { - spin_on(tokio::time::sleep(Duration::new(0, 1_000_000))); + sleep(Duration::new(0, 1_000_000)); } } } diff --git a/src/pixiv_web.rs b/src/pixiv_web.rs index d6f0264..9a64e26 100644 --- a/src/pixiv_web.rs +++ b/src/pixiv_web.rs @@ -7,13 +7,9 @@ use crate::webclient::WebClient; use json::JsonValue; use reqwest::IntoUrl; use reqwest::Response; -use spin_on::spin_on; use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::sync::RwLock; -use std::sync::RwLockReadGuard; -use std::sync::RwLockWriteGuard; -use std::time::Duration; /// A client which use Pixiv's web API pub struct PixivWebClient { @@ -36,40 +32,6 @@ impl PixivWebClient { } } - async fn aget_data_as_mut<'a>(&'a self) -> RwLockWriteGuard<'a, Option> { - loop { - match self.data.try_write() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } - } - - fn get_data_as_mut<'a>(&'a self) -> RwLockWriteGuard<'a, Option> { - spin_on(self.aget_data_as_mut()) - } - - async fn aget_data<'a>(&'a self) -> RwLockReadGuard<'a, Option> { - loop { - match self.data.try_read() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } - } - - fn get_data<'a>(&'a self) -> RwLockReadGuard<'a, Option> { - spin_on(self.aget_data()) - } - pub fn is_inited(&self) -> bool { self.inited.qload() } @@ -147,7 +109,7 @@ impl PixivWebClient { p.value.as_ref().unwrap().pretty(2).as_str() ); } - self.get_data_as_mut().replace(p.value.unwrap()); + self.data.get_mut().replace(p.value.unwrap()); true } @@ -340,7 +302,7 @@ impl PixivWebClient { } pub fn logined(&self) -> bool { - let data = self.get_data(); + let data = self.data.get_ref(); if data.is_none() { return false; } diff --git a/src/webclient.rs b/src/webclient.rs index e0e88fc..10aedc0 100644 --- a/src/webclient.rs +++ b/src/webclient.rs @@ -1,15 +1,13 @@ -extern crate spin_on; - use crate::cookies::Cookie; use crate::cookies::CookieJar; use crate::ext::atomic::AtomicQuick; use crate::ext::json::ToJson; +use crate::ext::rw_lock::GetRwLock; use crate::gettext; use crate::list::NonTailList; use crate::opthelper::get_helper; use json::JsonValue; use reqwest::{Client, IntoUrl, RequestBuilder, Response}; -use spin_on::spin_on; use std::collections::HashMap; use std::convert::TryInto; use std::default::Default; @@ -107,74 +105,20 @@ impl WebClient { } } - pub async fn aget_cookies_as_mut<'a>(&'a self) -> RwLockWriteGuard<'a, CookieJar> { - loop { - match self.cookies.try_write() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } - } - pub fn get_cookies_as_mut<'a>(&'a self) -> RwLockWriteGuard<'a, CookieJar> { - spin_on(self.aget_cookies_as_mut()) - } - - pub async fn aget_cookies<'a>(&'a self) -> RwLockReadGuard<'a, CookieJar> { - loop { - match self.cookies.try_read() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } + self.cookies.get_mut() } pub fn get_cookies<'a>(&'a self) -> RwLockReadGuard<'a, CookieJar> { - spin_on(self.aget_cookies()) - } - - pub async fn aget_headers_as_mut<'a>( - &'a self, - ) -> RwLockWriteGuard<'a, HashMap> { - loop { - match self.headers.try_write() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } + self.cookies.get_ref() } pub fn get_headers_as_mut<'a>(&'a self) -> RwLockWriteGuard<'a, HashMap> { - spin_on(self.aget_headers_as_mut()) - } - - pub async fn aget_headers<'a>(&'a self) -> RwLockReadGuard<'a, HashMap> { - loop { - match self.headers.try_read() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } + self.headers.get_mut() } pub fn get_headers<'a>(&'a self) -> RwLockReadGuard<'a, HashMap> { - spin_on(self.aget_headers()) + self.headers.get_ref() } /// return retry times, 0 means disable @@ -182,44 +126,14 @@ impl WebClient { self.retry.qload() } - pub async fn aget_retry_interval_as_mut<'a>( - &'a self, - ) -> RwLockWriteGuard<'a, Option>> { - loop { - match self.retry_interval.try_write() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } - } - pub fn get_retry_interval_as_mut<'a>( &'a self, ) -> RwLockWriteGuard<'a, Option>> { - spin_on(self.aget_retry_interval_as_mut()) - } - - pub async fn aget_retry_interval<'a>( - &'a self, - ) -> RwLockReadGuard<'a, Option>> { - loop { - match self.retry_interval.try_read() { - Ok(f) => { - return f; - } - Err(_) => { - tokio::time::sleep(Duration::new(0, 1_000_000)).await; - } - } - } + self.retry_interval.get_mut() } pub fn get_retry_interval<'a>(&'a self) -> RwLockReadGuard<'a, Option>> { - spin_on(self.aget_retry_interval()) + self.retry_interval.get_ref() } pub fn get_verbose(&self) -> bool {