Skip to content

Commit

Permalink
fix: respect GitHub rate limiting (#377)
Browse files Browse the repository at this point in the history
Previously, the client would repeatedly attempt requests until getting
actually rate limited by GitHub. If respecting rate limit would result in
the process sleeping more than 1 hour, instead fail.

Signed-off-by: Will Murphy <[email protected]>
  • Loading branch information
willmurphyscode authored Oct 31, 2023
1 parent 984b5cb commit 349985e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/vunnel/providers/github/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import datetime
import logging
import os
import time
from decimal import Decimal, DecimalException

import requests
Expand All @@ -42,6 +43,9 @@
"SWIFT": "swift",
}

GITHUB_RATE_LIMIT_REMAINING_HEADER = "x-ratelimit-remaining"
GITHUB_RATE_LIMIT_RESET_HEADER = "x-ratelimit-reset"


class Parser:
def __init__( # noqa: PLR0913
Expand Down Expand Up @@ -207,9 +211,26 @@ def get_query(token, query, timeout=125, api_url="https://api.github.com/graphql
logger = logging.getLogger("get-query")

headers = {"Authorization": f"token {token}"}
logger.debug(f"downloading github advisories from {api_url}")
logger.info(f"downloading github advisories from {api_url}")

response = requests.post(api_url, json={"query": query}, timeout=timeout, headers=headers)
if GITHUB_RATE_LIMIT_REMAINING_HEADER in response.headers and GITHUB_RATE_LIMIT_RESET_HEADER in response.headers:
remaining = int(response.headers[GITHUB_RATE_LIMIT_REMAINING_HEADER])
# reset time is the time in UNIX Epoch Seconds at which
# the rate limit will reset.
reset_time = int(response.headers[GITHUB_RATE_LIMIT_RESET_HEADER])
logger.debug(f"github rate limit has {remaining} requests left {reset_time}")
if remaining < 10:
current_time = int(time.time())
sleep_time = reset_time - current_time
# note that the rate limit resets 1x / hour, so this could be a long time
if sleep_time > 1 and sleep_time < 3600: # never sleep for more than 1 hour
logger.info(f"sleeping for {sleep_time} seconds to allow GitHub rate limit to reset")
time.sleep(sleep_time)
elif sleep_time > 3600:
raise Exception(
f"github rate limit exhaused and not expected to reset for {sleep_time} seconds. Try again later.",
)
response.raise_for_status()
if response.status_code == 200:
return response.json()
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/providers/github/test_github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import pytest
import time
from unittest.mock import patch, Mock
from vunnel import result, workspace
from vunnel.providers.github import Config, Provider, parser
from vunnel.utils import fdb as db
Expand Down Expand Up @@ -447,6 +449,48 @@ def test_provider_schema(helpers, fake_get_query, advisories):
assert workspace.result_schemas_valid(require_entries=True)


@patch("time.sleep")
@patch("requests.post")
def test_provider_respects_github_rate_limit(mock_post, mock_sleep):
response = Mock()
in_five_seconds = int(time.time()) + 5
response.headers = {"x-ratelimit-remaining": 9, "x-ratelimit-reset": in_five_seconds}
response.status_code = 200
mock_post.return_value = response

def mock_json():
return "{}"

def mock_raise_for_status():
pass

response.json = mock_json
response.raise_for_status = mock_raise_for_status
parser.get_query("some-token", "some-query")
mock_sleep.assert_called_once()


@patch("time.sleep")
@patch("requests.post")
def test_provider_respects_github_rate_limit(mock_post, mock_sleep):
response = Mock()
in_five_seconds = int(time.time()) + 5
response.headers = {"x-ratelimit-remaining": 11, "x-ratelimit-reset": in_five_seconds}
response.status_code = 200
mock_post.return_value = response

def mock_json():
return "{}"

def mock_raise_for_status():
pass

response.json = mock_json
response.raise_for_status = mock_raise_for_status
parser.get_query("some-token", "some-query")
mock_sleep.assert_not_called()


def test_provider_via_snapshot(helpers, fake_get_query, advisories):
fake_get_query([advisories(), advisories(has_next_page=True)])
workspace = helpers.provider_workspace_helper(name=Provider.name())
Expand Down

0 comments on commit 349985e

Please sign in to comment.