Skip to content

Commit

Permalink
Fix: Add axios abort signal (#3961)
Browse files Browse the repository at this point in the history
* Fix: Add axios abort signal

* Chore: Fix comment
  • Loading branch information
chakflying authored Nov 1, 2023
1 parent df832f1 commit fdfb572
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, MAX_INTERVAL_SECOND, MI
SQL_DATETIME_FORMAT
} = require("../../src/util");
const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, mqttAsync, setSetting, httpNtlm, radius, grpcQuery,
redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials,
redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, axiosAbortSignal
} = require("../util-server");
const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model");
Expand Down Expand Up @@ -456,6 +456,7 @@ class Monitor extends BeanModel {
validateStatus: (status) => {
return checkStatusCode(status, this.getAcceptedStatuscodes());
},
signal: axiosAbortSignal(this.timeout * 1000),
};

if (bodyValue) {
Expand Down
23 changes: 23 additions & 0 deletions server/util-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,26 @@ if (process.env.TEST_BACKEND) {
return module.exports.__test[functionName];
};
}

/**
* Generates an abort signal with the specified timeout.
* @param {number} timeoutMs - The timeout in milliseconds.
* @returns {AbortSignal | null} - The generated abort signal, or null if not supported.
*/
module.exports.axiosAbortSignal = (timeoutMs) => {
try {
return AbortSignal.timeout(timeoutMs);
} catch (_) {
// v16-: AbortSignal.timeout is not supported
try {
const abortController = new AbortController();

setTimeout(() => abortController.abort(), timeoutMs || 0);

return abortController.signal;
} catch (_) {
// v15-: AbortController is not supported
return null;
}
}
};

0 comments on commit fdfb572

Please sign in to comment.