对连接进行检查,查看是否超时或者已经达到最大的超时次数 :param host: :return:
(self, host)
| 248 | time.sleep(TIMEOUT_CHECK_INTERVAL - time_delta) |
| 249 | |
| 250 | def _check_conn(self, host): |
| 251 | """ |
| 252 | 对连接进行检查,查看是否超时或者已经达到最大的超时次数 |
| 253 | :param host: |
| 254 | :return: |
| 255 | """ |
| 256 | conn = self._connection_pool[host] |
| 257 | # 如果未达到最大的超时时间,则不进行任何操作 |
| 258 | if time.time() - conn.last_active <= TIMEOUT_IDLE: |
| 259 | return |
| 260 | |
| 261 | # 达到最大的超时次数,对此连接进行重连 |
| 262 | if self.client_heartbeats[host] >= TIMEOUT_MAX_TIMES: |
| 263 | self._new_connection(host) |
| 264 | self.client_heartbeats[host] = 0 |
| 265 | conn.close() # 关闭旧的连接 |
| 266 | logger.debug('{} timeout and reconnected by client.'.format(host)) |
| 267 | |
| 268 | # 未达到最大的超时次数,超时次数+1且发送心跳包 |
| 269 | else: |
| 270 | self.client_heartbeats[host] += 1 |
| 271 | invoke_id = get_invoke_id() |
| 272 | req = CLI_HEARTBEAT_REQ_HEAD + list(bytearray(pack('!q', invoke_id))) + CLI_HEARTBEAT_TAIL |
| 273 | conn.write(bytearray(req)) |
| 274 | logger.debug('Send ❤ request for invoke_id {}, host={}'.format(invoke_id, host)) |
| 275 | |
| 276 | |
| 277 | class SelectConnectionPool(BaseConnectionPool): |
no test coverage detected