-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrate-limit-control.ts
More file actions
183 lines (155 loc) · 5.59 KB
/
rate-limit-control.ts
File metadata and controls
183 lines (155 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Rate Limit Control
* Layer: poller
*
* Pure logic for handling 403/429 responses and gating poll cadence.
*
* Based on guidance in current docs at time of writing:
* https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#exceeding-the-rate-limit
*/
import type { RateLimitErrorDetails } from '../github';
import type { PollLogEntry, RateLimitErrorKind, PollLogError } from '../types';
import type { SleepPlan } from './sleep-plan';
export const MAX_SECONDARY_RETRIES = 5;
export const SECONDARY_DEFAULT_WAIT_MS = 60_000;
export interface RateLimitControlState {
blocked_until_ms: number | null;
secondary_consecutive: number;
}
export interface RateLimitEvent {
kind: RateLimitErrorKind;
details: RateLimitErrorDetails;
}
export interface RateLimitDecision {
state: RateLimitControlState;
kind: RateLimitErrorKind;
next_allowed_at_ms: number | null;
wait_ms: number | null;
secondary_retry_count: number;
fatal: boolean;
}
export interface RateLimitGateResult extends SleepPlan {
blocked: boolean;
}
export function createRateLimitControlState(): RateLimitControlState {
return { blocked_until_ms: null, secondary_consecutive: 0 };
}
export function resetRateLimitControl(state: RateLimitControlState): RateLimitControlState {
return { ...state, blocked_until_ms: null, secondary_consecutive: 0 };
}
export function classifyRateLimitError(details: RateLimitErrorDetails): RateLimitErrorKind | null {
if (details.status !== 403 && details.status !== 429) {
return null;
}
const message = (details.message ?? '').toLowerCase();
if (message.includes('secondary') || message.includes('abuse')) {
return 'secondary';
}
// "If you exceed your primary rate limit, you will receive a 403 or 429 response, and the x-ratelimit-remaining header will be 0."
if (details.rate_limit_remaining === 0) {
return 'primary';
}
return 'unknown';
}
export function handleRateLimitError(
state: RateLimitControlState,
event: RateLimitEvent,
nowMs: number,
): RateLimitDecision {
const { details, kind } = event;
const candidates: number[] = [nowMs + SECONDARY_DEFAULT_WAIT_MS];
// Independent checks (no else-if) so we can honor multiple constraints together.
// "If the retry-after response header is present, you should not retry your request until after that many seconds has elapsed."
if (details.retry_after_seconds !== null) {
candidates.push(nowMs + details.retry_after_seconds * 1000);
}
// "If the x-ratelimit-remaining header is 0"
if (details.rate_limit_remaining === 0 && details.rate_limit_reset !== null) {
// "You should not retry your request until after the time specified by the x-ratelimit-reset header."
candidates.push(details.rate_limit_reset * 1000);
}
const baseAllowedAt = Math.max(...candidates);
if (kind === 'secondary') {
const secondaryRetryCount = state.secondary_consecutive + 1;
const baseDelayMs = Math.max(0, baseAllowedAt - nowMs);
// "If your request continues to fail due to a secondary rate limit, wait for an exponentially increasing amount of time between retries."
const multiplier = Math.pow(2, secondaryRetryCount - 1);
const waitMs = baseDelayMs * multiplier;
const nextAllowedAtMs = nowMs + waitMs;
// "throw an error after a specific number of retries."
const fatal = secondaryRetryCount >= MAX_SECONDARY_RETRIES;
return {
state: { blocked_until_ms: nextAllowedAtMs, secondary_consecutive: secondaryRetryCount },
kind,
next_allowed_at_ms: nextAllowedAtMs,
wait_ms: waitMs,
secondary_retry_count: secondaryRetryCount,
fatal,
};
}
if (kind === 'primary') {
const nextAllowedAtMs =
details.rate_limit_reset !== null ? details.rate_limit_reset * 1000 : baseAllowedAt;
const waitMs = Math.max(0, nextAllowedAtMs - nowMs);
return {
state: { blocked_until_ms: nextAllowedAtMs, secondary_consecutive: 0 },
kind,
next_allowed_at_ms: nextAllowedAtMs,
wait_ms: waitMs,
secondary_retry_count: 0,
fatal: false,
};
}
// "Otherwise, wait for at least one minute before retrying."
const nextAllowedAtMs = baseAllowedAt;
const waitMs = Math.max(0, nextAllowedAtMs - nowMs);
return {
state: { blocked_until_ms: nextAllowedAtMs, secondary_consecutive: 0 },
kind,
next_allowed_at_ms: nextAllowedAtMs,
wait_ms: waitMs,
secondary_retry_count: 0,
fatal: false,
};
}
export function applyRateLimitGate(
plan: SleepPlan,
controlState: RateLimitControlState,
nowMs: number,
): RateLimitGateResult {
const blockedUntil = controlState.blocked_until_ms;
if (!blockedUntil || blockedUntil <= nowMs) {
return { ...plan, blocked: false };
}
const waitMs = Math.max(plan.sleepMs, blockedUntil - nowMs);
return {
sleepMs: waitMs,
burst: false,
burstGapMs: plan.burstGapMs,
blocked: true,
};
}
export function buildRateLimitErrorEntry(
event: RateLimitEvent,
pollNumber: number,
timestamp: string,
decision: RateLimitDecision,
): PollLogEntry {
const error: PollLogError = {
kind: event.kind,
status: event.details.status,
message: event.details.message ?? null,
retry_after_seconds: event.details.retry_after_seconds,
rate_limit_remaining: event.details.rate_limit_remaining,
rate_limit_reset: event.details.rate_limit_reset,
next_allowed_at:
decision.next_allowed_at_ms !== null ? Math.ceil(decision.next_allowed_at_ms / 1000) : null,
secondary_retry_count: decision.secondary_retry_count,
};
return {
timestamp,
poll_number: pollNumber,
buckets: {},
error,
};
}