feat: turn tokio-based async logic into futures

This commit is contained in:
VC
2022-11-15 09:34:05 +01:00
parent f371b8a297
commit 7f7219ea78

View File

@@ -27,6 +27,8 @@ use tokio::{spawn, sync::Mutex};
use futures::StreamExt; use futures::StreamExt;
const DEFAULT_RATE_LIMIT: usize = 4;
/// This is where the magic happens /// This is where the magic happens
#[tokio::main] #[tokio::main]
pub async fn run(config: Config) { pub async fn run(config: Config) {
@@ -52,8 +54,8 @@ pub async fn run(config: Config) {
// retrieve the last tweet ID for the username // retrieve the last tweet ID for the username
let lconn = task_conn.lock().await; let lconn = task_conn.lock().await;
let last_tweet_id = let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)?
read_state(&lconn, &mastodon_config.twitter_screen_name, None)?.map(|r| r.tweet_id); .map(|r| r.tweet_id);
drop(lconn); drop(lconn);
// get user timeline feed (Vec<tweet>) // get user timeline feed (Vec<tweet>)
@@ -137,8 +139,9 @@ pub async fn run(config: Config) {
drop(lconn); drop(lconn);
} }
Ok::<(), ScootalooError>(()) Ok::<(), ScootalooError>(())
}); })
}).buffer_unordered(4); })
.buffer_unordered(config.scootaloo.rate_limit.unwrap_or(DEFAULT_RATE_LIMIT));
// launch and wait for every handle // launch and wait for every handle
while let Some(result) = stream.next().await { while let Some(result) = stream.next().await {