Skip to content

Commit

Permalink
Add request ms to native request logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Oct 17, 2024
1 parent 7d191f9 commit 6043b3c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions plugins/_lib/native/request/requestStream.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const setDefaultUserAgent = async (userAgent: string) => (defaultUserAgen
const rateLimitSema = new Semaphore(1);
export type ExtendedRequestOptions = RequestOptions & { body?: string; rateLimit?: number };
export const requestStream = async (url: string, options: ExtendedRequestOptions = {}): Promise<IncomingMessage> => {
const start = Date.now();
options.headers ??= {};
options.headers["user-agent"] = defaultUserAgent;
options.rateLimit ??= 0;
Expand All @@ -27,14 +28,14 @@ export const requestStream = async (url: string, options: ExtendedRequestOptions
if (res.statusCode === 429 || res.statusCode === 503) {
const retryAfter = parseInt(res.headers["retry-after"] ?? "1", 10);
options.rateLimit!++;
libTrace.debug(`[${res.statusCode}${statusMsg}] (${req.method})`, `[Attempt ${options.rateLimit}, Retry in ${retryAfter}s]`, url);
libTrace.debug(`[${res.statusCode}${statusMsg}] (${req.method} - ${Date.now() - start}ms)`, `[Attempt ${options.rateLimit}, Retry in ${retryAfter}s]`, url);
return setTimeout(() => {
release?.();
requestStream(url, options).then(resolve, reject);
}, retryAfter);
}
if (options.rateLimit! > 0) libTrace.debug(`[${res.statusCode}${statusMsg}] (${req.method})`, `[After ${options.rateLimit} attempts]`, url);
else libTrace.debug(`[${res.statusCode}${statusMsg}] (${req.method})`, url);
if (options.rateLimit! > 0) libTrace.debug(`[${res.statusCode}${statusMsg}] (${req.method} - ${Date.now() - start}ms)`, `[After ${options.rateLimit} attempts]`, url);
else libTrace.debug(`[${res.statusCode}${statusMsg}] (${req.method} - ${Date.now() - start}ms)`, url);
resolve(res);
});
req.on("error", reject);
Expand Down

0 comments on commit 6043b3c

Please sign in to comment.