Skip to content

Commit 41adca1

Browse files
Refactor: perform calculation of whether a connection is healthy in one
place.
1 parent 2701ab2 commit 41adca1

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/oracledb/impl/thin/connection.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ cdef class BaseThinConnImpl(BaseConnImpl):
269269
return self._internal_name
270270

271271
def get_is_healthy(self):
272-
return self._protocol._transport is not None \
273-
and self._protocol._read_buf._pending_error_num == 0
272+
return self._protocol._get_is_healthy()
274273

275274
def get_ltxid(self):
276275
return self._ltxid or b''

src/oracledb/impl/thin/protocol.pyx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ cdef class BaseProtocol:
8888
self._write_buf._transport = None
8989
transport.disconnect()
9090

91+
cdef bint _get_is_healthy(self):
92+
"""
93+
Returns a boolean indicating if the connection is considered healthy.
94+
"""
95+
# if a read failed on the socket earlier, clear the socket
96+
if self._read_buf._transport is None \
97+
or self._read_buf._transport._transport is None:
98+
self._transport = None
99+
return self._transport is not None \
100+
and self._read_buf._pending_error_num == 0
101+
91102
cdef int _post_connect(self, BaseThinConnImpl conn_impl,
92103
AuthMessage auth_message) except -1:
93104
""""
@@ -155,15 +166,9 @@ cdef class Protocol(BaseProtocol):
155166

156167
with self._request_lock:
157168

158-
# if a read failed on the socket earlier, clear the socket
159-
if self._read_buf._transport is None \
160-
or self._read_buf._transport._transport is None:
161-
self._transport = None
162-
163169
# if the session was marked as needing to be closed, force it
164170
# closed immediately (unless it was already closed)
165-
if self._read_buf._pending_error_num != 0 \
166-
and self._transport is not None:
171+
if not self._get_is_healthy() and self._transport is not None:
167172
self._force_close()
168173

169174
# rollback any open transaction and release the DRCP session, if
@@ -529,14 +534,9 @@ cdef class BaseAsyncProtocol(BaseProtocol):
529534

530535
async with self._request_lock:
531536

532-
# if a read failed on the socket earlier, clear the socket
533-
if self._read_buf._transport is None:
534-
self._transport = None
535-
536537
# if the session was marked as needing to be closed, force it
537538
# closed immediately (unless it was already closed)
538-
if self._read_buf._pending_error_num != 0 \
539-
and self._transport is not None:
539+
if not self._get_is_healthy() and self._transport is not None:
540540
self._force_close()
541541

542542
# rollback any open transaction and release the DRCP session, if

0 commit comments

Comments
 (0)