Skip to content

feat: add Semaphore and concurrent data fetching utility (#294)#303

Open
MOHD-TAHA-KHAN wants to merge 1 commit intoTheAlgorithms:masterfrom
MOHD-TAHA-KHAN:feature/async-semaphore-concurrent-fetch
Open

feat: add Semaphore and concurrent data fetching utility (#294)#303
MOHD-TAHA-KHAN wants to merge 1 commit intoTheAlgorithms:masterfrom
MOHD-TAHA-KHAN:feature/async-semaphore-concurrent-fetch

Conversation

@MOHD-TAHA-KHAN
Copy link
Copy Markdown

[FEATURE]: Async Operations: Concurrent data fetching with semaphore control

📝 Description

This PR resolves issue #294. It introduces a robust Semaphore synchronization primitive and a Concurrent Data Fetching utility. These tools are designed to manage and limit the number of active asynchronous operations, which is essential for preventing resource exhaustion, hitting API rate limits, or overwhelming database connections.

🚀 Changes

  • async/Semaphore.ts: Core logic for the Semaphore class.
    • Features acquire() and release() methods.
    • Includes a run() wrapper to handle automatic permit management.
  • async/Concurrent_Data_Fetching.ts: A high-level utility function to execute batches of async tasks with a concurrency limit.
  • async/test/Semaphores.test.ts: A comprehensive test suite using Jest.
    • Verified concurrency limits.
    • Verified FIFO (First-In-First-Out) ordering.
    • Verified error handling (permits are released even if a task throws an exception).

📊 Complexity

  • Time Complexity: $O(1)$ per operation. Each acquire and release call operates in constant time.
  • Space Complexity: $O(N)$, where $N$ is the number of tasks currently waiting in the internal queue.

✅ Checklist

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have added tests that prove my feature works as intended.
  • All new and existing tests passed locally.
  • The code is documented with JSDoc for better readability.

Submitted as part of the Algorithms - TypeScript collection.

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