Simulators
Rate limits
Check that your mail code slows down when Lob refuses to create an address or postcard because it came too fast. With rate_limits: real on, the simulator applies the limit Rate limits describes. Each key’s window opens at its first create and ends at the whole second X-Rate-Limit-Reset names, at most 5 seconds later.
With the limit off, the default, 151 creates in one curl process all succeed, and each response still includes Lob’s counter headers. Wait out that window and turn the limit on. Then create 151 addresses under one key in one curl process, read the list and create under a second key. Last, reset, then read and delete an address between two creates:
URL="$RYSTIC_URL/v1/addresses"
ADDR='{"name":"Ada Lovelace","address_line1":"210 King St","address_city":"San Francisco","address_state":"CA","address_zip":"94107","address_country":"US"}'
BODY=$(mktemp); HEADERS=$(mktemp)
curl -s -u test_one: -H 'Content-Type: application/json' -d "$ADDR" -D "$HEADERS" -w '%{http_code}\n' $(for i in $(seq 151); do echo "-o /dev/null $URL"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 151
grep -iE '^x-rate-limit-(limit|remaining):' "$HEADERS" | head -2 | tr -d '\r' | tr 'A-Z' 'a-z'
# -> x-rate-limit-limit: 150
# -> x-rate-limit-remaining: 149
sleep 5
curl -s -X POST "$RYSTIC_URL/_rystic/network" -o /dev/null -w '%{http_code}\n' -d '{"rate_limits": "real"}'
# -> 204
curl -s -u test_one: -H 'Content-Type: application/json' -d "$ADDR" -D "$HEADERS" -w '%{http_code}\n' $(for i in $(seq 150); do echo "-o /dev/null $URL"; done) -o "$BODY" "$URL" | sort | uniq -c | awk '{print $2, $1}'
# -> 200 150
# -> 429 1
jq -c .error "$BODY"
# -> {"message":"Rate limit exceeded. Please wait 5 seconds and try your request again.","code":"rate_limit_exceeded","status_code":429}
grep -i '^x-rate-limit-remaining' "$HEADERS" | tail -1 | tr -d '\r' | tr 'A-Z' 'a-z'
# -> x-rate-limit-remaining: 0
awk -v now="$(date +%s)" 'tolower($1) == "x-rate-limit-reset:" {d = $2 - now} END {print (d >= 0 && d <= 5) ? "resets within 5 seconds" : "no reset"}' "$HEADERS"
# -> resets within 5 seconds
awk 'tolower($0) ~ /^retry-after:/ {n++} END {print n+0}' "$HEADERS"
# -> 0
curl -s -u test_one: -o /dev/null -w '%{http_code}\n' "$URL?limit=1"
# -> 200
curl -s -u test_one: -D - -o /dev/null "$RYSTIC_URL/v1/postcards" -d to=adr_0000000000000001 -d from=adr_0000000000000001 -d front='<html>Hello</html>' -d back='<html>World</html>' -d use_type=operational | grep -iE '^(http|x-rate-limit-remaining)' | tr -d '\r' | tr 'A-Z' 'a-z'
# -> http/1.1 200 ok
# -> x-rate-limit-remaining: 149
curl -s -u test_two: -H 'Content-Type: application/json' -d "$ADDR" -o /dev/null -w '%{http_code}\n' "$URL"
# -> 200
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
ID=$(curl -s -u test_one: -H 'Content-Type: application/json' -d "$ADDR" -D "$HEADERS" "$URL" | jq -r .id)
grep -i '^x-rate-limit-remaining' "$HEADERS" | tr -d '\r' | tr 'A-Z' 'a-z'
# -> x-rate-limit-remaining: 149
{ curl -s -u test_one: -D - -o /dev/null "$URL?limit=1" "$URL/$ID"; curl -s -u test_one: -X DELETE -D - -o /dev/null "$URL/$ID"; } | awk 'tolower($0) ~ /^x-rate-limit-/ {n++} END {print n+0}'
# -> 0
curl -s -u test_one: -H 'Content-Type: application/json' -d "$ADDR" -D - -o /dev/null "$URL" | grep -i '^x-rate-limit-remaining' | tr -d '\r' | tr 'A-Z' 'a-z'
# -> x-rate-limit-remaining: 148
The 151st create returned Lob’s 429 rate_limit_exceeded body with no Retry-After header. Its X-Rate-Limit-Reset names the window’s end in Unix seconds. The list read was served, since reads count toward no limit. Creating a postcard under the same key drew on its own window of 150. The second key had its own allowance.
After the reset the limit is still on, and every key starts a fresh window. The list read, the read by id and the delete return no counter header and leave the create’s allowance where it was.