From 48fb7a00951f91bdc707e5dd2d196e5bccb752c3 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 18 六月 2025 18:41:30 +0800
Subject: [PATCH] 异常保护

---
 outside_api_command_manager.py |   55 +++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/outside_api_command_manager.py b/outside_api_command_manager.py
index 348691d..93b140f 100644
--- a/outside_api_command_manager.py
+++ b/outside_api_command_manager.py
@@ -11,13 +11,14 @@
 # 蹇冭烦淇℃伅
 from huaxin_client import socket_util
 from huaxin_client.client_network import SendResponseSkManager
-from log_module.log import logger_debug, logger_system
+from log_module.log import logger_system, logger_request_api
 from utils import middle_api_protocol, tool
 
 MSG_TYPE_HEART = "heart"
 # 鍛戒护淇℃伅
 MSG_TYPE_CMD = "cmd"
 
+CLIENT_TYPE_COMMON = "common"
 CLIENT_TYPE_TRADE = "trade"
 
 # 蹇冭烦鏃堕棿闂撮殧
@@ -40,6 +41,8 @@
 CODE_LIST_BLACK = "black"
 CODE_LIST_WANT = "want"
 CODE_LIST_PAUSE_BUY = "pause_buy"
+CODE_LIST_MUST_BUY = "must_buy"
+CODE_LIST_GREEN = "green"
 
 # 绫诲瀷
 API_TYPE_TRADE = "trade"  # 浜ゆ槗
@@ -61,6 +64,7 @@
 API_TYPE_SAVE_RUNNING_DATA = "save_running_data"  # 淇濆瓨杩愯鏃舵暟鎹�
 API_TYPE_GET_CODE_POSITION_INFO = "get_code_position_info"  # 鑾峰彇浠g爜鎸佷粨淇℃伅
 API_TYPE_COMMON_REQUEST = "common_request"  # 閫氱敤璇锋眰
+
 
 class ActionCallback(object):
     # 浜ゆ槗
@@ -127,7 +131,11 @@
 # 浜ゆ槗鎸囦护绠$悊
 # 浜ゆ槗鎸囦护绠$悊
 class ApiCommandManager:
+    common_client_dict = {}
     trade_client_dict = {}
+    trade_client_count = 0
+    common_client_count = 0
+
     _instance = None
 
     def __new__(cls, *args, **kwargs):
@@ -155,16 +163,22 @@
         # 鍙戦�佸績璺�
         cls.__heartbeats_thread(type, key, sk)
         cls.__listen_command_thread(type, key, sk)
-        print("create_and_run_client success", type, key)
+        # print("create_and_run_client success", type, key)
+        logger_request_api.info(f"鍒涘缓鏈湴socket璇锋眰瀹㈡埛绔細{type}")
         return key, sk
 
     @classmethod
-    def init(cls, addr, port, trade_action_callback, trade_client_count=20):
-        cls.trade_client_dict = {}
+    def init(cls, addr, port, trade_action_callback, common_client_count=20, trade_client_count=20):
+        cls.common_client_dict.clear()
+        cls.common_client_count = common_client_count
+        cls.trade_client_dict.clear()
         cls.trade_client_count = trade_client_count
         cls.action_callback = trade_action_callback
         cls.ip_port = (addr, port)
 
+        for i in range(common_client_count):
+            result = cls.__create_and_run_client(CLIENT_TYPE_COMMON, i)
+            cls.common_client_dict[result[0]] = result[1]
         for i in range(trade_client_count):
             result = cls.__create_and_run_client(CLIENT_TYPE_TRADE, i)
             cls.trade_client_dict[result[0]] = result[1]
@@ -176,8 +190,9 @@
             try:
                 result = socket_util.recv_data(sk)[0]
                 if result:
+                    start_time = time.time()
                     try:
-                        print("鎺ユ敹鏁版嵁", _type, result)
+                        # print("鎺ユ敹鏁版嵁", _type, result)
                         result_json = json.loads(result)
                         if result_json["type"] == MSG_TYPE_HEART:
                             # 杩斿洖鍐呭
@@ -186,15 +201,14 @@
 
                         data = result_json["data"]
                         content_type = data["type"]
-                        print("鎺ユ敹鍐呭", data)
+                        # print("鎺ユ敹鍐呭", data)
                         request_id = result_json.get('request_id')
                         if not socket_util.is_client_params_sign_right(result_json):
-                            print("绛惧悕閿欒")
+                            # print("绛惧悕閿欒")
                             # 绛惧悕鍑洪敊
                             SendResponseSkManager.send_error_response(_type, request_id, client_id,
                                                                       {"code": -1, "msg": "绛惧悕閿欒"})
                             continue
-
                         if content_type == API_TYPE_TRADE:
                             # 浜ゆ槗
                             cls.action_callback.OnTrade(client_id, request_id, data)
@@ -234,13 +248,13 @@
                             cls.action_callback.OnGetCodePositionInfo(client_id, request_id, data)
                         elif content_type == API_TYPE_COMMON_REQUEST:
                             cls.action_callback.OnCommonRequest(client_id, request_id, data)
-
-
-
                     except Exception as e:
                         logging.exception(e)
-                        pass
                     finally:
+                        use_time = int(time.time() - start_time)
+                        if use_time > 5:
+                            result_json = json.loads(result)
+                            logger_request_api.info(f"瓒呮椂5s浠ヤ笂锛歿result_json['data']['type']}")
                         # 鍙戦�佸搷搴�
                         sk.send(json.dumps({"type": "cmd_recieve"}).encode('utf-8'))
                 else:
@@ -248,10 +262,12 @@
 
             except Exception as e:
                 logging.exception(e)
-                if _type == CLIENT_TYPE_TRADE:
+                if _type == CLIENT_TYPE_COMMON:
+                    if client_id in cls.common_client_dict:
+                        cls.common_client_dict.pop(client_id)
+                elif _type == CLIENT_TYPE_TRADE:
                     if client_id in cls.trade_client_dict:
                         cls.trade_client_dict.pop(client_id)
-                        print("pop trade client", client_id)
                 try:
                     sk.close()
                 except:
@@ -266,7 +282,10 @@
                 sk.send(socket_util.load_header(json.dumps({"type": "heart", "client_id": client_id}).encode('utf-8')))
                 # print("蹇冭烦淇℃伅鍙戦�佹垚鍔�", client_id)
             except Exception as e:
-                if _type == CLIENT_TYPE_TRADE:
+                if _type == CLIENT_TYPE_COMMON:
+                    if client_id in cls.common_client_dict:
+                        cls.common_client_dict.pop(client_id)
+                elif _type == CLIENT_TYPE_TRADE:
                     if client_id in cls.trade_client_dict:
                         cls.trade_client_dict.pop(client_id)
                 try:
@@ -294,8 +313,12 @@
         logger_system.info(f"outside_api __maintain_client 绾跨▼ID:{tool.get_thread_id()}")
         while True:
             try:
+                if len(cls.common_client_dict) < cls.common_client_count:
+                    for i in range(cls.common_client_count - len(cls.common_client_dict)):
+                        result = cls.__create_and_run_client(CLIENT_TYPE_COMMON)
+                        cls.common_client_dict[result[0]] = result[1]
+
                 if len(cls.trade_client_dict) < cls.trade_client_count:
-                    print("__maintain_client", CLIENT_TYPE_TRADE, cls.trade_client_count - len(cls.trade_client_dict))
                     for i in range(cls.trade_client_count - len(cls.trade_client_dict)):
                         result = cls.__create_and_run_client(CLIENT_TYPE_TRADE)
                         cls.trade_client_dict[result[0]] = result[1]

--
Gitblit v1.8.0