-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
348 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use bytes::Bytes; | ||
use std::{sync::Arc, time::Duration}; | ||
|
||
use super::Cache; | ||
use crate::{ | ||
models::{torrent::TorrentStatsDict, tracker::FullScrapeResponse}, | ||
storage::Processor, | ||
worker::{FullScrapeProcessor, Task, TaskOutput, Worker}, | ||
}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct FullScrapeCache { | ||
data: Option<Arc<bytes::Bytes>>, | ||
} | ||
|
||
impl FullScrapeCache { | ||
pub fn new(data: bytes::Bytes) -> FullScrapeCache { | ||
FullScrapeCache { | ||
data: Some(Arc::new(data)), | ||
} | ||
} | ||
} | ||
|
||
pub async fn refresh(cache: Arc<Cache>, worker: Arc<Worker>, expires_in: Duration) { | ||
let should_refresh = { | ||
let mut cache = cache.full_scrape.write().await; | ||
match cache.as_ref() { | ||
Some(_) if cache.is_expired() && !cache.is_refreshing() => { | ||
cache.set_refreshing(); | ||
true | ||
} | ||
None => true, | ||
_ => false, | ||
} | ||
}; | ||
|
||
if !should_refresh { | ||
return; | ||
} | ||
|
||
let task = Task::FullScrape(Box::new(FullScrapeResponse::new())); | ||
let data = match worker.work(task).await { | ||
Ok(TaskOutput::FullScrape(mut handler)) => handler.output().unwrap_or_default(), | ||
_ => Bytes::new(), | ||
}; | ||
|
||
let mut cache = cache.full_scrape.write().await; | ||
|
||
cache.set( | ||
FullScrapeCache::new(data), | ||
Some(std::time::Instant::now() + expires_in), | ||
); | ||
} | ||
|
||
impl std::ops::Deref for FullScrapeCache { | ||
type Target = Option<Arc<bytes::Bytes>>; | ||
fn deref(&self) -> &Self::Target { | ||
&self.data | ||
} | ||
} | ||
|
||
impl std::ops::DerefMut for FullScrapeCache { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.data | ||
} | ||
} | ||
|
||
impl FullScrapeProcessor for FullScrapeResponse { | ||
fn as_processor(&mut self) -> &mut dyn Processor<TorrentStatsDict> { | ||
self | ||
} | ||
|
||
fn output(&mut self) -> Option<Bytes> { | ||
self.output() | ||
} | ||
} | ||
|
||
impl Processor<TorrentStatsDict> for FullScrapeResponse { | ||
fn process(&mut self, input: &TorrentStatsDict) -> bool { | ||
self.bencode(input); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
pub mod full_scrape; | ||
|
||
use tokio::sync::RwLock; | ||
|
||
use self::full_scrape::FullScrapeCache; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
pub struct Cache { | ||
pub full_scrape: RwLock<CacheEntry<FullScrapeCache>>, | ||
} | ||
|
||
impl Cache { | ||
pub fn new() -> Cache { | ||
Cache { | ||
full_scrape: RwLock::new(CacheEntry::default()), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct CacheEntry<T> { | ||
data: T, | ||
expires: Option<std::time::Instant>, | ||
refreshing: AtomicBool, | ||
} | ||
|
||
impl<T> CacheEntry<T> | ||
where | ||
T: Send + Sync, | ||
{ | ||
pub fn new(data: T, expires: Option<std::time::Instant>) -> CacheEntry<T> { | ||
CacheEntry { | ||
data, | ||
expires, | ||
refreshing: AtomicBool::new(false), | ||
} | ||
} | ||
|
||
pub fn is_expired(&self) -> bool { | ||
match self.expires { | ||
Some(expires) => expires < std::time::Instant::now(), | ||
None => true, | ||
} | ||
} | ||
|
||
pub fn is_refreshing(&self) -> bool { | ||
self.refreshing.load(Ordering::SeqCst) | ||
} | ||
|
||
pub fn set_refreshing(&mut self) { | ||
self.refreshing.store(true, Ordering::SeqCst); | ||
} | ||
|
||
pub fn set(&mut self, data: T, expires: Option<std::time::Instant>) { | ||
self.data = data; | ||
self.expires = expires; | ||
self.refreshing.store(false, Ordering::SeqCst); | ||
} | ||
} | ||
|
||
impl<T> From<(T, std::time::Instant)> for CacheEntry<T> | ||
where | ||
T: Send + Sync, | ||
{ | ||
fn from(value: (T, std::time::Instant)) -> CacheEntry<T> { | ||
CacheEntry::new(value.0, Some(value.1)) | ||
} | ||
} | ||
|
||
impl<T> std::ops::Deref for CacheEntry<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
&self.data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
mod cache; | ||
mod http; | ||
mod udp; | ||
|
||
use crate::config::TSConfig; | ||
use crate::worker::Worker; | ||
use std::sync::Arc; | ||
|
||
use self::cache::Cache; | ||
pub use self::http::HttpServer; | ||
pub use self::udp::UdpServer; | ||
|
||
#[derive(Clone)] | ||
pub struct State { | ||
pub worker: Arc<Worker>, | ||
pub config: Arc<TSConfig>, | ||
pub cache: Arc<Cache>, | ||
} | ||
|
||
impl State { | ||
pub fn new(worker: Arc<Worker>, config: Arc<TSConfig>) -> State { | ||
State { | ||
worker, | ||
config, | ||
cache: Arc::new(Cache::new()), | ||
} | ||
} | ||
} |
Oops, something went wrong.