Skip to content

Commit 8a82cfa

Browse files
committed
Enhance README and code comments for clarity on rate limits and throttling behavior
1 parent 7959c1c commit 8a82cfa

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

api/ruby/find-inactive-members/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ gem install octokit faraday
3232

3333
The `OCTOKIT_ACCESS_TOKEN` is required in order to see activities on private repositories. Also note that GitHub.com has an rate limit of 60 unauthenticated requests per hour, which this tool can easily exceed. Access tokens can be generated at https://github.com/settings/tokens. The `OCTOKIT_API_ENDPOINT` isn't required if connecting to GitHub.com, but is required if connecting to a GitHub Enterprise instance.
3434

35+
`OCTOKIT_ACCESS_TOKEN` needs the scopes `read:org`, `read:user`, `repo`, and `user:email`.
36+
3537
```shell
36-
export OCTOKIT_ACCESS_TOKEN=00000000000000000000000 # Required if looking for activity in private repositories.
38+
export OCTOKIT_ACCESS_TOKEN=00000000000000000000000 # Required if looking for activity in private repositories.
3739
export OCTOKIT_API_ENDPOINT="https://<your_github_enterprise_instance>/api/v3" # Not required if connecting to GitHub.com.
3840
```
3941

@@ -55,3 +57,17 @@ Members are defined as inactive if they **have not performed** any of the follow
5557
- Merged or pushed commits into the default branch
5658
- Opened an Issue or Pull Request
5759
- Commented on an Issue or Pull Request
60+
61+
## Rate Limit
62+
63+
The script will use the following rate limit headers returned by the API to throttle requests in order to stay within the rate limit. You can disable throttling using the `-t` option.
64+
65+
| Header name | Description |
66+
| --- | --- |
67+
| `x-ratelimit-limit` | The maximum number of requests that you can make per hour |
68+
| `x-ratelimit-remaining` | The number of requests remaining in the current rate limit window |
69+
| `x-ratelimit-used` | The number of requests you have made in the current rate limit window |
70+
| `x-ratelimit-reset` | The time at which the current rate limit window resets, in UTC epoch seconds |
71+
| `x-ratelimit-resource` | The rate limit resource that the request counted against. |
72+
73+
For more information about the different resources, see [REST API endpoints for rate limits](https://docs.github.com/en/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user).

api/ruby/find-inactive-members/find_inactive_members.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def throttle_request
112112
elapsed_hour = @last_request_time - @hour_start_time
113113
current_rate = elapsed_hour > 0 ? (@request_count / elapsed_hour * 3600).round(1) : 0
114114
github_info = @github_rate_limit_remaining ? " GitHub: #{@github_rate_limit_remaining} remaining" : ""
115-
$stderr.print "Throttling status: #{@request_count} requests in #{elapsed_hour.round(1)}s (#{current_rate}/hour rate)#{github_info}\n"
115+
$stderr.print "\nThrottling status: #{@request_count} requests in #{elapsed_hour.round(1)}s (#{current_rate}/hour rate)#{github_info}\n"
116116
end
117117
end
118118

@@ -122,7 +122,7 @@ def log_throttle_status
122122

123123
elapsed_hour = Time.now - @hour_start_time
124124
rate_per_hour = elapsed_hour > 0 ? (@request_count / elapsed_hour * 3600).round(1) : 0
125-
$stderr.print "Throttle debug: #{@request_count} requests in last #{elapsed_hour.round(1)}s (#{rate_per_hour}/hour rate)\n"
125+
$stderr.print "\nThrottle debug: #{@request_count} requests in last #{elapsed_hour.round(1)}s (#{rate_per_hour}/hour rate)\n"
126126
end
127127
end
128128

@@ -179,7 +179,7 @@ def check_rate_limit
179179
rate_limit = retry_on_403("checking rate limit") do
180180
@client.rate_limit
181181
end
182-
info "Rate limit: #{rate_limit.remaining}/#{rate_limit.limit}\n"
182+
info "\nRate limit: #{rate_limit.remaining}/#{rate_limit.limit}\n"
183183
info "Rate limit resets at: #{rate_limit.resets_at}\n"
184184
info "Throttling: Limited to #{ThrottleMiddleware::MAX_REQUESTS_PER_HOUR} requests/hour (#{ThrottleMiddleware::MIN_DELAY_SECONDS.round(2)}s min delay)\n"
185185
end
@@ -222,7 +222,7 @@ def retry_on_403(description, max_retries = 3)
222222
return yield
223223
rescue Octokit::Forbidden => e
224224
retries += 1
225-
info "⚠️ 403 Forbidden error occurred while #{description}\n"
225+
info "\n⚠️ 403 Forbidden error occurred while #{description}\n"
226226

227227
if retries <= max_retries
228228
info "🔄 Waiting 5 seconds before retry #{retries}/#{max_retries}...\n"
@@ -507,7 +507,6 @@ def member_activity
507507
end
508508

509509
opts.on('-t', '--no-throttle', "Disable API request throttling (use with caution)") do |t|
510-
puts "DEBUG: -t flag was triggered, t value = #{t.inspect}"
511510
options[:no_throttle] = true
512511
end
513512

0 commit comments

Comments
 (0)