# Rate limits

Turn on Gmail's per-user and per-project quota, so a call past its allowance returns Gmail's 403 rateLimitExceeded.

Check that your client backs off when Gmail refuses a call for spending its quota. Once [`rate_limits: real`](https://www.rystic.ai/docs/network-profiles.md) is set, each method the [Rate limits](https://www.rystic.ai/docs/simulators/gmail.md#rate-limits) table prices spends from its cost's allowance. The simulator counts `threads.get` at 40 units, its cheapest format's cost, so one access token gets 150 of them a minute. Load the busy inbox and read one of its threads 152 times in one go, first with the setting off and then with it on:

```bash
G=(-H 'Authorization: Bearer ya29.placeholder')
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -d '{"name":"gmail_busy_inbox"}' -o /dev/null
URL="$RYSTIC_URL/gmail/v1/users/me/threads/19a4c0d1e2f30004"
curl -s "${G[@]}" -w '%{http_code}\n' $(for i in $(seq 152); do echo "-o /dev/null $URL"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 152
curl -s -X POST "$RYSTIC_URL/_rystic/network" -o /dev/null -w '%{http_code}\n' -d '{"rate_limits": "real"}'
# -> 204
curl -s -X POST "$RYSTIC_URL/_rystic/reset" -o /dev/null
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -d '{"name":"gmail_busy_inbox"}' -o /dev/null
curl -s "${G[@]}" -w '%{http_code}\n' $(for i in $(seq 152); do echo "-o /dev/null $URL"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 150
# -> 403 2
curl -s "$URL" "${G[@]}" | jq -c '.error | {code, status, reason: .errors[0].reason}'
# -> {"code":403,"status":"PERMISSION_DENIED","reason":"rateLimitExceeded"}
curl -s "$URL" "${G[@]}" | jq -r '.error.message'
# -> Quota exceeded for quota metric 'Total Query Cost' and limit 'Units per minute per user' of service 'gmail.googleapis.com' for consumer 'project_number:000000000000'.
curl -s "$URL" "${G[@]}" | jq -c '.error.details[0].metadata | {quota_limit_value, window_start_time: has("window_start_time")}'
# -> {"quota_limit_value":"6000","window_start_time":false}
curl -s -D - -o /dev/null "$URL" "${G[@]}" | awk 'tolower($0) ~ /^(retry-after|x-ratelimit-|ratelimit)/ {n++} END {print n+0}'
# -> 0
curl -s -o /dev/null -w '%{http_code}\n' "$URL" -H 'Authorization: Bearer ya29.second'
# -> 200
curl -s -o /dev/null -w '%{http_code}\n' "$RYSTIC_URL/gmail/v1/users/me/messages/19a4c0d1e2f31004" "${G[@]}"
# -> 200
```

The refusal has no `Retry-After` or rate-limit counter header, so a client waits out the minute by its own backoff. The second token's read succeeds because each token keeps its own per-user allowance. The first token's `messages.get` succeeds too, since a 20-unit call spends a different allowance from a 40-unit one.

**Differs from Gmail:** Gmail charges a `threads.get` with no `format` 120 units and refuses it after about 50 a minute. It also counts every token of one user against one quota, so the second token's read would be refused there. And with the token's 6,000 units spent, Gmail would refuse the `messages.get` as well.

### Reset the allowances

A reset gives every allowance back and empties the mailbox, and the quota stays on. Load the inbox again and the same thread reads at once:

```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 -X POST "$RYSTIC_URL/_rystic/scenario" -d '{"name":"gmail_busy_inbox"}' -o /dev/null
curl -s -o /dev/null -w '%{http_code}\n' "$URL" "${G[@]}"
# -> 200
```

---

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