Skip to content

Add semaphore and max threads#38

Merged
karlTGA merged 2 commits intomasterfrom
add-semaphore-and-max-threads
Nov 26, 2025
Merged

Add semaphore and max threads#38
karlTGA merged 2 commits intomasterfrom
add-semaphore-and-max-threads

Conversation

@karlTGA
Copy link
Contributor

@karlTGA karlTGA commented Sep 12, 2025

This pull request introduces resource management and concurrency limiting features to the elevation service, allowing for better control of server resource usage, especially in containerized or resource-constrained environments. The main changes include new configuration options for limiting the number of threads and concurrent request handler. These options are now documented, supported in the codebase, and integrated into the Docker Compose setup. Additionally, the request handlers are updated to enforce concurrency limits using a semaphore.

Resource Management and Concurrency Limiting

  • Added new configuration options: MAX_THREADS (limits tokio runtime threads), MAX_CONCURRENT_HANDLERS (limits concurrent request handlers), and improved MAX_PARALLEL_PROCESSING (controls parallel batch lookups). These are now documented in README.md and supported via environment variables. [1] [2]
  • Updated docker-compose.yaml to support the new environment variables with sensible defaults and handle empty strings gracefully, improving robustness in container deployments.

Configuration Handling Improvements

  • Refactored configuration parsing in src/config.rs to treat empty environment variables as unset, preventing accidental misconfiguration when variables are left blank in Docker Compose. Added helper functions get_non_empty_env_var and parse_env_var. [1] [2]

Concurrency Enforcement in Request Handlers

  • Integrated a tokio::sync::Semaphore into all request handlers (get_status, get_elevation, post_elevations) to enforce the MAX_CONCURRENT_HANDLERS limit, ensuring the server does not become overloaded with too many simultaneous requests. [1] [2] [3] [4]

Tokio Runtime Configuration

  • Modified src/main.rs to create the tokio runtime with an optional thread limit (MAX_THREADS), allowing for fine-tuned resource usage based on environment constraints.

Route and Filter Updates

  • Updated warp route definitions to pass the semaphore to all handlers, ensuring concurrency limits are consistently applied across all endpoints. [1] [2] [3]

@karlTGA karlTGA requested review from Copilot and neppi September 12, 2025 08:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces resource management and concurrency limiting features to the elevation service to provide better control over server resource usage in containerized environments. The changes focus on adding configurable limits for tokio runtime threads and concurrent request handlers.

  • Added new configuration options: MAX_THREADS, MAX_CONCURRENT_HANDLERS, and improved MAX_PARALLEL_PROCESSING
  • Integrated semaphore-based concurrency control in all request handlers
  • Refactored configuration parsing to handle empty environment variables gracefully

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/main.rs Modified to create custom tokio runtime with thread limits and integrate semaphore for concurrency control
src/handlers.rs Updated all request handlers to use semaphore permits for limiting concurrent requests
src/config.rs Added new configuration fields and helper functions for robust environment variable parsing
docker-compose.yaml Updated environment variables with default empty values to prevent misconfiguration
README.md Documented new resource management configuration options and usage examples

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@karlTGA karlTGA merged commit ba5f95d into master Nov 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant