tyk icon indicating copy to clipboard operation
tyk copied to clipboard

Add support for MaxConnsPerHost transport config

Open zalbiraw opened this issue 2 years ago • 10 comments

User description

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • [ ] I ensured that the documentation is up to date
  • [ ] I explained why this PR updates go.mod in detail with reasoning why it's required
  • [ ] I would like a code coverage CI quality gate exception and have explained why

PR Type

Enhancement


Description

  • Added a new configuration option MaxConnsPerHost to the Config struct in config/config.go.
  • Documented the new MaxConnsPerHost field to explain its purpose and usage.
  • Integrated the MaxConnsPerHost configuration into the http.Transport setup in gateway/reverse_proxy.go.

Changes walkthrough 📝

Relevant files
Enhancement
config.go
Add `MaxConnsPerHost` field to `Config` struct                     

config/config.go

  • Added MaxConnsPerHost field to Config struct.
  • Documented the new MaxConnsPerHost field.
  • +3/-0     
    reverse_proxy.go
    Integrate `MaxConnsPerHost` into `http.Transport` setup   

    gateway/reverse_proxy.go

  • Integrated MaxConnsPerHost configuration into the http.Transport
    setup.
  • +1/-0     

    💡 PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    zalbiraw avatar Oct 26 '23 16:10 zalbiraw

    Apply Sweep Rules to your PR?

    • [ ] Apply: Leftover TODOs in the code should be handled.
    • [ ] Apply: All new business logic should have corresponding unit tests in the tests/ directory.
    • [ ] Apply: Any clearly inefficient or repeated code should be optimized or refactored.

    API Changes

    --- prev.txt	2024-06-04 21:06:20.620549961 +0000
    +++ current.txt	2024-06-04 21:06:17.668526964 +0000
    @@ -5289,6 +5289,9 @@
     	MaxIdleConns int `bson:"max_idle_connections" json:"max_idle_connections"`
     	// Maximum idle connections, per API, per upstream, between Tyk and Upstream. Default:100
     	MaxIdleConnsPerHost int `bson:"max_idle_connections_per_host" json:"max_idle_connections_per_host"`
    +	// Limits the total number of connections per host, including connections in the dialing, active and idele states.
    +	// On limit violations, dials will block.
    +	MaxConnsPerHost int `bson:"max_connections_per_host" json:"max_connections_per_host"`
     	// Maximum connection time. If set it will force gateway reconnect to the upstream.
     	MaxConnTime int64 `json:"max_conn_time"`
     
    

    github-actions[bot] avatar Oct 26 '23 16:10 github-actions[bot]

    PR Analysis

    • 🎯 Main theme: Adding support for MaxConnsPerHost transport configuration
    • 📝 PR summary: This PR introduces a new configuration option, MaxConnsPerHost, which limits the total number of connections per host. This includes connections in the dialing, active, and idle states. When the limit is violated, dials will block.
    • 📌 Type of PR: Enhancement
    • 🧪 Relevant tests added: No
    • ⏱️ Estimated effort to review [1-5]: 2, the PR is straightforward and introduces a new configuration option. However, it lacks tests to verify the new functionality.
    • 🔒 Security concerns: No

    PR Feedback

    • 💡 General suggestions: The PR is generally well done, but it lacks tests for the new functionality. It would be beneficial to add tests that verify the behavior when the maximum number of connections per host is reached.

    • 🤖 Code feedback:

    How to use

    To invoke the PR-Agent, add a comment using one of the following commands: /review [-i]: Request a review of your Pull Request. For an incremental review, which only considers changes since the last review, include the '-i' option. /describe: Modify the PR title and description based on the contents of the PR. /improve [--extended]: Suggest improvements to the code in the PR. Extended mode employs several calls, and provides a more thorough feedback. /ask <QUESTION>: Pose a question about the PR. /update_changelog: Update the changelog based on the PR's contents.

    To edit any configuration parameter from configuration.toml, add --config_path=new_value For example: /review --pr_reviewer.extra_instructions="focus on the file: ..." To list the possible configuration parameters, use the /config command.

    github-actions[bot] avatar Oct 26 '23 16:10 github-actions[bot]

    :boom: CI tests failed :see_no_evil:

    git-state

    all ok
    

    Please look at the run or in the Checks tab.

    github-actions[bot] avatar Oct 26 '23 16:10 github-actions[bot]

    API tests result: success :white_check_mark: Branch used: refs/pull/5690/merge Commit:
    Triggered by: pull_request (@zalbiraw) Execution page

    buger avatar Oct 26 '23 17:10 buger

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are straightforward and localized to specific configuration parameters and their usage in the transport setup. The PR modifies existing structures and functions with new configuration options, which are easy to understand and review.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    No

    🔒 Security concerns

    No

    Code feedback:
    relevant fileconfig/config.go
    suggestion      

    Consider adding a default value for MaxConnsPerHost in the comment to clarify its behavior when not explicitly set by the user. This can help in understanding the expected system behavior without diving into the code. [medium]

    relevant lineMaxConnsPerHost int `bson:"max_connections_per_host" json:"max_connections_per_host"`

    relevant filegateway/reverse_proxy.go
    suggestion      

    Ensure that setting MaxConnsPerHost to 0 (which is the default as per the comment) does not lead to unexpected behavior such as preventing any connections. If 0 is intended to represent 'unlimited', this should be explicitly handled in the logic. [important]

    relevant line

    github-actions[bot] avatar Jun 04 '24 21:06 github-actions[bot]

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Maintainability
    Correct a typo in the comment

    Correct the typo in the comment for MaxConnsPerHost. Change "idele" to "idle".

    config/config.go [787]

    -// Limits the total number of connections per host, including connections in the dialing, active and idele states.
    +// Limits the total number of connections per host, including connections in the dialing, active and idle states.
     
    
    Suggestion importance[1-10]: 10

    Why: Correcting typos in comments improves code readability and maintainability. The typo "idele" should be corrected to "idle" to ensure the comment is clear and professional.

    10
    Add a comment to document the default behavior of MaxConnsPerHost

    Consider documenting the default behavior of MaxConnsPerHost in the configuration struct
    to maintain consistency with other fields.

    gateway/reverse_proxy.go [413]

    -MaxConnsPerHost:       p.Gw.GetConfig().MaxConnsPerHost,     // default is 0
    +MaxConnsPerHost:       p.Gw.GetConfig().MaxConnsPerHost,     // default is 0, limits the total number of connections per host
     
    
    Suggestion importance[1-10]: 7

    Why: Documenting the default behavior of MaxConnsPerHost in the code improves maintainability and helps future developers understand the default settings, though it is a minor enhancement.

    7
    Possible issue
    Ensure MaxConnsPerHost is not negative to prevent unexpected behavior

    Add a check to ensure MaxConnsPerHost is not set to a negative value, which could lead to
    unexpected behavior.

    gateway/reverse_proxy.go [413]

    -MaxConnsPerHost:       p.Gw.GetConfig().MaxConnsPerHost,     // default is 0
    +MaxConnsPerHost:       max(p.Gw.GetConfig().MaxConnsPerHost, 0), // Ensure non-negative, default is 0
     
    
    Suggestion importance[1-10]: 9

    Why: Adding a check to ensure MaxConnsPerHost is not negative is crucial for preventing potential runtime errors and unexpected behavior, thus improving the robustness of the code.

    9
    Enhancement
    Add a default value comment for MaxConnsPerHost

    Add a default value comment for MaxConnsPerHost to align with the documentation style of
    other configuration parameters.

    config/config.go [789]

    -MaxConnsPerHost int `bson:"max_connections_per_host" json:"max_connections_per_host"`
    +MaxConnsPerHost int `bson:"max_connections_per_host" json:"max_connections_per_host"` // Default: 0
     
    
    Suggestion importance[1-10]: 7

    Why: Adding a default value comment for MaxConnsPerHost aligns with the documentation style of other configuration parameters, improving consistency and maintainability.

    7

    github-actions[bot] avatar Jun 04 '24 21:06 github-actions[bot]

    :boom: CI tests failed :see_no_evil:

    git-state

    all ok
    

    Please look at the run or in the Checks tab.

    github-actions[bot] avatar Jun 04 '24 21:06 github-actions[bot]

    :boom: CI tests failed :see_no_evil:

    git-state

    all ok
    

    Please look at the run or in the Checks tab.

    github-actions[bot] avatar Jun 04 '24 21:06 github-actions[bot]

    :boom: CI tests failed :see_no_evil:

    git-state

    all ok
    

    Please look at the run or in the Checks tab.

    github-actions[bot] avatar Jun 04 '24 21:06 github-actions[bot]