mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-06-25 05:07:12 +08:00
Remove unneeded spin_on
This commit is contained in:
@@ -735,8 +735,7 @@ async fn test_failed_downloader() {
|
||||
let mut retry_interval = NonTailList::<Duration>::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 =
|
||||
|
||||
@@ -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<T: Sized> GetRwLock for RwLock<T> {
|
||||
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<T: Sized> GetRwLock for RwLock<T> {
|
||||
return f;
|
||||
}
|
||||
Err(_) => {
|
||||
spin_on(tokio::time::sleep(Duration::new(0, 1_000_000)));
|
||||
sleep(Duration::new(0, 1_000_000));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<JsonValue>> {
|
||||
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<JsonValue>> {
|
||||
spin_on(self.aget_data_as_mut())
|
||||
}
|
||||
|
||||
async fn aget_data<'a>(&'a self) -> RwLockReadGuard<'a, Option<JsonValue>> {
|
||||
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<JsonValue>> {
|
||||
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;
|
||||
}
|
||||
|
||||
100
src/webclient.rs
100
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<String, String>> {
|
||||
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<String, String>> {
|
||||
spin_on(self.aget_headers_as_mut())
|
||||
}
|
||||
|
||||
pub async fn aget_headers<'a>(&'a self) -> RwLockReadGuard<'a, HashMap<String, String>> {
|
||||
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<String, String>> {
|
||||
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<NonTailList<Duration>>> {
|
||||
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<NonTailList<Duration>>> {
|
||||
spin_on(self.aget_retry_interval_as_mut())
|
||||
}
|
||||
|
||||
pub async fn aget_retry_interval<'a>(
|
||||
&'a self,
|
||||
) -> RwLockReadGuard<'a, Option<NonTailList<Duration>>> {
|
||||
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<NonTailList<Duration>>> {
|
||||
spin_on(self.aget_retry_interval())
|
||||
self.retry_interval.get_ref()
|
||||
}
|
||||
|
||||
pub fn get_verbose(&self) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user