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

ARC-1231 fix preemptive rate limit negative number problem #1934

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/util/preemptive-rate-limit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import { numberFlag, NumberFlags } from "config/feature-flags";
jest.mock("config/feature-flags");

const TEST_INSTALLATION_ID = 1234;
const REALLY_SMALL_RESET_TIME = 1000;
const THIRTY_MINUTES_IN_SECONDS = 30 * 60;

const mockGitHubRateLimit = (limit , remaining) => {
const mockGitHubRateLimit = (limit , remaining, resetTime?) => {
githubUserTokenNock(TEST_INSTALLATION_ID);
githubNock.get(`/rate_limit`)
.reply(200, {
"resources": {
"core": {
"limit": limit,
"remaining": remaining,
"reset": 1372700873
"reset": resetTime || 1372700873
},
"graphql": {
"limit": 5000,
"remaining": 5000,
"reset": 1372700389
"reset": resetTime || 1372700389
}
}
});
Expand Down Expand Up @@ -59,6 +61,42 @@ describe("Preemptive rate limit check - Cloud", () => {
expect(await preemptiveRateLimitCheck(message, sqsQueue)).toBe(true);
});

it(`Should use default time delay if rest time is negative`, async () => {

when(numberFlag).calledWith(
NumberFlags.PREEMPTIVE_RATE_LIMIT_THRESHOLD,
expect.anything(),
expect.anything()
).mockResolvedValue(50);

mockGitHubRateLimit(100, 30, REALLY_SMALL_RESET_TIME);

const changeVisibilityTimeoutMock = jest.fn();
const message = {
payload: {
jiraHost: "JIRAHOST_MOCK",
installationId: TEST_INSTALLATION_ID,
gitHubAppConfig: {
gitHubAppId: 1
}
},
message: {},
log: {
info: jest.fn(),
warn: jest.fn()
}
};
const sqsQueue = {
queueName: "backfill",
changeVisibilityTimeout: changeVisibilityTimeoutMock
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(await preemptiveRateLimitCheck(message, sqsQueue)).toBe(true);

expect(changeVisibilityTimeoutMock).toHaveBeenLastCalledWith(expect.anything(), THIRTY_MINUTES_IN_SECONDS, expect.anything());
});

it(`Should return false since threshold is not met`, async () => {

when(numberFlag).calledWith(
Expand Down
13 changes: 11 additions & 2 deletions src/util/preemptive-rate-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Logger from "bunyan";

// List of queues we want to apply the preemptive rate limiting on
const TARGETTED_QUEUES = ["backfill"];
const DEFAULT_PREEMPTY_RATELIMIT_DELAY_IN_SECONDS = 30 * 60; //30 minutes

// Fetch the rate limit from GitHub API and check if the usages has exceeded the preemptive threshold
export const preemptiveRateLimitCheck = async (context: SQSMessageContext<any>, sqsQueue: SqsQueue<any>) : Promise<boolean> => {
Expand Down Expand Up @@ -47,6 +48,14 @@ const getRateResetTime = (rateLimitResponse: Octokit.RateLimitGetResponse, log:
const resetEpochDateTime = Math.max(rateLimitResponse?.resources?.core?.reset, rateLimitResponse?.resources?.graphql?.reset);
// Get the difference in seconds between now and reset time
const timeToResetInSeconds = resetEpochDateTime - (Date.now()/1000);
log.info({ timeToResetInSeconds }, "Preemptive rate limit reset time");
return timeToResetInSeconds;
const finalTimeToRestInSeconds = timeToResetInSeconds <= 0 ? DEFAULT_PREEMPTY_RATELIMIT_DELAY_IN_SECONDS : timeToResetInSeconds;

log.info({
timeToResetInSeconds,
finalTimeToRestInSeconds,
coreReset: rateLimitResponse?.resources?.core?.reset,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why just don't log the whole object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel this should be enough. But I don't have a strong reason either way though.

graphqlRest: rateLimitResponse?.resources?.graphql?.reset
}, "Preemptive rate limit reset time");

return finalTimeToRestInSeconds;
};