Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Last active November 11, 2022 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonRichards/707b8680a49155e6e8cb04a4eb334991 to your computer and use it in GitHub Desktop.
Save DonRichards/707b8680a49155e6e8cb04a4eb334991 to your computer and use it in GitHub Desktop.
Github Rate Limited Checker / Delayer
#!/usr/bin/env bash
set -e
: '
author :DonRichards
date :20221020
usage :bash github_rate_limit.sh
Description:
This is an example of how to check Githubs rate limit and wait for it to reset.
This is really useful for those rare cases where you are running a script that
makes a lot of requests to the Githubs API. I have also found it helpful avoiding
CI/CD pipelines from failing due to rate limits.
https://docs.github.com/en/rest/rate-limit#about-the-rate-limit-api
Warning: this could dramatically increase your build time if the
ESTIMATED_QUERIES_NEEDED is set too high.
'
GH_TOKEN=${1}
if [ -z "${GH_TOKEN}" ]; then
echo "No GitHub token provided."
exit 1
fi
echo ""
echo ""
echo " -------------------------------------------------------------------------- "
echo " - Running Github rate limit check - "
echo " -------------------------------------------------------------------------- "
echo ""
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed.' >&2
exit 1
# apk add jq --quiet
# apt-get install jq
fi
: '
Estimate the number of requests that will be made during the use of this script.
Too little requests will cause the script to fail.
Too many requests will cause the script to take a long time to complete.
'
ESTIMATED_QUERIES_NEEDED=450
function check_github_rate_limit() {
REMAINING=$(curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" https://api.github.com/rate_limit | jq .rate.remaining)
# While remaining request count is too low to complete this cycle, sleep for 30 seconds and check again.
while [ "$REMAINING" -lt "$ESTIMATED_QUERIES_NEEDED" ]; do
REMAINING=$(curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" https://api.github.com/rate_limit | jq .rate.remaining)
GH=$(curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" https://api.github.com/rate_limit | jq .rate.reset)
now=$(date '+%s')
echo ""
echo ""
echo " -------------------------------------------------------------------------- "
echo " - Github remaining request count: $REMAINING "
echo " - Github's hourly rate limit is close to going over. "
echo " - Waiting $(( $(( $GH - $now )) / 60 )) minutes for Github rate limit to reset."
echo " -------------------------------------------------------------------------- "
sleep 30
done
echo " -------------------------------------------------------------------------- "
echo ""
}
# Some commands that will use up the rate limit.
for i in {1..10000}; do
# INSERT COMMANDS HERE OR SOMETHING
check_github_rate_limit
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment