In this lesson, you’ll learn how to build a concurrent URL pinger in Rust. This project showcases asynchronous programming by concurrently pinging multiple URLs and displaying their HTTP status codes using Rust’s async features.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Project Goals
The primary objectives for this project are:- Make HTTP GET requests to several URLs concurrently.
- Display each URL’s response status code or provide an error message if the request fails.
- Limit the number of concurrent requests to prevent overloading the system.

Components Overview
This project leverages the following components:- HTTP Client: The
reqwestlibrary is used for making HTTP GET requests. - Concurrency: The Tokio runtime’s
tokio::spawnfunction enables asynchronous task management. - Error Handling: Errors during HTTP requests are managed gracefully with informative error messages.

Setting Up the Project
Start by creating a new Rust project. Open your project in your favorite editor (e.g., VS Code) and update yourCargo.toml file to include the dependencies for Tokio and reqwest:
Implementing the URL Pinger
Below is the full implementation of our URL pinger. This code demonstrates how to perform asynchronous HTTP requests, spawn tasks for each URL, and use a semaphore to limit the number of concurrent requests.-
Dependencies and Imports
The necessary modules are imported to manage HTTP requests usingreqwestand handle asynchronous tasks usingtokio. Concurrency is controlled with a semaphore wrapped in an Arc. -
ping_urlFunction
This async function sends an HTTP GET request to the given URL and returns its status code. Errors during the request are propagated using the?operator. -
ping_urlsFunction
This function accepts a list of URLs and a concurrency limit. It creates a semaphore to restrict the number of simultaneous HTTP requests. Each URL is processed within an asynchronous task that waits for a permit before proceeding. Once the request is finished, the permit is released. -
mainFunction
The entry point of the application, which initializes the Tokio runtime, defines the target URLs (including one invalid URL for error demonstration), and callsping_urlswith a limit of 5 concurrent tasks.
Running the Program
After building the project, run it from the terminal with the following command:Ideas for Extending the Project
There are several ways to enhance your concurrent URL pinger project:- Retry Failed Requests: Implement a mechanism to retry URLs that initially fail.
- Timeouts and Delays: Add configurable timeouts or delays to handle slow responses.
- Response Time Tracking: Measure and log how long each request takes.
- Saving Results: Write the output to a file for later analysis.
- Dynamic URL Input: Allow URLs to be read from an external file or user input instead of being hardcoded.

Conclusion
In this lesson, we covered:- How to perform asynchronous operations in Rust using
asyncandawait. - How to spawn multiple concurrent tasks with
tokio::spawn. - How to control the level of concurrency with a semaphore to manage simultaneous HTTP requests.
