Skip to content

Commit 72303d6

Browse files
committed
Cleanup
1 parent b12abc5 commit 72303d6

File tree

2 files changed

+46
-94
lines changed

2 files changed

+46
-94
lines changed

pymongo/asynchronous/encryption.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,6 @@ def __init__(
142142
self.opts = opts
143143
self._spawned = False
144144

145-
async def _async_kms_request(
146-
self,
147-
kms_context: MongoCryptKmsContext,
148-
host: str,
149-
port: Optional[int],
150-
opts: PoolOptions,
151-
message: bytes,
152-
) -> None:
153-
from pymongo.network_layer import async_receive_data_socket # type: ignore[attr-defined]
154-
155-
try:
156-
conn = await _configured_socket((host, port), opts)
157-
try:
158-
await async_sendall(conn, message)
159-
while kms_context.bytes_needed > 0:
160-
# CSOT: update timeout.
161-
conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0))
162-
data = await async_receive_data_socket(conn, kms_context.bytes_needed)
163-
kms_context.feed(data)
164-
except OSError as err:
165-
raise OSError("KMS connection closed") from err
166-
except BLOCKING_IO_ERRORS:
167-
raise socket.timeout("timed out") from None
168-
finally:
169-
conn.close()
170-
except (PyMongoError, MongoCryptError):
171-
raise # Propagate pymongo errors directly.
172-
except Exception as error:
173-
# Wrap I/O errors in PyMongo exceptions.
174-
_raise_connection_failure((host, port), error)
175-
176145
async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
177146
"""Complete a KMS request.
178147
@@ -205,23 +174,30 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
205174
)
206175
host, port = parse_host(endpoint, _HTTPS_PORT)
207176
try:
208-
if _IS_SYNC:
209-
conn = await _configured_socket((host, port), opts)
210-
try:
211-
await async_sendall(conn, message)
212-
while kms_context.bytes_needed > 0:
213-
# CSOT: update timeout.
214-
conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0))
177+
conn = await _configured_socket((host, port), opts)
178+
try:
179+
await async_sendall(conn, message)
180+
while kms_context.bytes_needed > 0:
181+
# CSOT: update timeout.
182+
conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0))
183+
if _IS_SYNC:
215184
data = conn.recv(kms_context.bytes_needed)
216-
if not data:
217-
raise OSError("KMS connection closed")
218-
kms_context.feed(data)
219-
except BLOCKING_IO_ERRORS:
220-
raise socket.timeout("timed out") from None
221-
finally:
222-
conn.close()
223-
else:
224-
await self._async_kms_request(kms_context, host, port, opts, message)
185+
else:
186+
from pymongo.network_layer import (
187+
async_receive_data_socket, # type: ignore[attr-defined]
188+
)
189+
190+
data = await async_receive_data_socket(conn, kms_context.bytes_needed)
191+
if not data:
192+
raise OSError("KMS connection closed")
193+
kms_context.feed(data)
194+
# Async raises an OSError instead of returning empty bytes
195+
except OSError as err:
196+
raise OSError("KMS connection closed") from err
197+
except BLOCKING_IO_ERRORS:
198+
raise socket.timeout("timed out") from None
199+
finally:
200+
conn.close()
225201
except (PyMongoError, MongoCryptError):
226202
raise # Propagate pymongo errors directly.
227203
except Exception as error:

pymongo/synchronous/encryption.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,6 @@ def __init__(
142142
self.opts = opts
143143
self._spawned = False
144144

145-
def _async_kms_request(
146-
self,
147-
kms_context: MongoCryptKmsContext,
148-
host: str,
149-
port: Optional[int],
150-
opts: PoolOptions,
151-
message: bytes,
152-
) -> None:
153-
from pymongo.network_layer import receive_data_socket # type: ignore[attr-defined]
154-
155-
try:
156-
conn = _configured_socket((host, port), opts)
157-
try:
158-
sendall(conn, message)
159-
while kms_context.bytes_needed > 0:
160-
# CSOT: update timeout.
161-
conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0))
162-
data = receive_data_socket(conn, kms_context.bytes_needed)
163-
kms_context.feed(data)
164-
except OSError as err:
165-
raise OSError("KMS connection closed") from err
166-
except BLOCKING_IO_ERRORS:
167-
raise socket.timeout("timed out") from None
168-
finally:
169-
conn.close()
170-
except (PyMongoError, MongoCryptError):
171-
raise # Propagate pymongo errors directly.
172-
except Exception as error:
173-
# Wrap I/O errors in PyMongo exceptions.
174-
_raise_connection_failure((host, port), error)
175-
176145
def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
177146
"""Complete a KMS request.
178147
@@ -205,23 +174,30 @@ def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
205174
)
206175
host, port = parse_host(endpoint, _HTTPS_PORT)
207176
try:
208-
if _IS_SYNC:
209-
conn = _configured_socket((host, port), opts)
210-
try:
211-
sendall(conn, message)
212-
while kms_context.bytes_needed > 0:
213-
# CSOT: update timeout.
214-
conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0))
177+
conn = _configured_socket((host, port), opts)
178+
try:
179+
sendall(conn, message)
180+
while kms_context.bytes_needed > 0:
181+
# CSOT: update timeout.
182+
conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0))
183+
if _IS_SYNC:
215184
data = conn.recv(kms_context.bytes_needed)
216-
if not data:
217-
raise OSError("KMS connection closed")
218-
kms_context.feed(data)
219-
except BLOCKING_IO_ERRORS:
220-
raise socket.timeout("timed out") from None
221-
finally:
222-
conn.close()
223-
else:
224-
self._async_kms_request(kms_context, host, port, opts, message)
185+
else:
186+
from pymongo.network_layer import (
187+
receive_data_socket, # type: ignore[attr-defined]
188+
)
189+
190+
data = receive_data_socket(conn, kms_context.bytes_needed)
191+
if not data:
192+
raise OSError("KMS connection closed")
193+
kms_context.feed(data)
194+
# Async raises an OSError instead of returning empty bytes
195+
except OSError as err:
196+
raise OSError("KMS connection closed") from err
197+
except BLOCKING_IO_ERRORS:
198+
raise socket.timeout("timed out") from None
199+
finally:
200+
conn.close()
225201
except (PyMongoError, MongoCryptError):
226202
raise # Propagate pymongo errors directly.
227203
except Exception as error:

0 commit comments

Comments
 (0)