summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-14 01:29:55 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-14 01:31:22 +0800
commit4259ab8d858bcbafe6a6c3f8d3be8979c61d80ac (patch)
tree00da4cb35d0ff5f859a419c04c9ff79fbb400bab
parent4f75bc1b3fd85a7b54cf6bbb6e895c2deb353299 (diff)
refactor(connector): move checking prev poll to connector instead of instance of it
chore(connector): move sleep to the top of the loop
-rw-r--r--src/client/connector.rs21
-rw-r--r--src/client/connector/connectors/httpconnector.rs27
2 files changed, 30 insertions, 18 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs
index cae944b..88b1939 100644
--- a/src/client/connector.rs
+++ b/src/client/connector.rs
@@ -15,14 +15,24 @@ pub(crate) trait Connection {
type Err: std::fmt::Display;
// should use channel to notify update
- fn update(&self) -> Option<Result<Self::Ok, Self::Err>>; // None if no update(content is same)
+ fn update(&self) -> Result<Self::Ok, Self::Err>; // None if no update(content is same)
fn ntfy(&self, data: String);
- fn r#loop(&self) -> ! {
+ fn is_prev(&mut self, str: &Self::Ok) -> bool;
+
+ fn r#loop(&mut self) -> ! {
loop {
+ // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO
+ // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication
+ // the cons of the current approch is that the first time we've entered, it would start sleeping
+ sleep(Duration::from_millis(500));
match self.update() {
- Some(Result::Ok(s)) => {
+ Result::Ok(s) => {
+ if self.is_prev(&s) {
+ continue;
+ }
+
let mut it = s.as_ref().split_whitespace();
let task = match it.next() {
Some(t) => t,
@@ -39,11 +49,8 @@ pub(crate) trait Connection {
.unwrap(),
);
}
- Some(Result::Err(e)) => eprintln!("Error: {}", e),
- _ => {}
+ Result::Err(e) => eprintln!("Error: {}", e),
}
-
- sleep(Duration::from_millis(500));
}
}
}
diff --git a/src/client/connector/connectors/httpconnector.rs b/src/client/connector/connectors/httpconnector.rs
index 923df94..e391771 100644
--- a/src/client/connector/connectors/httpconnector.rs
+++ b/src/client/connector/connectors/httpconnector.rs
@@ -1,12 +1,12 @@
use reqwest::{Error, StatusCode, Url, blocking::Client};
-use crate::connector::*;
+use crate::connector::{self, *};
use std::{thread::sleep, time::Duration};
pub(crate) struct HTTPConnector {
- prev: String,
url: Url,
client: Client,
+ prev: <HTTPConnector as connector::Connection>::Ok,
}
impl HTTPConnector {
@@ -37,7 +37,7 @@ impl std::fmt::Display for Err {
impl Connection for HTTPConnector {
type Err = Err;
- fn update(&self) -> Option<Result<Self::Ok, Self::Err>> {
+ fn update(&self) -> Result<Self::Ok, Self::Err> {
let resp = match self.client.get(self.url.as_str()).send() {
Ok(resp) => match resp.status() {
StatusCode::TOO_MANY_REQUESTS => {
@@ -46,25 +46,30 @@ impl Connection for HTTPConnector {
}
status => {
if !status.is_success() {
- return Some(Result::Err(Err::StatusCode(status)));
+ return Result::Err(Err::StatusCode(status));
}
match resp.text() {
Ok(text) => text,
- Err(e) => return Some(Result::Err(Err::Reqwest(e))),
+ Err(e) => return Result::Err(Err::Reqwest(e)),
}
}
},
- Err(e) => return Some(Result::Err(Err::Reqwest(e))),
+ Err(e) => return Result::Err(Err::Reqwest(e)),
};
- if resp == self.prev {
- return None;
- }
-
- Some(Result::Ok(resp))
+ Result::Ok(resp)
}
fn ntfy(&self, data: String) {
println!("{}", data)
}
+
+ fn is_prev(&mut self, str: &Self::Ok) -> bool {
+ if str == &self.prev {
+ return true;
+ } else {
+ self.prev = str.to_owned()
+ }
+ false
+ }
}