Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-12871, DX-1784] Add explanation of path ordering before pattern matching #5812

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

andyo-tyk
Copy link
Contributor

@andyo-tyk andyo-tyk commented Dec 9, 2024

DX-1784

Preview Link

https://deploy-preview-5812--tyk-docs.netlify.app/docs/nightly/getting-started/key-concepts/url-matching/#pattern-ordering

Description

Adding explanation of URL ordering algorithm


PR Type

Documentation


Description

  • Added a new section on "Path ordering" to explain how Tyk orders APIs and endpoints before matching requests.
  • Detailed the steps and rules used for ordering, including handling of custom domains, path lengths, and dynamic segments.
  • Included examples to clarify the ordering logic and its impact on routing.
  • Enhanced the explanation of the structure of API requests with a reference to RFC 3986 for better understanding.
  • Improved documentation to help users avoid accidental routing to incorrect endpoints.

Changes walkthrough 📝

Relevant files
Documentation
url-matching.md
Added detailed documentation on path ordering and URL matching.

tyk-docs/content/getting-started/key-concepts/url-matching.md

  • Added explanation for handling similar paths in URL matching.
  • Introduced a new section on "Path ordering" with detailed steps on how
    Tyk orders APIs and endpoints.
  • Provided examples and rules for path ordering and matching logic.
  • Clarified the structure of API requests with a reference to RFC 3986.
  • +26/-1   

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    @andyo-tyk andyo-tyk requested a review from titpetric December 9, 2024 15:49
    @andyo-tyk andyo-tyk changed the title Initial draft [TT-12871] Initial draft Dec 9, 2024
    Copy link

    netlify bot commented Dec 9, 2024

    PS. Pls add /docs/nightly to the end of url

    Name Link
    🔨 Latest commit 2b9c62d
    🔍 Latest deploy log https://app.netlify.com/sites/tyk-docs/deploys/6757119608e58a000826d906
    😎 Deploy Preview https://deploy-preview-5812--tyk-docs.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    1 similar comment
    Copy link

    netlify bot commented Dec 9, 2024

    PS. Pls add /docs/nightly to the end of url

    Name Link
    🔨 Latest commit 2b9c62d
    🔍 Latest deploy log https://app.netlify.com/sites/tyk-docs/deploys/6757119608e58a000826d906
    😎 Deploy Preview https://deploy-preview-5812--tyk-docs.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    @andyo-tyk andyo-tyk requested a review from andrei-tyk December 9, 2024 15:52
    Copy link

    netlify bot commented Dec 9, 2024

    PS. Pls add /docs/nightly to the end of url

    Name Link
    🔨 Latest commit 72c522f
    🔍 Latest deploy log https://app.netlify.com/sites/tyk-docs/deploys/67739ad795919c00089a8cc0
    😎 Deploy Preview https://deploy-preview-5812--tyk-docs.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify site configuration.

    @andyo-tyk andyo-tyk changed the title [TT-12871] Initial draft [TT-12871] Add explanation of path ordering before pattern matching Dec 20, 2024
    @andyo-tyk andyo-tyk marked this pull request as ready for review December 20, 2024 16:51
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Clarity of Path Ordering Explanation

    The newly added "Path ordering" section introduces a detailed explanation of the URL ordering algorithm. It is important to ensure that the explanation is clear, unambiguous, and easy to follow, especially for users unfamiliar with the topic. Verify that the examples provided effectively illustrate the rules and steps described.

    ## Path ordering
    
    Before attempting to [match](#pattern-matching) the incoming request to the various APIs deployed on Tyk to determine which route should be taken, Tyk will first place all the APIs in order. The order is important because the routing logic will compare the incoming request against each in turn until it finds a match (or returns `HTTP 404` if no match is found).
    
    On receipt of an API request Tyk firstly creates a list of all APIs and then iterates through the list for each of the following steps:
    1. APIs that don't have a custom domain defined are moved to the end of the list
    2. Then among each section (custom domain/no custom domain) it will sort by listen path length (longer paths first)
    - note that (dynamic) path parameters are not resolved at this stage so `/api/{category}/user` will rank higher than `/api/123/user`
    3. Then, for each listen path it will sort the endpoints using these rules:
    - remove path parameters (dynamic segments) in the listen path - for example `/{id}` will be replaced with `/`
    - order by the number of segments (count of `/`) from most to least - e.g. `/api/user/profile` before `/api/user`
    - if two endpoints differ only by parameters, the non-parameterized goes first - e.g. `/api/user` before `/api/{userId}`
    - if two endpoints have the same number of segments, the longer path goes first - e.g. `/api/user-access` before `/api/user`
    - for equal length paths, lexicographical order is applied - e.g. `/api/aba` before `/api/abc`
    
    Having created the ordered list, Tyk will attempt to match the request against the paths (patterns) in the list, starting from the top and moving down the list.
    If a match is found, no further checks will be perfomed, so it is important to understand how Tyk orders the patterns to ensure there is no accidental routing to the wrong endpoint.
    

    Copy link
    Contributor

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    @andyo-tyk andyo-tyk changed the title [TT-12871] Add explanation of path ordering before pattern matching [TT-12871, DX-1784] Add explanation of path ordering before pattern matching Dec 20, 2024
    Comment on lines +120 to +121
    Having created the ordered list, Tyk will attempt to match the request against the paths (patterns) in the list, starting from the top and moving down the list.
    If a match is found, no further checks will be perfomed, so it is important to understand how Tyk orders the patterns to ensure there is no accidental routing to the wrong endpoint.
    Copy link
    Contributor

    @titpetric titpetric Dec 20, 2024

    Choose a reason for hiding this comment

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

    Suggested change
    Having created the ordered list, Tyk will attempt to match the request against the paths (patterns) in the list, starting from the top and moving down the list.
    If a match is found, no further checks will be perfomed, so it is important to understand how Tyk orders the patterns to ensure there is no accidental routing to the wrong endpoint.
    Having created the ordered list, Tyk will attempt to match the request against the paths (patterns) in the list, starting from the top and moving down the list. If a match is found, no further checks will be perfomed, so it is important to understand how Tyk orders the patterns to ensure there is no accidental routing to the wrong endpoint.

    Removed unnecessary newline


    Before attempting to [match](#pattern-matching) the incoming request to the various APIs deployed on Tyk to determine which route should be taken, Tyk will first place all the APIs in order. The order is important because the routing logic will compare the incoming request against each in turn until it finds a match (or returns `HTTP 404` if no match is found).

    On receipt of an API request Tyk firstly creates a list of all APIs and then iterates through the list for each of the following steps:
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    I think this statement is incorrect.

    Does Ordering (making a list) happens for every API request, or does it happen when the gateway starts?

    If it happens for every request, isn't that inefficient?

    Comment on lines 110 to 118
    1. APIs that don't have a custom domain defined are moved to the end of the list
    2. Then among each section (custom domain/no custom domain) it will sort by listen path length (longer paths first)
    - note that (dynamic) path parameters are not resolved at this stage so `/api/{category}/user` will rank higher than `/api/123/user`
    3. Then, for each listen path it will sort the endpoints using these rules:
    - remove path parameters (dynamic segments) in the listen path - for example `/{id}` will be replaced with `/`
    - order by the number of segments (count of `/`) from most to least - e.g. `/api/user/profile` before `/api/user`
    - if two endpoints differ only by parameters, the non-parameterized goes first - e.g. `/api/user` before `/api/{userId}`
    - if two endpoints have the same number of segments, the longer path goes first - e.g. `/api/user-access` before `/api/user`
    - for equal length paths, lexicographical order is applied - e.g. `/api/aba` before `/api/abc`
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    Though, this lays out all the conditions. A better experience would be adding an embedded app here. Similar to the storage calculator app.

    I am creating a Ticket for it.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants