-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.ts
194 lines (173 loc) · 5.88 KB
/
index.ts
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
184
185
186
187
188
189
190
191
192
193
194
// @ts-expect-error
import BottleneckLight from "bottleneck/light";
import { Octokit } from "@octokit/core";
import { OctokitOptions } from "@octokit/core/dist-types/types.d";
import { Groups } from "./types";
import { VERSION } from "./version";
import { wrapRequest } from "./wrap-request";
import triggersNotificationPaths from "./generated/triggers-notification-paths";
import { routeMatcher } from "./route-matcher";
// Workaround to allow tests to directly access the triggersNotification function.
const regex = routeMatcher(triggersNotificationPaths);
const triggersNotification = regex.test.bind(regex);
const groups: Groups = {};
// @ts-expect-error
const createGroups = function (Bottleneck, common) {
groups.global = new Bottleneck.Group({
id: "octokit-global",
maxConcurrent: 10,
...common,
});
groups.search = new Bottleneck.Group({
id: "octokit-search",
maxConcurrent: 1,
minTime: 2000,
...common,
});
groups.write = new Bottleneck.Group({
id: "octokit-write",
maxConcurrent: 1,
minTime: 1000,
...common,
});
groups.notifications = new Bottleneck.Group({
id: "octokit-notifications",
maxConcurrent: 1,
minTime: 3000,
...common,
});
};
export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) {
const {
enabled = true,
Bottleneck = BottleneckLight,
id = "no-id",
timeout = 1000 * 60 * 2, // Redis TTL: 2 minutes
connection,
} = octokitOptions.throttle || {};
if (!enabled) {
return {};
}
const common = { connection, timeout };
if (groups.global == null) {
createGroups(Bottleneck, common);
}
const state = Object.assign(
{
clustering: connection != null,
triggersNotification,
minimumSecondaryRateRetryAfter: 5,
retryAfterBaseValue: 1000,
retryLimiter: new Bottleneck(),
id,
...groups,
},
octokitOptions.throttle
);
const isUsingDeprecatedOnAbuseLimitHandler =
typeof state.onAbuseLimit === "function" && state.onAbuseLimit;
if (
typeof (isUsingDeprecatedOnAbuseLimitHandler
? state.onAbuseLimit
: state.onSecondaryRateLimit) !== "function" ||
typeof state.onRateLimit !== "function"
) {
throw new Error(`octokit/plugin-throttling error:
You must pass the onSecondaryRateLimit and onRateLimit error handlers.
See https://octokit.github.io/rest.js/#throttling
const octokit = new Octokit({
throttle: {
onSecondaryRateLimit: (retryAfter, options) => {/* ... */},
onRateLimit: (retryAfter, options) => {/* ... */}
}
})
`);
}
const events = {};
const emitter = new Bottleneck.Events(events);
// @ts-expect-error
events.on(
"secondary-limit",
isUsingDeprecatedOnAbuseLimitHandler
? function (...args: [number, OctokitOptions, Octokit]) {
octokit.log.warn(
"[@octokit/plugin-throttling] `onAbuseLimit()` is deprecated and will be removed in a future release of `@octokit/plugin-throttling`, please use the `onSecondaryRateLimit` handler instead"
);
// @ts-expect-error
return state.onAbuseLimit(...args);
}
: state.onSecondaryRateLimit
);
// @ts-expect-error
events.on("rate-limit", state.onRateLimit);
// @ts-expect-error
events.on("error", (e) =>
octokit.log.warn("Error in throttling-plugin limit handler", e)
);
// @ts-expect-error
state.retryLimiter.on("failed", async function (error, info) {
const [state, request, options] = info.args;
const { pathname } = new URL(options.url, "http://github.test");
const shouldRetryGraphQL =
pathname.startsWith("/graphql") && error.status !== 401;
if (!(shouldRetryGraphQL || error.status === 403)) {
return;
}
const retryCount = ~~request.retryCount;
request.retryCount = retryCount;
// backward compatibility
options.request.retryCount = retryCount;
const { wantRetry, retryAfter = 0 } = await (async function () {
if (/\bsecondary rate\b/i.test(error.message)) {
// The user has hit the secondary rate limit. (REST and GraphQL)
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#secondary-rate-limits
// The Retry-After header can sometimes be blank when hitting a secondary rate limit,
// but is always present after 2-3s, so make sure to set `retryAfter` to at least 5s by default.
const retryAfter = Math.max(
~~error.response.headers["retry-after"],
state.minimumSecondaryRateRetryAfter
);
const wantRetry = await emitter.trigger(
"secondary-limit",
retryAfter,
options,
octokit,
retryCount
);
return { wantRetry, retryAfter };
}
if (
error.response.headers != null &&
error.response.headers["x-ratelimit-remaining"] === "0"
) {
// The user has used all their allowed calls for the current time period (REST and GraphQL)
// https://docs.github.com/en/rest/reference/rate-limit (REST)
// https://docs.github.com/en/graphql/overview/resource-limitations#rate-limit (GraphQL)
const rateLimitReset = new Date(
~~error.response.headers["x-ratelimit-reset"] * 1000
).getTime();
const retryAfter = Math.max(
Math.ceil((rateLimitReset - Date.now()) / 1000),
0
);
const wantRetry = await emitter.trigger(
"rate-limit",
retryAfter,
options,
octokit,
retryCount
);
return { wantRetry, retryAfter };
}
return {};
})();
if (wantRetry) {
request.retryCount++;
return retryAfter * state.retryAfterBaseValue;
}
});
octokit.hook.wrap("request", wrapRequest.bind(null, state));
return {};
}
throttling.VERSION = VERSION;
throttling.triggersNotification = triggersNotification;