# Rate limits

Each token's REST requests count against GitHub's primary, search and secondary limits, refused past them only once you turn the limits on.

Test how your integration backs off when GitHub refuses a request for going past a rate limit. Each REST response has `x-ratelimit-limit`, `x-ratelimit-remaining` and `x-ratelimit-resource`, counted per token. A search counts against search's 30 a minute, and other REST requests against the primary 5,000 an hour. With the limits off, the default, nothing is refused and the count stops at 0:

```bash
H='Authorization: Bearer ghp_anything'
SEARCH="$RYSTIC_URL/search/issues?q=repo%3Atb-owner%2Ftb-repo+is%3Apr"
curl -s -D - -o /dev/null "$RYSTIC_URL/repos/tb-owner/tb-repo" -H "$H" | awk 'tolower($0) ~ /^x-ratelimit-/ {gsub("\r", ""); print tolower($1), $2}'
# -> x-ratelimit-limit: 5000
# -> x-ratelimit-remaining: 4999
# -> x-ratelimit-resource: core
curl -s -H "$H" -w '%{http_code}\n' $(for i in $(seq 31); do echo "-o /dev/null $SEARCH"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 31
curl -s -D - -o /dev/null "$SEARCH" -H "$H" | awk 'tolower($0) ~ /^x-ratelimit-remaining:/ {gsub("\r", ""); print $2}'
# -> 0
```

Turn the limits on. This token's searches are spent for the minute, so its next search returns 429, while another token's search still returns 200. Past 80 writes in a minute, the secondary limit refuses with `retry-after`:

```bash
curl -s -X POST "$RYSTIC_URL/_rystic/network" -d '{"rate_limits": "real"}' -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s "$SEARCH" -H "$H" | jq -c '{message, status}'
# -> {"message":"API rate limit exceeded.","status":"429"}
curl -s "$SEARCH" -H 'Authorization: Bearer ghp_another' -o /dev/null -w '%{http_code}\n'
# -> 200
N=$(curl -s "$RYSTIC_URL/repos/tb-owner/tb-repo/pulls?per_page=1" -H "$H" | jq '.[0].number')
curl -s -H "$H" -d '{"body":"hi"}' -w '%{http_code}\n' $(for i in $(seq 80); do echo "-o /dev/null $RYSTIC_URL/repos/tb-owner/tb-repo/issues/$N/comments"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 201 80
curl -s -D - -o /dev/null -H "$H" -d '{"body":"hi"}' "$RYSTIC_URL/repos/tb-owner/tb-repo/issues/$N/comments" | awk '{gsub("\r", "")} tolower($1) ~ /^http/ {print $2} tolower($1) == "retry-after:" && $2 >= 1 && $2 <= 60 {print "retry-after within the minute"}'
# -> 429
# -> retry-after within the minute
```

After a reset the setting is still on and the search allowance is full again:

```bash
curl -s -X POST "$RYSTIC_URL/_rystic/reset" -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s "$RYSTIC_URL/_rystic/network" | jq -r .rate_limits
# -> real
curl -s -D - -o /dev/null "$SEARCH" -H "$H" | awk 'tolower($0) ~ /^x-ratelimit-remaining:/ {gsub("\r", ""); print $2}'
# -> 29
```

---

Source: https://www.rystic.ai/docs/simulators/github-pulls/features/rate-limits · Markdown: https://www.rystic.ai/docs/simulators/github-pulls/features/rate-limits.md
