Fix sentry http request info
Add async bot running with semaphore
This commit is contained in:
+22
-2
@@ -4,7 +4,7 @@ use crate::{
|
||||
open_router::OpenRouterClient,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::time::Duration;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
use tracing::{error, info, instrument};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
@@ -22,11 +22,13 @@ pub struct ReviewItem {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Bot {
|
||||
config: EnvConfig,
|
||||
gitea_api: GiteaAPI,
|
||||
open_router_client: OpenRouterClient,
|
||||
http_client: reqwest::Client,
|
||||
max_concurrent: usize,
|
||||
}
|
||||
|
||||
impl Bot {
|
||||
@@ -41,6 +43,7 @@ impl Bot {
|
||||
&config.open_router_model,
|
||||
open_router_timeout,
|
||||
)?,
|
||||
max_concurrent: config.bot_max_concurrent,
|
||||
config,
|
||||
http_client: reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(gitea_timeout))
|
||||
@@ -54,8 +57,25 @@ impl Bot {
|
||||
) -> anyhow::Result<()> {
|
||||
info!("Bot started");
|
||||
|
||||
let sem = Arc::new(tokio::sync::Semaphore::new(self.max_concurrent));
|
||||
let mut tasks = tokio::task::JoinSet::new();
|
||||
|
||||
while let Some(wb) = rx.recv().await {
|
||||
self.exec(wb).await;
|
||||
let permit = sem.clone().acquire_owned().await?;
|
||||
let self_clone = self.clone();
|
||||
|
||||
tasks.spawn(async move {
|
||||
self_clone.exec(wb).await;
|
||||
drop(permit);
|
||||
});
|
||||
}
|
||||
|
||||
// Le channel est fermé : on attend que les tâches en cours se terminent
|
||||
// proprement avant de rendre la main
|
||||
while let Some(res) = tasks.join_next().await {
|
||||
if let Err(e) = res {
|
||||
error!("Task panicked: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
info!("Bot shutting down, channel closed");
|
||||
|
||||
Reference in New Issue
Block a user