Closed titpetric closed 1 month ago
API Changes
--- prev.txt 2024-07-15 14:14:30.365866423 +0000
+++ current.txt 2024-07-15 14:14:27.101873520 +0000
@@ -3853,6 +3853,9 @@
// RequestSizeLimit limits the maximum allowed size of the request body in bytes.
RequestSizeLimit *RequestSizeLimit `bson:"requestSizeLimit,omitempty" json:"requestSizeLimit,omitempty"`
+
+ // RateLimit contains endpoint level rate limit configuration.
+ RateLimit *RateLimitEndpoint `bson:"rateLimit,omitempty" json:"rateLimit,omitempty"`
}
Operation holds a request operation configuration, allowances,
tranformations, caching, timeouts and validation.
@@ -4086,7 +4089,7 @@
// be considered as 0s/empty.
//
// Tyk classic API definition: `global_rate_limit.per`.
- Per time.ReadableDuration `json:"per" bson:"per"`
+ Per ReadableDuration `json:"per" bson:"per"`
}
RateLimit holds the configurations related to rate limit. The API-level rate
limit applies a base-line limit on the frequency of requests to the upstream
@@ -4100,6 +4103,15 @@
func (r *RateLimit) Fill(api apidef.APIDefinition)
Fill fills *RateLimit from apidef.APIDefinition.
+type RateLimitEndpoint RateLimit
+ RateLimitEndpoint carries same settings as RateLimit but for endpoints.
+
+func (r *RateLimitEndpoint) ExtractTo(meta *apidef.RateLimitMeta)
+ ExtractTo extracts *Ratelimit into *apidef.RateLimitMeta.
+
+func (r *RateLimitEndpoint) Fill(api apidef.RateLimitMeta)
+ Fill fills *RateLimit from apidef.RateLimitMeta.
+
type ReadableDuration = time.ReadableDuration
ReadableDuration is an alias maintained to be used in imports.
@@ -4773,7 +4785,7 @@
// An empty value is interpreted as "0s", implying no cool-down.
// It's important to format the string correctly, as invalid formats will
// be considered as 0s/empty.
- CoolDownPeriod time.ReadableDuration `json:"cooldownPeriod" bson:"cooldownPeriod"`
+ CoolDownPeriod ReadableDuration `json:"cooldownPeriod" bson:"cooldownPeriod"`
// BodyTemplate is the template to be used for request payload.
BodyTemplate string `json:"bodyTemplate,omitempty" bson:"bodyTemplate,omitempty"`
// Headers are the list of request headers to be used.
โฑ๏ธ Estimated effort to review: 3 ๐ต๐ต๐ตโชโช |
๐งช PR contains tests |
๐ No security concerns identified |
โก Key issues to review Rate Limit Logic Ensure that the logic for setting the rate limit per minute is correctly implemented and tested, especially the use of `time.ReadableDuration(time.Minute)`. Data Conversion Verify the correctness of data conversion in `RateLimitEndpoint.Fill` and `RateLimitEndpoint.ExtractTo`, particularly the transformation of rate and period values. |
Category | Suggestion | Score |
Possible bug |
Add error handling for nil returns from
___
**Add error handling for | 10 |
Possible issue |
Avoid overwriting existing rate limit duration if already set___ **Consider checking ifop.RateLimit.Per is already set before assigning a new value to avoid overwriting existing configurations unintentionally.** [apidef/oas/linter_test.go [37]](https://github.com/TykTechnologies/tyk/pull/6416/files#diff-b92239afd81e77a829fe7fe8410044dfd4dfda525d17dbf5f8811714a9c986d3R37-R37) ```diff -if op.RateLimit != nil { +if op.RateLimit != nil && op.RateLimit.Per == 0 { op.RateLimit.Per = time.ReadableDuration(time.Minute) } ``` Suggestion importance[1-10]: 9Why: This suggestion prevents unintentional overwriting of existing configurations, which is crucial for maintaining the integrity of the rate limit settings. | 9 |
Maintainability |
Use a constant for the rate limit duration to improve readability___ **Consider using a constant fortime.Minute to avoid magic numbers and improve code readability and maintainability.** [apidef/oas/operation_test.go [57]](https://github.com/TykTechnologies/tyk/pull/6416/files#diff-cd234db716d6d2edc97c135ef546021c9ab4fa9282d63964bd155d41635cf964R57-R57) ```diff -operation.RateLimit.Per = time.ReadableDuration(time.Minute) +const defaultRateLimitDuration = time.Minute +operation.RateLimit.Per = time.ReadableDuration(defaultRateLimitDuration) ``` Suggestion importance[1-10]: 6Why: Using a constant improves code readability and maintainability, but it is a minor improvement and not crucial for functionality. | 6 |
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
95.7% Coverage on New Code
0.0% Duplication on New Code
User description
https://tyktech.atlassian.net/browse/TT-12634
PR Type
Enhancement, Tests
Description
RateLimit
field to theOperation
struct inoperation.go
.fillRateLimitEndpoints
andextractRateLimitEndpointTo
.fillPathsAndOperations
andextractPathsAndOperations
methods.RateLimitEndpoint
struct and implementedFill
andExtractTo
methods for it inupstream.go
.X-Tyk-Operation
schema definition to includerateLimit
field.Changes walkthrough ๐
linter_test.go
Add rate limit configuration handling in linter test
apidef/oas/linter_test.go - Added rate limit configuration handling in the `Lint` test.
operation_test.go
Add rate limit configuration handling in operation test
apidef/oas/operation_test.go
PathsAndOperations
test.operation.go
Add rate limit handling in OAS operations
apidef/oas/operation.go
RateLimit
field toOperation
struct.fillRateLimitEndpoints
andextractRateLimitEndpointTo
methods.
fillPathsAndOperations
andextractPathsAndOperations
.upstream.go
Add RateLimitEndpoint struct and methods
apidef/oas/upstream.go
RateLimitEndpoint
struct.Fill
andExtractTo
methods forRateLimitEndpoint
.x-tyk-api-gateway.json
Add rateLimit field to X-Tyk-Operation schema
apidef/oas/schema/x-tyk-api-gateway.json - Added `rateLimit` field to `X-Tyk-Operation` schema definition.