mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-01 00:32:05 +00:00
fix: bound direct command fetches
This commit is contained in:
@@ -516,6 +516,15 @@ pub async fn fetch_once(url: &str) -> Result<Option<Fetched>, FetchError> {
|
|||||||
fetch_with_attempts(url, 1).await
|
fetch_with_attempts(url, 1).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A small admission gate for `/test` and `/debug`, which run directly in
|
||||||
|
/// dispatcher handlers instead of the URL worker pool.
|
||||||
|
pub async fn acquire_command_fetch_slot() -> tokio::sync::OwnedSemaphorePermit {
|
||||||
|
std::sync::Arc::clone(&COMMAND_FETCH_SLOTS)
|
||||||
|
.acquire_owned()
|
||||||
|
.await
|
||||||
|
.expect("command fetch gate closed")
|
||||||
|
}
|
||||||
|
|
||||||
/// Total attempts of the retried [`fetch`] (3: the initial try plus two).
|
/// Total attempts of the retried [`fetch`] (3: the initial try plus two).
|
||||||
const MAX_FETCH_ATTEMPTS: u32 = 3;
|
const MAX_FETCH_ATTEMPTS: u32 = 3;
|
||||||
|
|
||||||
@@ -530,6 +539,8 @@ const MAX_FETCH_ATTEMPTS: u32 = 3;
|
|||||||
/// deliberately simple: the wait is bounded by the same retries.
|
/// deliberately simple: the wait is bounded by the same retries.
|
||||||
static FETCH_SLOTS: LazyLock<tokio::sync::Semaphore> =
|
static FETCH_SLOTS: LazyLock<tokio::sync::Semaphore> =
|
||||||
LazyLock::new(|| tokio::sync::Semaphore::new(8));
|
LazyLock::new(|| tokio::sync::Semaphore::new(8));
|
||||||
|
static COMMAND_FETCH_SLOTS: LazyLock<std::sync::Arc<tokio::sync::Semaphore>> =
|
||||||
|
LazyLock::new(|| std::sync::Arc::new(tokio::sync::Semaphore::new(2)));
|
||||||
|
|
||||||
async fn fetch_with_attempts(url: &str, attempts: u32) -> Result<Option<Fetched>, FetchError> {
|
async fn fetch_with_attempts(url: &str, attempts: u32) -> Result<Option<Fetched>, FetchError> {
|
||||||
// Wall time of the whole fetch, retry backoff included: the ugoira encode
|
// Wall time of the whole fetch, retry backoff included: the ugoira encode
|
||||||
|
|||||||
@@ -598,11 +598,7 @@ pub(crate) async fn execute_command(
|
|||||||
.await?;
|
.await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// The ordinary link pipeline with the chat's post-send actions
|
let _command_fetch = x_media::site::acquire_command_fetch_slot().await;
|
||||||
// suppressed: the media is sent (and cached) like a normal link,
|
|
||||||
// but nothing is forwarded to the channel and no
|
|
||||||
// edit-before-forward prompt opens. Info level echoes the
|
|
||||||
// normalized key (never the raw URL) per the logging convention.
|
|
||||||
log::info!("test: sending [key={}]", log_key(url));
|
log::info!("test: sending [key={}]", log_key(url));
|
||||||
url_media(
|
url_media(
|
||||||
ctx,
|
ctx,
|
||||||
@@ -612,6 +608,7 @@ pub(crate) async fn execute_command(
|
|||||||
PostSend::Suppressed,
|
PostSend::Suppressed,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
drop(_command_fetch);
|
||||||
}
|
}
|
||||||
Command::Debug(arg) => {
|
Command::Debug(arg) => {
|
||||||
let url = arg.trim();
|
let url = arg.trim();
|
||||||
@@ -625,8 +622,7 @@ pub(crate) async fn execute_command(
|
|||||||
.await?;
|
.await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// Debug tool: report the parse result only — nothing is sent,
|
let _command_fetch = x_media::site::acquire_command_fetch_slot().await;
|
||||||
// cached or forwarded.
|
|
||||||
log::info!("debug: parsing [key={}]", log_key(url));
|
log::info!("debug: parsing [key={}]", log_key(url));
|
||||||
match x_media::site::fetch(url).await {
|
match x_media::site::fetch(url).await {
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user