Skip to content

Commit

Permalink
Tweak Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dakanji committed Dec 27, 2024
1 parent f94136b commit 1a33632
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 188 deletions.
17 changes: 0 additions & 17 deletions .github/actions/avoid-rate-limit/action.yml

This file was deleted.

78 changes: 0 additions & 78 deletions .github/actions/check-rate-limit/action.yml

This file was deleted.

112 changes: 112 additions & 0 deletions .github/actions/handle-rate-limits/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Rate Limit Handler
description: Check rate limits and dynamically calculate/apply sleep times.
inputs:
token:
description: Personal Access Token (PAT) for GitHub API.
required: true
runs:
using: "composite"
steps:
- name: Handle Rate Limits
shell: bash
run: |
TOKEN="${{ inputs.token }}"
SLEEP_TIME=0
BASE_TIME=64
MAX_RETRIES=6
RETRY_COUNT=0
calculate_sleep_time() {
RATE_LIMIT_DATA=$(curl -s --max-time 10 \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/rate_limit)
printf "Rate Limit Response:\n%s\n" "$RATE_LIMIT_DATA"
# Validate JSON response
if ! echo "$RATE_LIMIT_DATA" | jq -e '.' > /dev/null 2>&1; then
SLEEP_TIME=$BASE_TIME
echo "Error: Invalid JSON Response!"
echo " Defaulting sleep time to $SLEEP_TIME seconds."
return
fi
REMAINING_CALLS=$(echo "$RATE_LIMIT_DATA" | jq -r '.resources.core.remaining // 0')
RESET_TIME=$(echo "$RATE_LIMIT_DATA" | jq -r '.resources.core.reset // 0')
CURRENT_TIME=$(date +%s)
WAIT_PERIOD=$(( RESET_TIME - CURRENT_TIME ))
if (( WAIT_PERIOD < 0 )); then
SLEEP_TIME=$(( BASE_TIME * 2 ))
echo "Error: Invalid Wait Time Esitmate!"
echo " Defaulting sleep time to $SLEEP_TIME seconds."
return
fi
if (( REMAINING_CALLS > 99 )); then
SLEEP_TIME=0
elif (( REMAINING_CALLS > 1 )); then
SLEEP_TIME=$(( WAIT_PERIOD / REMAINING_CALLS ))
else
SLEEP_TIME=$(( WAIT_PERIOD + 60 ))
fi
if (( SLEEP_TIME < 0 )); then
SLEEP_TIME=0
fi
echo "Calculated sleep time is $SLEEP_TIME seconds."
}
handle_secondary_rate_limit() {
while (( RETRY_COUNT < MAX_RETRIES )); do
RESPONSE=$(curl -s -I --max-time 10 \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/rate_limit)
printf "Secondary Rate Limit Response:\n%s\n" "$RESPONSE"
RETRY_BASE=$(echo "$RESPONSE" | grep -i '^retry-after:' | awk '{print $2}' | tr -d '\r')
if [[ ! -n "$RETRY_BASE" ]]; then
RETRY_AFTER=$(( $BASE_TIME / 16 ))
echo "'Retry-After' header not detected."
echo "Proceed after a token $RETRY_AFTER seconds."
sleep "$RETRY_AFTER"
break
fi
if (( RETRY_BASE < BASE_TIME )); then
RETRY_BASE=$BASE_TIME
fi
SCALE_FACTOR=$(( RETRY_COUNT + 1 ))
RETRY_AFTER=$(( RETRY_BASE * SCALE_FACTOR * SCALE_FACTOR ))
echo "Secondary Rate Limit is Active."
echo "Wait $RETRY_AFTER seconds for reset..."
sleep "$RETRY_AFTER"
RETRY_COUNT=$SCALE_FACTOR
if (( RETRY_COUNT >= MAX_RETRIES )); then
break
fi
done
}
# Check for secondary rate limits
handle_secondary_rate_limit
# Calculate sleep time for primary rate limits
calculate_sleep_time
# Pause if necessary
if (( SLEEP_TIME == 0 )); then
echo "Skip Pause ... Far from Rate Limit."
else
if (( SLEEP_TIME < BASE_TIME )); then
echo "Defaulting sleep time to $BASE_TIME seconds."
SLEEP_TIME=$BASE_TIME
fi
echo "Apply ${SLEEP_TIME} Second Pause for Rate Limit."
sleep "$SLEEP_TIME"
fi
42 changes: 9 additions & 33 deletions .github/workflows/issues-close.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ jobs:
name: Flag or Close Issues
runs-on: ubuntu-24.04

if: github.event_name == 'schedule' || github.event.inputs.job_id == 'all' || github.event.inputs.job_id == 'stale'
if: github.actor == 'dakanji' && (github.event_name == 'schedule' || github.event.inputs.job_id == 'all' || github.event.inputs.job_id == 'stale')
steps:
- name: Repository Checkout
uses: actions/checkout@v4

- name: Check Rate Limit
id: rate-limit-check01
uses: ./.github/actions/check-rate-limit
- name: Handle Rate Limits
uses: ./.github/actions/handle-rate-limits
with:
token: ${{ secrets.PAT_TOKEN }}

- name: Avoid Rate Limit
uses: ./.github/actions/avoid-rate-limit
with:
sleep_time: ${{ steps.rate-limit-check01.outputs.sleep_time }}

- name: Handle Cancelled/Duplicate/Invalid/Wierd/Not-Planned Issues
uses: actions/stale@v9
with:
Expand All @@ -50,17 +44,11 @@ jobs:
remove-issue-stale-when-updated: false
repo-token: ${{ secrets.PAT_TOKEN }}

- name: Check Rate Limit
id: rate-limit-check02
uses: ./.github/actions/check-rate-limit
- name: Handle Rate Limits
uses: ./.github/actions/handle-rate-limits
with:
token: ${{ secrets.PAT_TOKEN }}

- name: Avoid Rate Limit
uses: ./.github/actions/avoid-rate-limit
with:
sleep_time: ${{ steps.rate-limit-check02.outputs.sleep_time }}

- name: Handle Incomplete Issues
uses: actions/stale@v9
with:
Expand All @@ -82,17 +70,11 @@ jobs:
remove-issue-stale-when-updated: true
repo-token: ${{ secrets.PAT_TOKEN }}

- name: Check Rate Limit
id: rate-limit-check03
uses: ./.github/actions/check-rate-limit
- name: Handle Rate Limits
uses: ./.github/actions/handle-rate-limits
with:
token: ${{ secrets.PAT_TOKEN }}

- name: Avoid Rate Limit
uses: ./.github/actions/avoid-rate-limit
with:
sleep_time: ${{ steps.rate-limit-check03.outputs.sleep_time }}

- name: Handle No-Response Issues
uses: actions/stale@v9
with:
Expand All @@ -114,17 +96,11 @@ jobs:
remove-issue-stale-when-updated: true
repo-token: ${{ secrets.PAT_TOKEN }}

- name: Check Rate Limit
id: rate-limit-check04
uses: ./.github/actions/check-rate-limit
- name: Handle Rate Limits
uses: ./.github/actions/handle-rate-limits
with:
token: ${{ secrets.PAT_TOKEN }}

- name: Avoid Rate Limit
uses: ./.github/actions/avoid-rate-limit
with:
sleep_time: ${{ steps.rate-limit-check04.outputs.sleep_time }}

- name: Catchall Closer
uses: actions/stale@v9
with:
Expand Down
Loading

0 comments on commit 1a33632

Please sign in to comment.