Skip to content

initial ratelimiting implementation - #145

Merged
henrybley merged 1 commit into
mainfrom
tom/#128-initial-rate-limiting
May 27, 2024
Merged

henrybley merged 1 commit into
mainfrom
tom/#128-initial-rate-limiting

Conversation

@tomellm

@tomellm tomellm commented May 22, 2024 •

Copy link
Copy Markdown
Contributor

This is our initial idea on how to implement some kind of rate limiting in the system. Please consider that this is still in progress and in no way done so there definitely are a few dumb mistakes in here.

Overview

The general idea is the following. On every request we can know how our rate-limiting stats. This allows us to adjust requests so that we don't hit the rate-limit. To adjust the number of requests being made we decided to have a kind of status based system. The github-adapter tracks the rate-limit stats and updates this status accordingly. With this status we can then decide for every adapter individually if makes sense to keep fetching new data or to wait it out until the limit improves.

Implementation

To be able to decide when to make a request and when not to we though changing the way scheduling works was needed.

Before

the exporters defined a schedule on which the the Github Api was hit. The caching layer existed because some exporters needed the same Api paths leading to redundand requests.

Now

the exporters have been decoupled from the Github Api. The exporters run periodically very 30s but they practically always hit the caches. These caches are now evicted on the schedule that exporters were running before. Functionally this is the same as before, but compared to before this allows us to decide not to evict the cache if nessesary, which is what we are doing.

Every Adapter implementation then looks at the current Ratelimit status, which is one of the following:

public enum ApiRateLimitStatus {
CRITICAL, WARNING, CONCERNING, GOOD, OK;

and decides whether to cache evict or not.

Changing the status

To look at the headers of every response gotten I decided to use a interceptor on the RestClient. This interceptor looks at the ratio of requests made to time passed and compares it to a "ideal" ratio. This ideal ratio is simply calculated from the ratelimit state. When comparing the "actual" and the "ideal" ratio we can then set the status. The "ideal" ratio times one of these multiplies should be less or equal to the "actual" to result in the following statuses.

ideal * 1.2 >= actual -> CRITICAL
ideal * 0.9 >= actual -> WARNING
ideal * 0.7 >= actual -> CONCERNING
ideal * 0.5 >= actual -> GOOD
else -> OK

*Consider that even tho the status may jump from OK to CRITICAL it can only go down one step at a time.

@tomellm
tomellm marked this pull request as draft May 22, 2024 16:27
@tomellm tomellm self-assigned this May 22, 2024
@tomellm
tomellm force-pushed the tom/#128-initial-rate-limiting branch from 99ddcf1 to 80735d1 Compare May 22, 2024 17:03
@tomellm
tomellm force-pushed the tom/#128-initial-rate-limiting branch from 80735d1 to 2e4fc82 Compare May 23, 2024 14:34
tomellm added a commit that referenced this pull request May 27, 2024
check out the PR #145 for more details
@tomellm
tomellm force-pushed the tom/#128-initial-rate-limiting branch from 2e4fc82 to d211c50 Compare May 27, 2024 07:27
@tomellm
tomellm marked this pull request as ready for review May 27, 2024 07:28
tomellm added a commit that referenced this pull request May 27, 2024
check out the PR #145 for more details
@tomellm
tomellm force-pushed the tom/#128-initial-rate-limiting branch from d211c50 to b0ae0a2 Compare May 27, 2024 07:46

@henrybley henrybley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mostly looking good just the few comments

import java.util.Optional;

public class ApiRateLimitState {
private static final double RATE_LIMIT_BUFFER = 0.9;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be .env configurable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


@Override
public boolean freshDataCanWait() {
return this.rateLimitState.shouldDataWait(ApiRateLimitStatus.GOOD);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think all these should be .env configurable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

ROOT: INFO
githubadapter.config.ratelimiting: TRACE
githubadapter.ratelimiting: TRACE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should this be committed? this was for your testing I assume?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

tomellm added a commit that referenced this pull request May 27, 2024
check out the PR #145 for more details
@tomellm
tomellm force-pushed the tom/#128-initial-rate-limiting branch from b0ae0a2 to 30e9782 Compare May 27, 2024 10:54
check out the PR #145 for more details
@tomellm
tomellm force-pushed the tom/#128-initial-rate-limiting branch from 30e9782 to 16d5690 Compare May 27, 2024 11:04

@henrybley henrybley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks good to me

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
66.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud

@henrybley
henrybley merged commit 899e44e into main May 27, 2024
@henrybley
henrybley deleted the tom/#128-initial-rate-limiting branch May 27, 2024 11:07
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.

3 participants