Skip to content

Commit 567c1cf

Browse files
committed
chore: add HTTPAdapter to request session
1 parent 98f0c17 commit 567c1cf

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Added
2+
3+
- A HTTPAdapter with wider parameters has been setup to better address scanning multiple files at the same time.

ggshield/core/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pygitguardian import GGClient, GGClientCallbacks
88
from pygitguardian.models import APITokensResponse, Detail, TokenScope
99
from requests import Session
10+
from requests.adapters import HTTPAdapter
1011

1112
from . import ui
1213
from .config import Config
@@ -101,6 +102,13 @@ def create_session(allow_self_signed: bool = False) -> Session:
101102
)
102103
urllib3.disable_warnings()
103104
session.verify = False
105+
# Mount HTTPAdapter with larger pool sizes for better concurrency
106+
adapter = HTTPAdapter(
107+
pool_connections=20, # default 10
108+
pool_maxsize=100, # default 10
109+
)
110+
session.mount("http://", adapter)
111+
session.mount("https://", adapter)
104112
return session
105113

106114

tests/unit/core/test_client.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
from pygitguardian import GGClient
1010
from pygitguardian.models import APITokensResponse, Detail, TokenScope
1111

12-
from ggshield.core.client import check_client_api_key, create_client_from_config
12+
from ggshield.core.client import (
13+
check_client_api_key,
14+
create_client_from_config,
15+
create_session,
16+
)
1317
from ggshield.core.config import Config
1418
from ggshield.core.errors import (
1519
APIKeyCheckError,
@@ -239,3 +243,56 @@ def test_retrieve_client_unknown_custom_dashboard_url(isolated_fs: FakeFilesyste
239243
config = Config()
240244
config.cmdline_instance_name = "https://example.com"
241245
create_client_from_config(config)
246+
247+
248+
def test_create_session_mounts_http_adapter():
249+
"""
250+
GIVEN create_session is called
251+
WHEN the session is created
252+
THEN HTTPAdapter is mounted for both http:// and https://
253+
"""
254+
session = create_session()
255+
256+
# Verify adapters are mounted
257+
assert "http://" in session.adapters
258+
assert "https://" in session.adapters
259+
260+
from requests.adapters import HTTPAdapter
261+
262+
assert isinstance(session.get_adapter("http://example.com"), HTTPAdapter)
263+
assert isinstance(session.get_adapter("https://example.com"), HTTPAdapter)
264+
265+
266+
def test_create_session_pool_configuration():
267+
"""
268+
GIVEN create_session is called
269+
WHEN the session is created
270+
THEN the HTTPAdapter has the correct pool configuration
271+
"""
272+
session = create_session()
273+
274+
adapter = session.get_adapter("https://example.com")
275+
276+
# Verify pool configuration by checking the init parameters
277+
assert getattr(adapter, "_pool_connections", None) == 20
278+
assert getattr(adapter, "_pool_maxsize", None) == 100
279+
280+
281+
@pytest.mark.parametrize("allow_self_signed", [True, False])
282+
def test_create_session_with_self_signed_option(allow_self_signed: bool):
283+
"""
284+
GIVEN create_session is called with allow_self_signed parameter
285+
WHEN the session is created
286+
THEN HTTPAdapter is mounted regardless of allow_self_signed value
287+
AND verify is set correctly
288+
"""
289+
session = create_session(allow_self_signed=allow_self_signed)
290+
291+
# Verify adapters are mounted
292+
assert "https://" in session.adapters
293+
294+
# Verify SSL verification setting
295+
if allow_self_signed:
296+
assert session.verify is False
297+
else:
298+
assert session.verify is True

0 commit comments

Comments
 (0)