This project implements a Priority Queue Service using Rust, Actix Web, and Redis. The service allows you to manage URLs with different priorities, handle retries with exponential backoff, and manage dead-letter queues for failed retries. It provides a RESTful API for adding URLs to the queue, fetching URLs for processing, and retrying failed URLs.
- Priority Queue: URLs are stored with a priority score. Lower scores have higher priority.
- Retry Logic: If processing a URL fails, it can be retried with an exponential backoff strategy.
- Dead-Letter Queue: URLs that fail too many times are moved to a dead-letter queue for further inspection or manual processing.
- Asynchronous Operations: Uses asynchronous Redis operations and Actix Web for efficient handling of HTTP requests.
priority_queue_service/
├── src/
│ ├── main.rs
│ ├── lib.rs
│ ├── api/
│ │ ├── mod.rs
│ │ ├── add_url.rs
│ │ ├── fetch_url.rs
│ │ ├── retry_url.rs
│ ├── services/
│ │ ├── mod.rs
│ │ ├── priority_queue_service.rs
│ ├── models/
│ │ ├── mod.rs
│ │ ├── url_data.rs
│ ├── config/
│ │ ├── mod.rs
│ │ ├── config.rs
│ └── utils/
│ ├── mod.rs
│ ├── logging.rs
│ ├── time.rs
├── Cargo.toml
└── .env
main.rs
: Entry point of the application.lib.rs
: Library file that includes all modules.api/
: Contains Actix Web route handlers.add_url.rs
: Handles the/add_url
endpoint.fetch_url.rs
: Handles the/fetch_url
endpoint.retry_url.rs
: Handles the/retry_url
endpoint.
services/
: Contains the core service logic.priority_queue_service.rs
: Implements thePriorityQueueService
.
models/
: Contains data models used in the API.url_data.rs
: Defines theUrlData
struct.
config/
: Handles configuration settings.config.rs
: Manages configuration such as Redis URL.
utils/
: Contains utility functions.logging.rs
: Initializes the logging system.time.rs
: Handles time-related utilities.
-
Clone the repository:
git clone https://github.com/your-username/priority_queue_service.git cd priority_queue_service
-
Set up environment variables:
Create a
.env
file in the root directory with the following content:REDIS_URL=redis://127.0.0.1/
-
Install dependencies:
Cargo will handle dependencies for you. Just build the project:
cargo build
-
Run the server:
cargo run
The server will start on
http://127.0.0.1:8080
.
-
Endpoint:
POST /add_url
-
Description: Adds a new URL to the priority queue.
-
Request Body:
{ "url": "http://example.com", "priority": 5 }
-
Response:
{ "message": "URL added to the queue: http://example.com" }
-
Endpoint:
GET /fetch_url
-
Description: Fetches the next URL from the queue for processing.
-
Response:
{ "url": "http://example.com", "score": 4.9 }
If no URLs are available:
{ "message": "No URLs available in the queue" }
-
Endpoint:
POST /retry_url
-
Description: Retries a URL that failed to process.
-
Request Body:
{ "url": "http://example.com", "priority": 5 }
-
Response:
{ "message": "URL scheduled for retry: http://example.com" }
Add a URL:
curl -X POST http://localhost:8080/add_url -H "Content-Type: application/json" -d '{"url":"http://example.com", "priority":3}'
Fetch a URL:
curl -X GET http://localhost:8080/fetch_url
Retry a Failed URL:
curl -X POST http://localhost:8080/retry_url -H "Content-Type: application/json" -d '{"url":"http://example.com", "priority":2}'
Logs are handled using the env_logger
crate. Ensure that logging is set up by calling utils::logging::init_logger()
in the main.rs
file.
- Redis connection settings and other configurations are managed through environment variables. Adjust these settings in the
.env
file or withinsrc/config/config.rs
.