Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracker Checker: handle HTTP Tracker timeouts #679

Closed
Tracked by #669 ...
josecelano opened this issue Feb 2, 2024 · 1 comment
Closed
Tracked by #669 ...

Tracker Checker: handle HTTP Tracker timeouts #679

josecelano opened this issue Feb 2, 2024 · 1 comment
Assignees
Labels
- Admin - Enjoyable to Install and Setup our Software - Developer - Torrust Improvement Experience Code Cleanup / Refactoring Tidying and Making Neat Easy Good for Newcomers Enhancement / Feature Request Something New good first issue Good for newcomers Testing Checking Torrust
Milestone

Comments

@josecelano
Copy link
Member

josecelano commented Feb 2, 2024

Parent issue: #677

You can run a Tracker Checker with:

TORRUST_CHECKER_CONFIG='{
    "udp_trackers": [],
    "http_trackers": ["http://127.0.0.1:7070"],
    "health_checks": []
}' cargo run --bin tracker_checker

The HTTP Tracker client does not have any timeout so it will wait forever.

We have to add a timeout to the HTTP client requests:

/// HTTP Tracker Client
pub struct Client {
    base_url: Url,
    reqwest: ReqwestClient,
    key: Option<Key>,
}

impl Client {
    // ...

    pub async fn announce(&self, query: &announce::Query) -> Response {
        self.get(&self.build_announce_path_and_query(query)).await
    }

    pub async fn scrape(&self, query: &scrape::Query) -> Response {
        self.get(&self.build_scrape_path_and_query(query)).await
    }

    pub async fn announce_with_header(&self, query: &Query, key: &str, value: &str) -> Response {
        self.get_with_header(&self.build_announce_path_and_query(query), key, value)
            .await
    }

    pub async fn health_check(&self) -> Response {
        self.get(&self.build_path("health_check")).await
    }

    /// # Panics
    ///
    /// This method fails if there was an error while sending request.
    pub async fn get(&self, path: &str) -> Response {
        self.reqwest.get(self.build_url(path)).send().await.unwrap()
    }

    /// # Panics
    ///
    /// This method fails if there was an error while sending request.
    pub async fn get_with_header(&self, path: &str, key: &str, value: &str) -> Response {
        self.reqwest
            .get(self.build_url(path))
            .header(key, value)
            .send()
            .await
            .unwrap()
    }

    // ...
}

You can add a timeout to all reqwest.get like this in the Health Check:

HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();

You can force the error by adding a sleep in the handler:

async fn handle(
    tracker: &Arc<Tracker>,
    announce_request: &Announce,
    client_ip_sources: &ClientIpSources,
    maybe_key: Option<Key>,
) -> Response {
    thread::sleep(time::Duration::from_secs(7));

    let announce_data = match handle_announce(tracker, announce_request, client_ip_sources, maybe_key).await {
        Ok(announce_data) => announce_data,
        Err(error) => return error.into_response(),
    };
    build_response(announce_request, announce_data)
}

This issue should be implemented after refactoring the HTTP Tracker client to return errors instead of panicking.

@josecelano josecelano added Enhancement / Feature Request Something New Easy Good for Newcomers Code Cleanup / Refactoring Tidying and Making Neat - Developer - Torrust Improvement Experience - Admin - Enjoyable to Install and Setup our Software Testing Checking Torrust good first issue Good for newcomers labels Feb 2, 2024
@josecelano josecelano added this to the v3.1.0 milestone Feb 2, 2024
@da2ce7 da2ce7 self-assigned this Mar 28, 2024
@josecelano
Copy link
Member Author

This was implemented by @da2ce7. I've tested it with a "sleep" in the handler.

Announce:

2024-09-11T08:21:44.159164Z  INFO torrust_tracker::console::clients::checker::service: Running checks for trackers ...
[
  {
    "Http": {
      "Err": {
        "url": "http://127.0.0.1:7070/",
        "results": [
          [
            "Announce",
            {
              "Err": "Http request did not receive a response within the timeout: ResponseError { err: reqwest::Error { kind: Request, url: \"http://127.0.0.1:7070/announce?info_hash=%9C8B%22%13%E3%0B%FF%21%2B0%C3%60%D2o%9A%02%13d%22&peer_addr=192.168.1.88&downloaded=0&uploaded=0&peer_id=%2DqB00000000000000001&port=17548&left=0&event=completed&compact=0\", source: TimedOut } }"
            }
          ],
          [
            "Scrape",
            {
              "Ok": null
            }
          ]
        ]
      }
    }
  }
]

Scrape:

2024-09-11T08:24:45.952450Z  INFO torrust_tracker::console::clients::checker::service: Running checks for trackers ...
[
  {
    "Http": {
      "Err": {
        "url": "http://127.0.0.1:7070/",
        "results": [
          [
            "Announce",
            {
              "Ok": null
            }
          ],
          [
            "Scrape",
            {
              "Err": "Http request did not receive a response within the timeout: ResponseError { err: reqwest::Error { kind: Request, url: \"http://127.0.0.1:7070/scrape?info_hash=%9C8B%22%13%E3%0B%FF%21%2B0%C3%60%D2o%9A%02%13d%22\", source: TimedOut } }"
            }
          ]
        ]
      }
    }
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- Admin - Enjoyable to Install and Setup our Software - Developer - Torrust Improvement Experience Code Cleanup / Refactoring Tidying and Making Neat Easy Good for Newcomers Enhancement / Feature Request Something New good first issue Good for newcomers Testing Checking Torrust
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants