From 2406793343aa54e0f560f4642cefd99482051d8b Mon Sep 17 00:00:00 2001 From: Yichen Wang <18348405+Aiee@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:57:36 +0800 Subject: [PATCH] [DO NOT MERGE] Remove unusable connection if connection Ping() failed (#247) * rRemove unusable connection if connectino Ping() failed * Fix typo --- .github/workflows/run_test.yaml | 2 +- nebula2/gclient/net/ConnectionPool.py | 11 +++++------ tests/test_pool.py | 8 ++++---- tests/test_session.py | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.github/workflows/run_test.yaml b/.github/workflows/run_test.yaml index 6757ae2d..71cc8c10 100644 --- a/.github/workflows/run_test.yaml +++ b/.github/workflows/run_test.yaml @@ -4,7 +4,7 @@ on: push: branches: [ master ] pull_request: - branches: [ master ] + branches: [ master, 'v*' ] schedule: - cron: '0 6 * * *' diff --git a/nebula2/gclient/net/ConnectionPool.py b/nebula2/gclient/net/ConnectionPool.py index 07fc549b..9379eb0c 100644 --- a/nebula2/gclient/net/ConnectionPool.py +++ b/nebula2/gclient/net/ConnectionPool.py @@ -149,6 +149,7 @@ def get_connection(self): try: ok_num = self.get_ok_servers_num() if ok_num == 0: + logging.error('No available server') return None max_con_per_address = int(self._configs.max_connection_pool_size / ok_num) try_count = 0 @@ -162,15 +163,13 @@ def get_connection(self): connection.is_used = True logging.info('Get connection to {}'.format(addr)) return connection + # remove unusable connection + self._connections[addr].remove(connection) if len(self._connections[addr]) < max_con_per_address: connection = Connection() - if self._ssl_configs is None: - connection.open( - addr[0], addr[1], self._configs.timeout) - else: - connection.open_SSL( - addr[0], addr[1], self._configs.timeout, self._ssl_configs) + connection.open_SSL( + addr[0], addr[1], self._configs.timeout, self._ssl_configs) connection.is_used = True self._connections[addr].append(connection) logging.info('Get connection to {}'.format(addr)) diff --git a/tests/test_pool.py b/tests/test_pool.py index 9a15352b..a45ed701 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -42,7 +42,7 @@ def setup_class(self): self.configs.interval_check = 2 self.pool = ConnectionPool() assert self.pool.init(self.addresses, self.configs) - assert self.pool.connnects() == 2 + assert self.pool.connects() == 2 def test_right_hostname(self): pool = ConnectionPool() @@ -108,7 +108,7 @@ def test_get_session(self): session.release() assert self.pool.in_used_connects() == 0 - assert self.pool.connnects() == 4 + assert self.pool.connects() == 4 # test get session after release for num in range(0, self.configs.max_connection_pool_size - 1): @@ -118,10 +118,10 @@ def test_get_session(self): sessions.append(session) assert self.pool.in_used_connects() == 3 - assert self.pool.connnects() == 4 + assert self.pool.connects() == 4 # test the idle connection delete time.sleep(5) - assert self.pool.connnects() == 3 + assert self.pool.connects() == 3 def test_stop_close(self): session = self.pool.get_session('root', 'nebula') diff --git a/tests/test_session.py b/tests/test_session.py index cded39ca..6796f08b 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -31,7 +31,7 @@ def setup_class(self): ('127.0.0.1', 9670), ('127.0.0.1', 9671)], self.configs) - assert self.pool.connnects() == 0 + assert self.pool.connects() == 0 assert self.pool.in_used_connects() == 0 def test_1_release_by_del(self):