Skip to content

Commit

Permalink
[DO NOT MERGE] Remove unusable connection if connection Ping() failed (
Browse files Browse the repository at this point in the history
…#247)

* rRemove unusable connection if connectino Ping() failed

* Fix typo
  • Loading branch information
Aiee authored Dec 9, 2022
1 parent 2a602eb commit 2406793
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, 'v*' ]
schedule:
- cron: '0 6 * * *'

Expand Down
11 changes: 5 additions & 6 deletions nebula2/gclient/net/ConnectionPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2406793

Please sign in to comment.