Fix HTTP 429 rate limit errors for USCensusPEP_Annual_Population and USCensusPEP_Sex - #2251
Open
niveditasing wants to merge 2 commits into
Open
niveditasing wants to merge 2 commits into
niveditasing wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request enhances the data downloading process in the US Census PEP preprocessing scripts by introducing robust URL probing, exponential backoff, and persistent caching to handle rate limits and avoid redundant downloads. The review feedback highlights two critical improvements: first, the regex pattern for identifying historical files in the cache cleanup process is too restrictive and could lead to the unintended deletion of historical files; second, temporary files created during download may leak on disk if exceptions occur, which should be resolved by wrapping the file operations in try-finally blocks.
…nd clean up temp files in try-finally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes recurring P0 production failures caused by HTTP 429 (Too Many Requests) rate limiting from
www2.census.govforUSCensusPEP_Annual_PopulationandUSCensusPEP_Sex.Root Cause
@retrywrappedrequests.get()without checkingresponse.status_code. When Census returned HTTP 429 or 503, no exception was thrown, bypassing retries and immediately aborting the import.USCensusPEP_Sex,cleanup()deleted cached historical data every 30 days.HEADrequests probing candidate years (up to 112 HEAD requests per year) triggered WAF throttling.Changes Made
USCensusPEP_Annual_Population: Added persistent caching viagcs_folder/usa_annual_population_source_files(symlinked to mounted GCS bucket cache in Cloud Batch). If a historical file is present, it copies locally without making an HTTP request.USCensusPEP_Sex: Modifiedcleanup()to preserve immutable historical files (pre-2021) indefinitely.Retry-Afterresponse header when present, and applies jittered exponential backoff.requests.Sessionwith browser User-Agent header.time.sleep(1.0)between uncached file downloads andtime.sleep(0.2)–0.5betweenHEADprobes.min(2030, dt.now().year + 1).Testing
python3 -m unittest discover -v -s scripts/us_census/pep/annual_population -p "*test*.py"-> PASSpython3 -m unittest discover -v -s scripts/us_census/pep/us_pep_sex -p "*test*.py"-> PASSyapf --style=google.