Skip to main content

pong_rs/task/http/
http_executor.rs

1use crate::executor::Executor;
2use crate::ping_error::PingError;
3use crate::task::http::http_ping::HttpPing;
4use async_trait::async_trait;
5use log::trace;
6use std::time::Duration;
7
8#[derive(Clone)]
9pub struct HttpExecutor {
10    urn: String,
11    http_ping: HttpPing,
12    timeout: Duration,
13}
14
15impl HttpExecutor {
16    /// 构造函数
17    /// # 参数
18    /// * `urn` - 要请求的URN地址,格式为 `<method>:<url>`,例如: `GET:http://127.0.0.1:8080`
19    /// * `timeout` - 一个 `Duration`,表示超时时间
20    pub fn new(urn: String, timeout: Duration) -> Self {
21        let http_ping = HttpPing::new(urn.clone());
22
23        Self {
24            http_ping,
25            urn,
26            timeout,
27        }
28    }
29}
30
31#[async_trait]
32impl Executor for HttpExecutor {
33    fn get_name(&self) -> String {
34        String::from("HTTP")
35    }
36
37    async fn exec(&self) -> Result<(), PingError> {
38        trace!("开始执行 HTTP 任务: ping {}", self.urn);
39        self.http_ping.ping(self.timeout).await
40    }
41}