Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TT-12566/TT-12851] Add client endpoint rate limiter (#6462)
### **User description** <!-- Provide a general summary of your changes in the Title above --> ## Description This PR adds endpoint level rate limiting to keys. Merging this in policies would be added in a follow up PR. Counter usage and increments would be as follows. - Endpoint rate limits for `/API1/Endpoint1`, `/API1/Endpoint2` and `/API2/Endpoint1` (using `API1Endpoint1_Counter`, `API1Endpoint2_Counter`, `API2Endpoint1_Counter`), - API rate limits for `/API1` and `/API3` (using `API1_Counter`, `API3_Counter`), no per API limits specified for `API2` - Global rate limit for all other requests (using Global_Counter) | Client request | Global_Counter | API1_Counter | API3_Counter | API1Endpoint1_Counter | API1Endpoint2_Counter | API2Endpoint1_Counter | |-----------------|----------------|--------------|--------------|-----------------------|-----------------------|-----------------------| | /API1/Endpoint1 | | | | ++ | | | | /API1/Endpoint2 | | | | | ++ | | | /API1/Endpoint3 | | ++ | | | | | | /API2/Endpoint1 | | | | | | ++ | | /API2/Endpoint2 | ++ | | | | | | | /API2/Endpoint3 | ++ | | | | | | | /API3/Endpoint1 | | | ++ | | | | | /API3/Endpoint2 | | | ++ | | | | | /API3/Endpoint3 | | | ++ | | | | ## Related Issue parent: https://tyktech.atlassian.net/browse/TT-12566 subtask: https://tyktech.atlassian.net/browse/TT-12851 ## Motivation and Context <!-- Why is this change required? What problem does it solve? --> ## How This Has Been Tested <!-- Please describe in detail how you tested your changes --> <!-- Include details of your testing environment, and the tests --> <!-- you ran to see how your change affects other areas of the code, etc. --> <!-- This information is helpful for reviewers and QA. --> ## Screenshots (if appropriate) ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] 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 <!-- Go over all the following points, and put an `x` in all the boxes that apply --> <!-- If there are no documentation updates required, mark the item as checked. --> <!-- Raise up any additional concerns not covered by the 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, Tests ___ ### **Description** - Implemented endpoint-specific rate limiting logic in `session_manager.go`. - Added new test cases and refactored existing ones to support endpoint rate limiting. - Introduced new structures in `session.go` to handle endpoint-specific rate limits. - Enhanced test framework to support setup functions in test cases. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>mw_rate_limiting_test.go</strong><dd><code>Add and refactor tests for endpoint rate limiting</code> </dd></summary> <hr> gateway/mw_rate_limiting_test.go <li>Added new test cases for endpoint rate limiting.<br> <li> Refactored existing rate limit test cases into reusable structures.<br> <li> Introduced <code>endpointRateLimitTestHelper</code> function for testing endpoint <br>rate limits.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6462/files#diff-7cf2199231924147d538ba7ad576a48a3c0e691852077e147c9b2d86ba9b7c4d">+196/-51</a></td> </tr> <tr> <td> <details> <summary><strong>session_manager_test.go</strong><dd><code>Add tests for endpoint rate limit info retrieval</code> </dd></summary> <hr> gateway/session_manager_test.go <li>Added tests for <code>getEndpointRateLimitInfo</code> function.<br> <li> Verified correct rate limit info retrieval based on endpoint and <br>method.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6462/files#diff-9674b884106ef21bfbe19418c5f640e530cac4fc0fbe85edc04ffe3190468874">+105/-0</a> </td> </tr> </table></td></tr><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>session_manager.go</strong><dd><code>Implement endpoint-specific rate limiting logic</code> </dd></summary> <hr> gateway/session_manager.go <li>Implemented endpoint-specific rate limiting logic.<br> <li> Added support for endpoint rate limit key suffixes.<br> <li> Integrated endpoint rate limit checks into existing rate limiting <br>flow.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6462/files#diff-e6b40a285464cd86736e970c4c0b320b44c75b18b363d38c200e9a9d36cdabb6">+62/-9</a> </td> </tr> <tr> <td> <details> <summary><strong>http.go</strong><dd><code>Add support for setup functions in test cases</code> </dd></summary> <hr> test/http.go <li>Added support for <code>BeforeFn</code> in test cases to execute setup functions <br>before tests.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6462/files#diff-a5530e34c740ce6fe2efe8dda5a356463c450696b39b97b91228f1be2491e05e">+4/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>session.go</strong><dd><code>Add endpoint-specific rate limit structures and clone method</code></dd></summary> <hr> user/session.go <li>Added <code>Clone</code> method to <code>APILimit</code> for deep copying.<br> <li> Introduced <code>Endpoint</code>, <code>EndpointMethod</code>, and <code>EndpointMethodRateLimit</code> <br>structs for endpoint-specific rate limits.<br> <li> Updated <code>AccessDefinition</code> to include <code>Endpoints</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6462/files#diff-95f1b81f7fbfef1d0372a02f3c2d557d32a3dd42e1a7d1626fdd209aaf7537f4">+50/-5</a> </td> </tr> </table></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions
- Loading branch information