Skip to content

Add a concurrency limit to Process.ProxyRequest#123

Merged
mostlygeek merged 1 commit intomainfrom
concurrency-limit
May 13, 2025
Merged

Add a concurrency limit to Process.ProxyRequest#123
mostlygeek merged 1 commit intomainfrom
concurrency-limit

Conversation

@mostlygeek
Copy link
Copy Markdown
Owner

@mostlygeek mostlygeek commented May 13, 2025

Part of the stability and reliability improvements work.

llama-swap does not have a concurrent connection limit, or a connection timeout to the upstream. When the upstream does not respond those connections will be kept open, waiting for a response. This PR adds a default connection limit of 10. After 10 concurrent requests the response will respond with a 429 Too Many Requests.

This can be overridden in the configuration like so:

models: 
   my_model: 
      cmd: ...
      concurrencyLimit: 100

Note: This PR comes out when working with llama.cpp vscode extension for FIM. After restarting llama-swap, hundreds of connections, some waiting for hours, wound up being dropped.

Summary by CodeRabbit

  • New Features

    • Introduced a configurable concurrency limit for handling HTTP requests, allowing users to control the maximum number of simultaneous requests processed.
    • Requests exceeding the concurrency limit are now rejected with an HTTP 429 (Too Many Requests) response.
  • Tests

    • Added tests to verify correct enforcement of the concurrency limit and appropriate response behavior.

@mostlygeek mostlygeek added the enhancement New feature or request label May 13, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented May 13, 2025

Walkthrough

A concurrency limit feature was introduced to control the number of simultaneous HTTP requests handled by the proxy. This involved adding a ConcurrencyLimit field to the configuration, implementing a semaphore-based limit in the request handler, and adding a test to verify that requests exceeding the limit receive a 429 response.

Changes

File(s) Change Summary
proxy/config.go Added ConcurrencyLimit integer field to ModelConfig struct with YAML tag concurrencyLimit.
proxy/process.go Added a semaphore channel to Process for concurrency limiting; enforced limit in ProxyRequest; initialized in NewProcess.
proxy/process_test.go Added TestProcess_ConcurrencyLimit to test rejection of requests exceeding the concurrency limit.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Proxy (Process)
    participant Backend

    Client->>Proxy (Process): HTTP Request
    alt Concurrency slot available
        Proxy (Process)->>Proxy (Process): Acquire semaphore slot
        Proxy (Process)->>Backend: Forward request
        Backend-->>Proxy (Process): Response
        Proxy (Process)->>Proxy (Process): Release semaphore slot
        Proxy (Process)-->>Client: Response
    else Concurrency limit reached
        Proxy (Process)-->>Client: HTTP 429 Too Many Requests
    end
Loading

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
proxy/process_test.go (1)

344-374: Well-designed test for concurrency limiting

The test effectively validates the concurrency limiting behavior by:

  1. Setting the limit to 1
  2. Verifying the semaphore capacity
  3. Occupying the semaphore with a slow request
  4. Confirming that additional requests receive a 429 status code

Consider adding an additional test case with a higher concurrency limit (e.g., 2 or 3) to verify that multiple requests can be processed simultaneously up to the limit, and only excess requests are rejected.

proxy/process.go (1)

432-438: Effective implementation of concurrency limiting

The non-blocking send and receive on the semaphore is the correct approach for limiting concurrency. The implementation also:

  1. Returns the appropriate 429 status code
  2. Uses proper error messaging
  3. Ensures the semaphore is released after request processing via defer

Consider adding request metrics for rejected requests (due to concurrency limits) to help with monitoring and capacity planning.

+// Track metrics for rejected requests
 http.Error(w, "Too many requests", http.StatusTooManyRequests)
+p.proxyLogger.Debugf("<%s> request %s - rejected due to concurrency limit", p.ID, r.RequestURI)
 return
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cb876c1 and 1fb382e.

📒 Files selected for processing (3)
  • proxy/config.go (1 hunks)
  • proxy/process.go (3 hunks)
  • proxy/process_test.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
proxy/process_test.go (1)
proxy/process.go (1)
  • NewProcess (65-89)
🔇 Additional comments (4)
proxy/config.go (1)

26-28: Good implementation of configuration for concurrency limiting

The new ConcurrencyLimit field added to ModelConfig is well-documented and correctly tagged for YAML deserialization. This aligns with the PR objective of making the concurrency limit configurable at the model level.

proxy/process.go (3)

61-62: Good field naming and documentation

The field name clearly describes its purpose for managing concurrency limits.


67-72: Well-implemented default concurrency limit

Setting a reasonable default (10) and providing debug logging when using the default is a good practice. This aligns with the PR objective of introducing a configurable concurrency limit.


86-87: Correct semaphore initialization

The semaphore is properly initialized as a buffered channel with the configured capacity.

@mostlygeek mostlygeek merged commit 9dc4bcb into main May 13, 2025
2 checks passed
@mostlygeek mostlygeek deleted the concurrency-limit branch May 13, 2025 01:12
@coderabbitai coderabbitai bot mentioned this pull request Jan 31, 2026
rohitpaul pushed a commit to rohitpaul/llama-swap that referenced this pull request Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant