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

Limit aiohttp simultaneously opened connections to NVD #1093

Merged
merged 1 commit into from
Mar 11, 2021
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
11 changes: 5 additions & 6 deletions cve_bin_tool/cvedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
DISK_LOCATION_DEFAULT = os.path.join(os.path.expanduser("~"), ".cache", "cve-bin-tool")
DBNAME = "cve.db"
OLD_CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "cvedb")
# Workaround for issue #1081
RATE_LIMITER = asyncio.BoundedSemaphore(2)


class CVEDB:
Expand Down Expand Up @@ -132,9 +130,9 @@ async def cache_update(self, session, url, sha, chunk_size=16 * 1024):
self.LOGGER.debug(f"Correct SHA for {filename}")
return
self.LOGGER.debug(f"Updating CVE cache for {filename}")
async with RATE_LIMITER:
async with session.get(url) as response:
gzip_data = await response.read()

async with session.get(url) as response:
gzip_data = await response.read()
json_data = gzip.decompress(gzip_data)
gotsha = hashlib.sha256(json_data).hexdigest().upper()
async with FileIO(filepath, "wb") as filepath_handle:
Expand Down Expand Up @@ -193,7 +191,8 @@ async def refresh(self):
self.LOGGER.info("Checking if there is a newer version.")
check_latest_version()
if not self.session:
self.session = aiohttp.ClientSession(trust_env=True)
connector = aiohttp.TCPConnector(limit_per_host=19)
self.session = aiohttp.ClientSession(connector=connector, trust_env=True)
self.LOGGER.info("Downloading CVE data...")
nvd_metadata, curl_metadata = await asyncio.gather(
self.nist_scrape(self.session), self.get_curl_versions(self.session)
Expand Down
5 changes: 4 additions & 1 deletion test/test_cvedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def teardown_class(cls):

@pytest.mark.asyncio
async def test_00_getmeta(self):
async with aiohttp.ClientSession(trust_env=True) as session:
connector = aiohttp.TCPConnector(limit_per_host=19)
async with aiohttp.ClientSession(
connector=connector, trust_env=True
) as session:
_jsonurl, meta = await self.cvedb.getmeta(
session,
"https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-modified.meta",
Expand Down