-
Notifications
You must be signed in to change notification settings - Fork 8
Added rate limit to health check API #150
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
base: develop
Are you sure you want to change the base?
Conversation
Summary by CodeRabbit
WalkthroughThe changes introduce new dependencies to the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant LambdaHandler
participant Utils
participant DynamoDB
Client->>LambdaHandler: Invoke health check
LambdaHandler->>DynamoDB: Create DynamoDB client
LambdaHandler->>Utils: CheckRequestAllowed(DynamoDB client, concurrency constant)
Utils-->>LambdaHandler: Validation result
LambdaHandler-->>Client: Return health check response
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
⛔ Files ignored due to path filters (1)
health-check/go.sum
is excluded by!**/*.sum
📒 Files selected for processing (2)
health-check/go.mod
(1 hunks)health-check/main.go
(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
health-check/main.go (3)
utils/CheckRequestAllowed.go (1)
CheckRequestAllowed
(27-97)database/dynamodb.go (1)
CreateDynamoDB
(25-67)utils/Constants.go (1)
ConcurrencyDisablingLambda
(22-22)
🔇 Additional comments (4)
health-check/go.mod (3)
12-15
: New indirect dependencies added to support rate limitingThe added dependencies (AWS SDK, jmespath, godotenv) support the DynamoDB operations required for the rate limiting functionality. This is aligned with the PR objectives.
6-6
:✅ Verification successful
Verify the specific commit hash of feature-flag-backend dependency
The PR adds a direct dependency on
github.com/Real-Dev-Squad/feature-flag-backend
with a very specific commit hash. Ensure this is the intended version and not a temporary development reference.
🏁 Script executed:
#!/bin/bash # Check if this commit hash exists in the repository gh repo view Real-Dev-Squad/feature-flag-backend echo "Checking validity of commit hash: a49b2080c46c" gh api repos/Real-Dev-Squad/feature-flag-backend/commits/a49b2080c46c --silent && echo "Commit exists" || echo "Invalid commit reference"Length of output: 7964
Dependency commit hash verified
Verified that commit
a49b2080c46c
exists in the Real-Dev-Squad/feature-flag-backend repository. The reference inhealth-check/go.mod
(line 6) is valid and requires no changes.
8-8
:✅ Verification successful
Testify version upgrade noted
The testify package has been upgraded from v1.7.2 to v1.8.2. Are there any test files that have been updated to use new features from this version?
🏁 Script executed:
#!/bin/bash # Look for test files that might be using new testify features find health-check -name "*_test.go" | xargs cat | grep -A 5 "github.com/stretchr/testify"Length of output: 185
No new Testify features detected in tests
A search through all
*_test.go
files underhealth-check
shows only the standardassert
import fromgithub.com/stretchr/testify/assert
and no usage of any newly introduced functions or APIs in v1.8.2. The upgrade appears safe and no test updates are required.health-check/main.go (1)
6-8
: New feature-flag-backend imports introducedThese imports enable the health check handler to use rate limiting functionality from the feature-flag-backend module. This is consistent with the PR objectives.
db := database.CreateDynamoDB() | ||
|
||
checkRequestAllowed(db, utils.ConcurrencyDisablingLambda) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Missing documentation for rate limiting behavior
The implementation adds significant rate limiting behavior to the health check endpoint, but there's no documentation on how this works, what limits are applied, or how to configure them. Consider adding comments or documentation.
func handler(request events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse, error){
+ // Create DynamoDB client for rate limiting operations
db := database.CreateDynamoDB()
+ // Apply rate limiting to health check endpoint
+ // This uses a counter in DynamoDB to track and limit requests
+ // When the limit is reached, concurrency is disabled for other lambdas
checkRequestAllowed(db, utils.ConcurrencyDisablingLambda)
🛠️ Refactor suggestion
Add error handling for database operations and rate limiting
The calls to CreateDynamoDB()
and checkRequestAllowed()
don't include any error handling, which could lead to unexpected behavior if these operations fail. Based on the provided snippets, it appears that CheckRequestAllowed
handles errors internally but doesn't return them to the caller.
func handler(request events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse, error){
- db := database.CreateDynamoDB()
+ // Wrap the database creation in a defer-recover block to handle panics
+ var db *dynamodb.DynamoDB
+ func() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Printf("Recovered from database creation: %v", r)
+ }
+ }()
+ db = database.CreateDynamoDB()
+ }()
+
+ if db == nil {
+ return events.APIGatewayProxyResponse{
+ Body: "Internal server error - database client initialization failed",
+ StatusCode: 500,
+ }, nil
+ }
checkRequestAllowed(db, utils.ConcurrencyDisablingLambda)
💡 Verification agent
🧩 Analysis chain
Consider performance impact of DynamoDB operations on health check endpoint
Adding DynamoDB operations to a health check endpoint may increase latency and reduce reliability. Health checks are typically lightweight and shouldn't depend on external services. Consider if this is the appropriate place for rate limiting, or if it should be implemented at an API Gateway or proxy layer instead.
🏁 Script executed:
#!/bin/bash
# Check if there are any tests that validate the performance of the health check
find . -name "*_test.go" | xargs grep -l "health" | xargs grep -l "performance\|latency\|benchmark"
Length of output: 99
🏁 Script executed:
#!/bin/bash
# Search for Go benchmark tests related to the health‐check endpoint
# This looks for any Benchmark functions that include "health" (case-insensitive)
rg -n 'func Benchmark.*health' -i -t go || true
Length of output: 48
🏁 Script executed:
#!/bin/bash
# Locate invocation of checkRequestAllowed with context
rg -n 'checkRequestAllowed' -C5 -t go || true
# Locate the definition of checkRequestAllowed
rg -n 'func checkRequestAllowed' -C5 -t go || true
# Locate the CreateDynamoDB factory method
rg -n 'func CreateDynamoDB' -C5 -t go || true
Length of output: 1422
Avoid external calls in health‐check handler
The health‐check lambda in health-check/main.go currently initializes a DynamoDB client and invokes the rate‐limiting check on every probe, which can increase latency and introduce a dependency on an external service:
• File: health-check/main.go, lines 13–15
db := database.CreateDynamoDB()
checkRequestAllowed(db, utils.ConcurrencyDisablingLambda)
Health checks should remain lightweight and reliable. Please consider removing the DynamoDB call from this endpoint and moving concurrency/rate‐limit enforcement to API Gateway, a proxy layer, or a dedicated middleware.
func handler(request events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse, error){ | ||
db := database.CreateDynamoDB() | ||
|
||
checkRequestAllowed(db, utils.ConcurrencyDisablingLambda) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify rate limiting behavior with tests
The rate limiting implementation is a significant change to the health check functionality. However, there don't appear to be any tests validating this behavior. Consider adding unit or integration tests.
🏁 Script executed:
#!/bin/bash
# Look for test files related to health check and rate limiting
find . -name "*_test.go" | xargs grep -l "health\|rate\|limit"
Length of output: 92
🏁 Script executed:
#!/bin/bash
# Check for rate limiting tests in health-check/main_test.go
grep -R -n "checkRequestAllowed\|ConcurrencyDisablingLambda\|RateLimit\|limit" health-check/main_test.go
Length of output: 106
Add tests for health‑check rate‑limiting behavior
The new checkRequestAllowed
logic for rate limiting isn’t covered by any existing tests. Please add unit (or integration) tests to validate both allowed and disallowed scenarios:
- File to update:
health-check/main_test.go
- Cover calls to
checkRequestAllowed(db, utils.ConcurrencyDisablingLambda)
- Include cases for:
- Requests under the limit (should succeed)
- Requests exceeding the limit (should be blocked)
- Optionally, mock the database or rate‑limiter state to simulate edge conditions
|
||
) | ||
|
||
var checkRequestAllowed = utils.CheckRequestAllowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Consider function initialization in handler to improve testability
The global variable checkRequestAllowed
makes testing more difficult. Consider initializing this within the handler function or using dependency injection to improve testability.
-var checkRequestAllowed = utils.CheckRequestAllowed
func handler(request events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse, error){
db := database.CreateDynamoDB()
-
- checkRequestAllowed(db, utils.ConcurrencyDisablingLambda)
+ utils.CheckRequestAllowed(db, utils.ConcurrencyDisablingLambda)
Committable suggestion skipped: line range outside the PR's diff.
add health check to the rate limiting logic to shutdown this |
Date: 23rd April
Developer Name: Vikhyat
Issue Ticket Number
Description
Documentation Updated?
Under Feature Flag
Database Changes
Breaking Changes
Development Tested?
Screenshots
Screenshot 1
Test Coverage
Test cases were already written for this
Additional Notes