Add API rate limiting for trip planner requests #294
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 stateprivate 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?
RateLimiter
interface andAPIRateLimiter
implementationTimeTableViewModel
to use rate limiting for trip fetchingHow to test?
APIRateLimiterTest
to verify rate limiting behaviorWhy 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