Skip to content
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

Add API rate limiting for trip planner requests #294

Merged
merged 1 commit into from
Nov 3, 2024

Conversation

ksharma-xyz
Copy link
Owner

@ksharma-xyz ksharma-xyz commented Nov 3, 2024

TL;DR

Added rate limiting functionality to the Trip Planner API calls to prevent excessive requests. For first time the api will be called without delay and subsequent ones will be rate limited by 1 second.

NOTE: Also making the APIRateLimiter as NON Singleton as we want to lose the state private val isFirstTime = MutableStateFlow(false). Otherwise , if we go to another screen and come back to TimeTableScreen this class will still be alive and rate limit first time API calls also.

What changed?

  • Introduced a new RateLimiter interface and APIRateLimiter implementation
  • Added rate limiting with a 1-second debounce interval between API calls
  • Updated TimeTableViewModel to use rate limiting for trip fetching
  • Implemented unit tests for rate limiting behavior
  • Removed unused demo UI test file

How to test?

  1. Navigate to the Trip Planner screen
  2. Rapidly toggle between different trips or reverse trips
  3. Verify that API calls are limited to once per second
  4. Run the new unit tests in APIRateLimiterTest to verify rate limiting behavior

Why make this change?

To prevent potential API rate limit issues and optimize network usage by controlling the frequency of API calls, especially during rapid user interactions or screen state changes. This implementation ensures smoother user experience while maintaining server-friendly request patterns.

Screenshots

Screen.Recording.2024-11-03.at.9.37.09.pm.mov

Copy link
Owner Author

ksharma-xyz commented Nov 3, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @ksharma-xyz and the rest of your teammates on Graphite Graphite

@ksharma-xyz ksharma-xyz changed the title Add RatLimit api Add API rate limiting for trip planner requests Nov 3, 2024
@ksharma-xyz ksharma-xyz added the achievement When a big problem is fixed or something exciting gets working label Nov 3, 2024 — with Graphite App
@ksharma-xyz ksharma-xyz marked this pull request as ready for review November 3, 2024 05:44
@ksharma-xyz ksharma-xyz self-assigned this Nov 3, 2024
@ksharma-xyz ksharma-xyz force-pushed the 11-03-add_ratlimit_api branch 2 times, most recently from 9f491bc to 5cfc637 Compare November 3, 2024 10:27
Base automatically changed from 11-02-update_titlebar_to_lay_out_actions_by_itself_with_spacing to main November 3, 2024 11:04
@ksharma-xyz ksharma-xyz enabled auto-merge (squash) November 3, 2024 11:04
@ksharma-xyz ksharma-xyz force-pushed the 11-03-add_ratlimit_api branch from 5cfc637 to fc286f3 Compare November 3, 2024 11:04
@ksharma-xyz ksharma-xyz merged commit c0e1520 into main Nov 3, 2024
3 checks passed
@ksharma-xyz ksharma-xyz deleted the 11-03-add_ratlimit_api branch November 3, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
achievement When a big problem is fixed or something exciting gets working
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant