Browse Source

将日志 与 机器人状态详情进行缓存,

hl 3 weeks ago
parent
commit
8bae93805b
3 changed files with 50 additions and 3 deletions
  1. 1 1
      src/Interval.js
  2. 1 1
      src/robot.js
  3. 48 1
      src/utils/http.js

+ 1 - 1
src/Interval.js

@@ -154,7 +154,7 @@ function cachePredictorState() {
                 const megabyteSize = byteSize / (1024 * 1024);
                 logger.info(`~~~缓存机器人状态详情${value.id}--占用大小:${megabyteSize.toFixed(6)} M`)
                 var accUrl = "http://127.0.0.1:" + port
-                http.request_get(`${accUrl}/predictor_state`, {...config.headers})
+                http.request_get_timeout(`${accUrl}/predictor_state`, {...config.headers})
                     .then((data) => {
                         if(data.length < 50){
                             logger.info(`可能出错的data~~: ${data}`)

+ 1 - 1
src/robot.js

@@ -513,7 +513,7 @@ function robotAmount(app) {
     //拿到策略余额
     try {
         var config = getRustConfig()
-        http.request_get(`${accUrl}/account`, {...config.headers})
+        http.request_get_timeout(`${accUrl}/account`, {...config.headers})
             .then((data) => {
                 var d = JSON.parse(data)
                 app.nowBalance = d.now_balance

+ 48 - 1
src/utils/http.js

@@ -14,7 +14,6 @@ function getHeaders(headers, body) {
     return headers
 }
 
-
 function request_get(url, headers) {
     return new Promise((resolve, reject) => {
         var callback = function (res) {
@@ -47,6 +46,53 @@ function request_get(url, headers) {
         } else {
             httpsReq = https.request(url, options, callback).on('error', errorback);
         }
+
+        // 不要忘了关闭请求,以防止内存泄漏
+        httpsReq.end();
+    });
+}
+
+function request_get_timeout(url, headers) {
+    return new Promise((resolve, reject) => {
+        var callback = function (res) {
+            let data = '';
+
+            // 监听数据事件来收集响应数据
+            res.on('data', (chunk) => {
+                data += chunk;
+            });
+
+            // 监听结束事件来处理响应结束
+            res.on('end', () => {
+                clearTimeout(timeout); // 清除超时定时器
+                // logger.info(`响应体: ${data}`);
+                resolve(data);
+            });
+        }
+        var errorback = function errorback(e) {
+            clearTimeout(timeout); // 清除超时定时器
+            // logger.error(`请求遇到问题: ${e.message}`);
+            reject(e);
+        }
+        //  请求的选项
+        const options = {
+            method: 'GET',
+            headers: getHeaders(headers, "")
+        };
+
+        let httpsReq = null
+        if (!url.includes("https")) {
+            httpsReq = http.request(url, options, callback).on('error', errorback);
+        } else {
+            httpsReq = https.request(url, options, callback).on('error', errorback);
+        }
+
+        // 设置超时限制为2秒
+        const timeout = setTimeout(() => {
+            httpsReq.abort(); // 中止请求
+            reject(new Error('请求超时')); // 拒绝 Promise
+        }, 2000); // 2000 毫秒即2秒
+
         // 不要忘了关闭请求,以防止内存泄漏
         httpsReq.end();
     });
@@ -143,6 +189,7 @@ function dowFile(url, fileName, dowPath, headers) {
 }
 
 module.exports = {
+    request_get_timeout,
     request_get,
     request_post,
     dowFile,