Skip to content

Conversation

hemanth5055
Copy link
Contributor

Description:
This PR introduces IP-based rate limiting to the Cloudflare Worker handling /v1/translate and /v1/explain routes. The main goal is to protect the Gemini API from abuse and prevent excessive requests that could exhaust our quota.

Changes Made:

  • Implemented checkRateLimit function using Cloudflare KV to track request counts per IP.
  • Set a limit of 10 requests per minute per IP, returning a 429 Too Many Requests error if exceeded.
  • Updated fetch handler to integrate the rate limiting check before processing requests.
  • Added JSON parse safety and TTL management for KV storage.
  • Retained existing functionality for /v1/translate and /v1/explain routes.
  • Added a new test route /test-rate-limit to verify rate limiting logic

Benefits:

  • Prevents API abuse and uncontrolled usage.
  • Protects the Gemini API quota and reduces potential costs.
  • Improves overall security and stability of the service.

Screenshots:

Screenshot 2025-09-05 at 10 44 58 PM

@dineshsutihar
Copy link
Collaborator

It looks good from a quick glance. Let me pull it down to my system and verify everything, and then we should be good to merge.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds IP-based rate limiting to protect the Cloudflare Worker's /v1/translate and /v1/explain routes from abuse. The implementation sets a limit of 10 requests per minute per IP address using Cloudflare KV storage.

  • Introduces a checkRateLimit function that tracks request counts per IP using KV storage
  • Integrates rate limiting into the main request handler with 429 responses for exceeded limits
  • Adds KV namespace configuration and a test endpoint for verification

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
backend/wrangler.jsonc Adds KV namespace binding for rate limiting storage
backend/src/index.ts Implements rate limiting logic and integrates it into the request handler

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Collaborator

@dineshsutihar dineshsutihar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Good Work @hemanth5055

@dineshsutihar dineshsutihar merged commit fabe5ca into StructZ:develop Sep 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants