Skip to content

Commit 9eeb9dc

Browse files
committed
adjust timeout
1 parent 27a4d96 commit 9eeb9dc

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/node-binance-api.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -413,23 +413,13 @@ export default class Binance {
413413
}
414414

415415
async proxyRequest(opt: any) {
416-
// const req = request(this.addProxy(opt), this.reqHandler(cb)).on('error', (err) => { cb(err, {}) });
417-
// family: opt.family,
418-
// timeout: opt.timeout,
419-
420416
const urlBody = new URLSearchParams(opt.form);
421417
const reqOptions: Dict = {
422418
method: opt.method,
423419
headers: opt.headers,
424-
// body: urlBody
425-
// body: (opt.form)
426420
};
427421
if (opt.method !== 'GET') {
428422
reqOptions.body = urlBody;
429-
} else {
430-
if (opt.qs) {
431-
// opt.url += '?' + this.makeQueryString(opt.qs);
432-
}
433423
}
434424
if (this.Options.verbose) {
435425
this.Options.log('HTTP Request:', opt.method, opt.url, reqOptions);
@@ -451,21 +441,36 @@ export default class Binance {
451441
opt.url = urlProxy + opt.url;
452442
}
453443

444+
// Apply timeout via AbortController
445+
const timeout = opt.timeout || this.Options.recvWindow || 30000;
446+
const controller = new AbortController();
447+
const timeoutId = setTimeout(() => controller.abort(), timeout);
448+
reqOptions.signal = controller.signal;
449+
454450
let fetchImplementation = fetch;
455451
// require node-fetch
456452
if (reqOptions.agent) {
457453
fetchImplementation = nodeFetch;
458454
}
459455

460-
const response = await fetchImplementation(opt.url, reqOptions);
456+
try {
457+
const response = await fetchImplementation(opt.url, reqOptions);
458+
clearTimeout(timeoutId);
461459

462-
await this.reqHandler(response);
463-
const json = await response.json();
460+
await this.reqHandler(response);
461+
const json = await response.json();
464462

465-
if (this.Options.verbose) {
466-
this.Options.log('HTTP Response:', json);
463+
if (this.Options.verbose) {
464+
this.Options.log('HTTP Response:', json);
465+
}
466+
return json;
467+
} catch (error) {
468+
clearTimeout(timeoutId);
469+
if (error.name === 'AbortError') {
470+
throw new Error(`Request timeout: ${opt.method} ${opt.url} (${timeout}ms)`);
471+
}
472+
throw error;
467473
}
468-
return json;
469474
}
470475

471476
reqObj(url: string, data: Dict = {}, method: HttpMethod = 'GET', key?: string) {

0 commit comments

Comments
 (0)