Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging to release-5.4.0: [TT-12454] Extract ApplyPolicies into inter…
…nal/policy scope (#6367) [TT-12454] Extract ApplyPolicies into internal/policy scope (#6367) ### **User description** This extracts a large problematic `ApplyPolicies` function into it's own package scope. It does this by: - defining an interface for policy access and deleting inline mutex use - updated *Gateway to implement storage interface - implement storage interface in policy.Store as well (used inline) On top of that: - Fixes a bug in the Duration/Less() functions on user.APILimit, fixes tests - Adds a test particular to ApplyRateLimits (decrease coginitive complexity) The duration was calculated as rate/per, however, the correct way was per/rate; This fixes it so duration is calculated correctly, fixing the Less function comparison. ___ ### **PR Type** Enhancement, Bug fix ___ ### **Description** - Refactored `handleGetPolicy` to use the new `PolicyByID` method. - Introduced a `Repository` interface and added methods `PolicyIDs`, `PolicyByID`, and `PolicyCount` to the `Gateway` struct. - Refactored `ApplyPolicies` in `BaseMiddleware` to use the new `policy` package. - Updated `buildNodeInfo` to use `PolicyCount` instead of `policiesByIDLen`. - Removed redundant methods `getPolicy` and `policiesByIDLen` from `Gateway`. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement </strong></td><td><table> <tr> <td> <details> <summary><strong>api.go</strong><dd><code>Refactor policy retrieval in `handleGetPolicy`.</code> </dd></summary> <hr> gateway/api.go - Replaced `getPolicy` with `PolicyByID` in `handleGetPolicy`. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6367/files#diff-644cda3aeb4ac7f325359e85fcddb810f100dd5e6fa480b0d9f9363a743c4e05">+1/-1</a> </td> </tr> <tr> <td> <details> <summary><strong>gateway.go</strong><dd><code>Add policy-related methods and interface to Gateway.</code> </dd></summary> <hr> gateway/gateway.go <li>Introduced <code>Repository</code> interface.<br> <li> Added methods <code>PolicyIDs</code>, <code>PolicyByID</code>, and <code>PolicyCount</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6367/files#diff-17cb8b37eda9018fe1c6cdb5f96b3fc948fc8ba49bc516987b8269576db9fcd4">+38/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>middleware.go</strong><dd><code>Refactor ApplyPolicies to use new policy store.</code> </dd></summary> <hr> gateway/middleware.go <li>Removed <code>clearSession</code> method.<br> <li> Refactored <code>ApplyPolicies</code> to use <code>policy.New</code> and <code>store.Apply</code>.<br> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6367/files#diff-703054910891a4db633eca0f42ed779d6b4fa75cd9b3aa4c503e681364201c1b">+3/-411</a> </td> </tr> <tr> <td> <details> <summary><strong>rpc_storage_handler.go</strong><dd><code>Update policy count retrieval in buildNodeInfo.</code> </dd></summary> <hr> gateway/rpc_storage_handler.go - Replaced `policiesByIDLen` with `PolicyCount` in `buildNodeInfo`. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6367/files#diff-8875f75b602664c44b62b67a4da41d748124ad270573a44db4ec977ee5d68021">+1/-1</a> </td> </tr> <tr> <td> <details> <summary><strong>server.go</strong><dd><code>Remove redundant policy methods from Gateway.</code> </dd></summary> <hr> gateway/server.go - Removed `getPolicy` and `policiesByIDLen` methods. </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6367/files#diff-4652d1bf175a0be8f5e61ef7177c9666f23e077d8626b73ac9d13358fa8b525b">+0/-13</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 --------- Co-authored-by: Tit Petric <tit@tyk.io>
- Loading branch information