Skip to content

Commit 2c323c2

Browse files
committed
feat: replace requests with global HTTP client
Reuse HTTP connections to avoid number of connections in TIME_WAIT status. - Removed direct imports of `requests` in test files. - Added `http_client` module to provide a global HTTP client session. - Updated test files to use the global HTTP client session. - Modified `submit_api.py` to use the global HTTP client session.
1 parent a6bba6e commit 2c323c2

File tree

6 files changed

+29
-8
lines changed

6 files changed

+29
-8
lines changed

cardano_node_tests/tests/kes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import typing as tp
66

77
import pytest
8-
import requests
98
from cardano_clusterlib import clusterlib
109

1110
from cardano_node_tests.tests import issues
1211
from cardano_node_tests.utils import blockers
1312
from cardano_node_tests.utils import cluster_nodes
13+
from cardano_node_tests.utils import http_client
1414

1515
LOGGER = logging.getLogger(__name__)
1616

@@ -85,7 +85,9 @@ def check_kes_period_info_result( # noqa: C901
8585
cluster_nodes.get_instance_num()
8686
)
8787
prometheus_port = instance_ports.node_ports[pool_num].prometheus
88-
response = requests.get(f"http://localhost:{prometheus_port}/metrics", timeout=10)
88+
response = http_client.get_session().get(
89+
f"http://localhost:{prometheus_port}/metrics", timeout=10
90+
)
8991

9092
_prometheus_metrics_raw = [m.split() for m in response.text.strip().split("\n")]
9193
prometheus_metrics_raw = {m[0]: m[-1] for m in _prometheus_metrics_raw}

cardano_node_tests/tests/test_kes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import allure
1212
import pytest
13-
import requests
1413
from cardano_clusterlib import clusterlib
1514

1615
from cardano_node_tests.cluster_management import cluster_management
@@ -21,6 +20,7 @@
2120
from cardano_node_tests.utils import clusterlib_utils
2221
from cardano_node_tests.utils import configuration
2322
from cardano_node_tests.utils import helpers
23+
from cardano_node_tests.utils import http_client
2424
from cardano_node_tests.utils import locking
2525
from cardano_node_tests.utils import logfiles
2626
from cardano_node_tests.utils import temptools
@@ -86,7 +86,7 @@ def _save_metrics(pool_num: int, temp_template: str) -> None:
8686
)
8787
port = instance_ports.node_ports[pool_num].prometheus
8888

89-
response = requests.get(f"http://localhost:{port}/metrics", timeout=10)
89+
response = http_client.get_session().get(f"http://localhost:{port}/metrics", timeout=10)
9090
assert response, f"Request failed, status code {response.status_code}"
9191

9292
with open(f"{temp_template}_pool{pool_num}_metrics.txt", "w", encoding="utf-8") as fp_out:

cardano_node_tests/tests/test_metrics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from cardano_node_tests.tests import common
1212
from cardano_node_tests.utils import cluster_nodes
1313
from cardano_node_tests.utils import helpers
14+
from cardano_node_tests.utils import http_client
1415
from cardano_node_tests.utils import model_ekg
1516

1617
LOGGER = logging.getLogger(__name__)
@@ -27,13 +28,13 @@ def wait_epochs(cluster: clusterlib.ClusterLib):
2728

2829

2930
def get_prometheus_metrics(port: int) -> requests.Response:
30-
response = requests.get(f"http://localhost:{port}/metrics", timeout=10)
31+
response = http_client.get_session().get(f"http://localhost:{port}/metrics", timeout=10)
3132
assert response, f"Request failed, status code {response.status_code}"
3233
return response
3334

3435

3536
def get_ekg_metrics(port: int) -> requests.Response:
36-
response = requests.get(
37+
response = http_client.get_session().get(
3738
f"http://localhost:{port}/", headers={"Accept": "application/json"}, timeout=10
3839
)
3940
assert response, f"Request failed, status code {response.status_code}"

cardano_node_tests/tests/test_reconnect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from cardano_node_tests.utils import cluster_nodes
1717
from cardano_node_tests.utils import configuration
1818
from cardano_node_tests.utils import helpers
19+
from cardano_node_tests.utils import http_client
1920
from cardano_node_tests.utils.versions import VERSIONS
2021

2122
LOGGER = logging.getLogger(__name__)
@@ -119,7 +120,7 @@ def node_submit_tx(
119120
os.environ["CARDANO_NODE_SOCKET_PATH"] = orig_socket
120121

121122
def get_prometheus_metrics(self, port: int) -> requests.Response:
122-
response = requests.get(f"http://localhost:{port}/metrics", timeout=10)
123+
response = http_client.get_session().get(f"http://localhost:{port}/metrics", timeout=10)
123124
assert response, f"Request failed, status code {response.status_code}"
124125
return response
125126

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Global HTTP client."""
2+
3+
import requests
4+
5+
_session = None
6+
7+
8+
def get_session() -> requests.Session:
9+
"""Get a session object."""
10+
global _session # noqa: PLW0603
11+
12+
if _session is None:
13+
_session = requests.Session()
14+
return _session

cardano_node_tests/utils/submit_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cardano_clusterlib import clusterlib
1414

1515
from cardano_node_tests.utils import cluster_nodes
16+
from cardano_node_tests.utils import http_client
1617

1718
LOGGER = logging.getLogger(__name__)
1819

@@ -64,7 +65,9 @@ def post_cbor(cbor_file: clusterlib.FileType, url: str) -> requests.Response:
6465
if i > 0:
6566
LOGGER.warning("Resubmitting transaction to submit-api.")
6667
try:
67-
response = requests.post(url, headers=headers, data=cbor_binary, timeout=20)
68+
response = http_client.get_session().post(
69+
url, headers=headers, data=cbor_binary, timeout=20
70+
)
6871
except requests.exceptions.ReadTimeout:
6972
delay = True
7073
else:

0 commit comments

Comments
 (0)