Skip to content

Commit a1f534e

Browse files
nisamsonpdxjohnny
andcommitted
Add rate limiting to address #1081 (#1085)
* Add rate limiting to address #1081 Workaround for issue #1081. Limits max concurrent connections for downloading the CVE tarballs to two at a time. Moves non-response related code out of the async block to avoid holding a semaphore permit unnecessarily. * style: format with black Signed-off-by: John Andersen <[email protected]> Co-authored-by: John Andersen <[email protected]>
1 parent 3e30fcf commit a1f534e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

cve_bin_tool/cvedb.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
DISK_LOCATION_DEFAULT = os.path.join(os.path.expanduser("~"), ".cache", "cve-bin-tool")
3636
DBNAME = "cve.db"
3737
OLD_CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "cvedb")
38+
# Workaround for issue #1081
39+
RATE_LIMITER = asyncio.BoundedSemaphore(2)
3840

3941

4042
class CVEDB:
@@ -130,12 +132,13 @@ async def cache_update(self, session, url, sha, chunk_size=16 * 1024):
130132
self.LOGGER.debug(f"Correct SHA for {filename}")
131133
return
132134
self.LOGGER.debug(f"Updating CVE cache for {filename}")
133-
async with session.get(url) as response:
134-
gzip_data = await response.read()
135-
json_data = gzip.decompress(gzip_data)
136-
gotsha = hashlib.sha256(json_data).hexdigest().upper()
137-
async with FileIO(filepath, "wb") as filepath_handle:
138-
await filepath_handle.write(gzip_data)
135+
async with RATE_LIMITER:
136+
async with session.get(url) as response:
137+
gzip_data = await response.read()
138+
json_data = gzip.decompress(gzip_data)
139+
gotsha = hashlib.sha256(json_data).hexdigest().upper()
140+
async with FileIO(filepath, "wb") as filepath_handle:
141+
await filepath_handle.write(gzip_data)
139142
# Raise error if there was an issue with the sha
140143
if gotsha != sha:
141144
# Remove the file if there was an issue

0 commit comments

Comments
 (0)