Closed buger closed 1 month ago
API Changes
--- prev.txt 2024-06-26 09:19:53.557602332 +0000
+++ current.txt 2024-06-26 09:19:50.317566233 +0000
@@ -8234,6 +8234,12 @@
func (gw *Gateway) NotifyCurrentServerStatus()
+func (gw *Gateway) PolicyByID(polID string) (user.Policy, bool)
+
+func (gw *Gateway) PolicyCount() int
+
+func (gw *Gateway) PolicyIDs() []string
+
func (gw *Gateway) ProcessOauthClientsOps(clients map[string]string)
ProcessOauthClientsOps performs the appropriate action for the received
clients it can be any of the Create,Update and Delete operations
@@ -9522,6 +9528,10 @@
TickOk triggers a reload and ensures a queue happened and a reload cycle
happens. This will block until all the cases are met.
+type Repository interface {
+ policy.Repository
+}
+
type RequestDefinition struct {
Method string `json:"method"`
Headers map[string]string `json:"headers"`
@@ -12032,10 +12042,6 @@
func (limit APILimit) IsEmpty() bool
-func (g *APILimit) Less(in APILimit) bool
- Less will return true if the receiver has a smaller duration between
- requests than `in`.
-
type AccessDefinition struct {
APIName string `json:"api_name" msg:"api_name"`
APIID string `json:"api_id" msg:"api_id"`
β±οΈ Estimated effort to review [1-5] | 4 |
π§ͺ Relevant tests | Yes |
π Security concerns | No |
β‘ Key issues to review |
Possible Bug: The refactoring of the ApplyPolicies function into the new policy package should be carefully reviewed to ensure that all previous functionalities are preserved and correctly implemented. The changes are extensive and involve multiple components, which increases the risk of introducing bugs or regressions. |
Performance Concern: The new implementation introduces additional layers of abstraction and interface calls (e.g., Repository and Service in the policy package). It's important to assess the impact on performance, especially in high-load environments. | |
Code Complexity: The new policy package introduces a significant amount of new code and logic. Reviewers should ensure that the code is maintainable and well-documented, particularly the parts handling policy application logic and session modifications. |
Category | Suggestion | Score |
Enhancement |
Improve error message specificity for better debugging___ **Consider using a more specific error message infmt.Errorf("policy not found: %s", polID) by including additional context about the error, such as the function or scenario in which it occurred.** [internal/policy/apply.go [44]](https://github.com/TykTechnologies/tyk/pull/6371/files#diff-59b92e9d31f142f1d99b746eb3ff7db4e26bf6c3044c9b87b58034a947ee04d1R44-R44) ```diff -return fmt.Errorf("policy not found: %s", polID) +return fmt.Errorf("in ClearSession, policy not found: %s", polID) ``` Suggestion importance[1-10]: 9Why: Providing more specific error messages greatly aids in debugging and understanding the context of the error, which is crucial for maintaining and troubleshooting the code. | 9 |
Simplify the handling of
___
**Instead of using a pointer for | 7 | |
Error handling |
Enhance error handling by checking the return value of the
___
**To improve error handling, consider adding a check for the success of the | 9 |
Possible bug |
Add error handling for nil
___
**Consider handling the case where | 8 |
Maintainability |
Maintain method naming consistency and usage pattern___ **Replace the call tor.Gw.PolicyCount() with r.Gw.policiesByIDLen() to maintain consistency with the existing method naming and usage pattern in the codebase.** [gateway/rpc_storage_handler.go [174]](https://github.com/TykTechnologies/tyk/pull/6371/files#diff-8875f75b602664c44b62b67a4da41d748124ad270573a44db4ec977ee5d68021R174-R174) ```diff -PoliciesCount: r.Gw.PolicyCount(), +PoliciesCount: r.Gw.policiesByIDLen(), ``` Suggestion importance[1-10]: 8Why: The suggestion ensures consistency in method naming and usage patterns, which is important for maintainability and readability of the codebase. | 8 |
Ensure method naming consistency across the codebase___ **Replace the call tots.Gw.PolicyCount() with ts.Gw.policiesByIDLen() to ensure consistency with the existing method naming and usage pattern in the codebase.** [gateway/server_test.go [156]](https://github.com/TykTechnologies/tyk/pull/6371/files#diff-d9f006370c9748c09affd99d0a4edeb8f3419057703a67fd70838a764a485696R156-R156) ```diff -actual := ts.Gw.PolicyCount() +actual := ts.Gw.policiesByIDLen() ``` Suggestion importance[1-10]: 8Why: This suggestion promotes consistency in method naming, which helps in maintaining a uniform codebase and reduces potential confusion. | 8 | |
Refactor test setup into a separate helper function___ **It's a good practice to separate the creation of test data from the test logic. Considerrefactoring the test setup (creation of session , apiLimits , and policy ) into a separate helper function to improve test readability and reusability.** [internal/policy/apply_test.go [16-24]](https://github.com/TykTechnologies/tyk/pull/6371/files#diff-5af7e299a6b0ce11e22f8aa4a01854b1151f4b54dccc68f0cd1cbedee5aed7c8R16-R24) ```diff -session := &user.SessionState{ - Rate: 5, - Per: 10, -} -apiLimits := user.APILimit{ - Rate: 10, - Per: 10, -} -policy := user.Policy{} +session, apiLimits, policy := setupTestData(5, 10, 10, 10) ``` Suggestion importance[1-10]: 6Why: Refactoring the test setup into a helper function improves code readability and reusability, making the tests easier to maintain and understand. | 6 | |
Best practice |
Add error handling check for
___
**Consider adding a check for the return value of | 8 |
Possible issue |
Maintain original functionality unless change is justified___ **Replace the call toString(RateLimitSmoothingUp) with String(RateLimitExceeded) to maintain the original functionality unless there is a specific reason for the change.** [internal/event/event_test.go [17]](https://github.com/TykTechnologies/tyk/pull/6371/files#diff-441a34fa81121d95f8fad2cbaddca97199411a8fad7b3e2f3df5be69acf1a94aR17-R17) ```diff -s := String(RateLimitSmoothingUp) +s := String(RateLimitExceeded) ``` Suggestion importance[1-10]: 7Why: The suggestion aims to maintain the original functionality, which is generally a good practice unless there is a specific reason for the change. However, the impact is relatively minor. | 7 |
Verify that the
___
**To ensure that the | 7 | |
Performance |
Optimize
___
**To enhance the performance of | 5 |
Failed conditions
30.0% Coverage on New Code (required β₯ 80%)
C Reliability Rating on New Code (required β₯ A)
See analysis details on SonarCloud
Catch issues before they fail your Quality Gate with our IDE extension SonarLint
User description
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:On top of that:
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
handleGetPolicy
to use the newPolicyByID
method.Repository
interface and added methodsPolicyIDs
,PolicyByID
, andPolicyCount
to theGateway
struct.ApplyPolicies
inBaseMiddleware
to use the newpolicy
package.buildNodeInfo
to usePolicyCount
instead ofpoliciesByIDLen
.getPolicy
andpoliciesByIDLen
fromGateway
.Changes walkthrough π
api.go
Refactor policy retrieval in `handleGetPolicy`.
gateway/api.go - Replaced `getPolicy` with `PolicyByID` in `handleGetPolicy`.
gateway.go
Add policy-related methods and interface to Gateway.
gateway/gateway.go
Repository
interface.PolicyIDs
,PolicyByID
, andPolicyCount
.middleware.go
Refactor ApplyPolicies to use new policy store.
gateway/middleware.go
clearSession
method.ApplyPolicies
to usepolicy.New
andstore.Apply
.rpc_storage_handler.go
Update policy count retrieval in buildNodeInfo.
gateway/rpc_storage_handler.go - Replaced `policiesByIDLen` with `PolicyCount` in `buildNodeInfo`.
server.go
Remove redundant policy methods from Gateway.
gateway/server.go - Removed `getPolicy` and `policiesByIDLen` methods.
Co-authored-by: Tit Petric tit@tyk.io
PR Type
Enhancement, Bug fix, Tests
Description
handleGetPolicy
to use the newPolicyByID
method.Repository
interface and added methodsPolicyIDs
,PolicyByID
, andPolicyCount
to theGateway
struct.ApplyPolicies
inBaseMiddleware
to use the newpolicy
package.buildNodeInfo
to usePolicyCount
instead ofpoliciesByIDLen
.getPolicy
andpoliciesByIDLen
fromGateway
.Duration
method inAPILimit
and removed theLess
method.policy
package and updated existing tests.Changes walkthrough π
10 files
api.go
Refactor `handleGetPolicy` to use `PolicyByID`
gateway/api.go - Refactored `handleGetPolicy` to use `PolicyByID` method.
gateway.go
Introduce `Repository` interface and methods in `Gateway`
gateway/gateway.go
Repository
interface.PolicyIDs
,PolicyByID
, andPolicyCount
methods toGateway
.middleware.go
Refactor `ApplyPolicies` to use new `policy` package
gateway/middleware.go
ApplyPolicies
to use the newpolicy
package.clearSession
and inlined its logic into the new package.mw_rate_limiting.go
Update error message in `handleRateLimitFailure`
gateway/mw_rate_limiting.go - Updated error message in `handleRateLimitFailure`.
rpc_storage_handler.go
Refactor `buildNodeInfo` to use `PolicyCount`
gateway/rpc_storage_handler.go - Refactored `buildNodeInfo` to use `PolicyCount` method.
server.go
Remove redundant methods from `Gateway`
gateway/server.go - Removed `getPolicy` and `policiesByIDLen` methods.
event.go
Remove `RateLimitExceeded` event from `eventMap`
internal/event/event.go - Removed `RateLimitExceeded` event from `eventMap`.
apply.go
Introduce `Service` struct and refactor policy application
internal/policy/apply.go
Service
struct for policy application.ApplyPolicies
andClearSession
logic toService
.ApplyRateLimits
method.store.go
Introduce `Store` struct for in-memory policy storage
internal/policy/store.go - Introduced `Store` struct for in-memory policy storage.
util.go
Add utility functions for policy application
internal/policy/util.go - Added utility functions for policy application.
5 files
policy_test.go
Update tests to use `PolicyByID` method
gateway/policy_test.go
PolicyByID
method.PolicyByID
.server_test.go
Update tests to use `PolicyCount` method
gateway/server_test.go - Updated tests to use `PolicyCount` method.
event_test.go
Update test to use `RateLimitSmoothingUp` event
internal/event/event_test.go - Updated test to use `RateLimitSmoothingUp` event.
apply_test.go
Add tests for `ApplyRateLimits` and `ApplyPolicies`
internal/policy/apply_test.go - Added tests for `ApplyRateLimits` and `ApplyPolicies`.
session_test.go
Update tests for `Duration` method and remove `Less` method tests
user/session_test.go
Duration
method.Less
method.1 files
session.go
Fix `Duration` method and remove `Less` method in `APILimit`
user/session.go
Duration
method inAPILimit
.Less
method fromAPILimit
.1 files
Taskfile.yml
Add Taskfile for running tests and coverage
internal/policy/Taskfile.yml - Added Taskfile for running tests and coverage.