做了一些简单的优化
diff --git a/dubbo/codec/encoder.py b/dubbo/codec/encoder.py
index 4e21c3a..e5ed8ba 100644
--- a/dubbo/codec/encoder.py
+++ b/dubbo/codec/encoder.py
@@ -349,11 +349,7 @@
:param body:
:return:
"""
- request_body_length = num_2_byte_list(len(body))
- # 用4个字节表示请求body的长度
- while len(request_body_length) < 4:
- request_body_length = [0] + request_body_length
- return request_body_length
+ return list(bytearray(struct.pack('!i', len(body))))
if __name__ == '__main__':
diff --git a/dubbo/common/constants.py b/dubbo/common/constants.py
index 4f9f5fa..5e86174 100644
--- a/dubbo/common/constants.py
+++ b/dubbo/common/constants.py
@@ -61,3 +61,10 @@
DUBBO_ZK_PROVIDERS = '/dubbo/{}/providers'
DUBBO_ZK_CONSUMERS = '/dubbo/{}/consumers'
DUBBO_ZK_CONFIGURATORS = '/dubbo/{}/configurators'
+
+# 客户端检测与远程主机的连接是否超时的间隔
+TIMEOUT_CHECK_INTERVAL = 0.03 # 30ms
+# 连接最长允许的空闲时间
+TIMEOUT_IDLE = 60
+# 连接允许的最多的超时次数
+TIMEOUT_MAX_TIMES = 3
diff --git a/dubbo/common/util.py b/dubbo/common/util.py
index 2ea4873..8cb3195 100644
--- a/dubbo/common/util.py
+++ b/dubbo/common/util.py
@@ -71,15 +71,6 @@
return os.getpid()
-def get_heartbeat_id():
- global heartbeat_id
- heartbeat_id += 1
- heartbeat_id_byte = num_2_byte_list(heartbeat_id)
- while len(heartbeat_id_byte) < 8:
- heartbeat_id_byte = [0] + heartbeat_id_byte
- return heartbeat_id_byte
-
-
def is_linux():
if platform == "linux" or platform == "linux2":
return True
diff --git a/dubbo/connection/connections.py b/dubbo/connection/connections.py
index c237725..158064f 100644
--- a/dubbo/connection/connections.py
+++ b/dubbo/connection/connections.py
@@ -8,7 +8,8 @@
from dubbo.codec.encoder import Request
from dubbo.codec.decoder import Response, get_body_length
-from dubbo.common.constants import CLI_HEARTBEAT_RES_HEAD, CLI_HEARTBEAT_TAIL, CLI_HEARTBEAT_REQ_HEAD
+from dubbo.common.constants import CLI_HEARTBEAT_RES_HEAD, CLI_HEARTBEAT_TAIL, CLI_HEARTBEAT_REQ_HEAD, \
+ TIMEOUT_CHECK_INTERVAL, TIMEOUT_IDLE, TIMEOUT_MAX_TIMES
from dubbo.common.exceptions import DubboResponseException, DubboRequestTimeoutException
from dubbo.common.util import get_invoke_id
@@ -167,8 +168,8 @@
starting = time.time()
for host in self._connection_pool.keys():
conn = self._connection_pool[host]
- if time.time() - conn.last_active > 60:
- if self.client_heartbeats[host] >= 3:
+ if time.time() - conn.last_active > TIMEOUT_IDLE:
+ if self.client_heartbeats[host] >= TIMEOUT_MAX_TIMES:
self._delete_connection(conn)
conn.close() # 客户端主动关闭连接
logger.debug('{} closed by client'.format(host))
@@ -179,8 +180,8 @@
conn.write(bytearray(req))
ending = time.time()
time_delta = ending - starting
- if time_delta < 10:
- time.sleep(10 - time_delta)
+ if time_delta < TIMEOUT_CHECK_INTERVAL:
+ time.sleep(TIMEOUT_CHECK_INTERVAL - time_delta)
class EpollConnectionPool(BaseConnectionPool):