From e3f35a461c4bb140757500133af9374fc6430f44 Mon Sep 17 00:00:00 2001 From: BADtochka Date: Sat, 6 Jun 2026 17:53:16 +0300 Subject: [PATCH] fix(payments): restore default provider connections --- .../bot/payment_providers/shared/http_client.py | 17 +++-------------- tests/test_payment_http_client.py | 6 ++---- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/backend/bot/payment_providers/shared/http_client.py b/backend/bot/payment_providers/shared/http_client.py index 6d38239..73787e2 100644 --- a/backend/bot/payment_providers/shared/http_client.py +++ b/backend/bot/payment_providers/shared/http_client.py @@ -5,15 +5,10 @@ import json import logging from typing import Any, Callable, Dict, Mapping, Optional, Tuple -from aiohttp import ClientError, ClientSession, ClientTimeout, TCPConnector, TraceConfig +from aiohttp import ClientError, ClientSession, ClientTimeout, TraceConfig SuccessCheck = Callable[[int, Any], bool] _TRANSPORT_ATTEMPTS = 2 -_PAYMENT_REQUEST_USER_AGENT = ( - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " - "AppleWebKit/537.36 (KHTML, like Gecko) " - "Chrome/125.0.0.0 Safari/537.36" -) def http_ok(status: int, _body: Any) -> bool: @@ -120,27 +115,21 @@ class HttpClientMixin: ``__init__`` and inherits ``_get_session`` / ``close``. The session is created on first use and recreated transparently if it was closed. - Payment provider calls are infrequent but user-facing, so the default - connector does not reuse TCP connections. This avoids intermittent hangs - on stale keep-alive sockets after long idle periods. + Provider API calls are traced so callers can retry transport failures only + when aiohttp has not sent request headers yet. """ _timeout: ClientTimeout _session: Optional[ClientSession] - _connector_force_close: bool def _init_http_client(self, *, total_timeout: float = 20.0) -> None: self._timeout = ClientTimeout(total=total_timeout) self._session = None - self._connector_force_close = True async def _get_session(self) -> ClientSession: if self._session is None or self._session.closed: - connector = TCPConnector(force_close=self._connector_force_close) self._session = ClientSession( timeout=self._timeout, - connector=connector, - headers={"User-Agent": _PAYMENT_REQUEST_USER_AGENT}, trace_configs=[_payment_trace_config()], ) return self._session diff --git a/tests/test_payment_http_client.py b/tests/test_payment_http_client.py index a46602a..67e6b9a 100644 --- a/tests/test_payment_http_client.py +++ b/tests/test_payment_http_client.py @@ -2,7 +2,6 @@ import unittest from bot.payment_providers.shared.http_client import ( HttpClientMixin, - _PAYMENT_REQUEST_USER_AGENT, _should_retry_transport_error, ) @@ -13,13 +12,12 @@ class _DummyHttpClient(HttpClientMixin): class PaymentHttpClientTests(unittest.IsolatedAsyncioTestCase): - async def test_http_client_does_not_reuse_provider_tcp_connections(self): + async def test_http_client_tracks_sent_headers_for_safe_retries(self): client = _DummyHttpClient() try: session = await client._get_session() - self.assertTrue(session.connector.force_close) + self.assertFalse(session.connector.force_close) self.assertTrue(session.trace_configs) - self.assertEqual(session.headers["User-Agent"], _PAYMENT_REQUEST_USER_AGENT) finally: await client.close()