Skip to content

Commit 3323d34

Browse files
committed
chore: add HTTPAdapter to request session
1 parent 98f0c17 commit 3323d34

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-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: 66 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,64 @@ 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+
# Verify it's an HTTPAdapter (not the default)
261+
from requests.adapters import HTTPAdapter
262+
263+
assert isinstance(session.get_adapter("http://example.com"), HTTPAdapter)
264+
assert isinstance(session.get_adapter("https://example.com"), HTTPAdapter)
265+
266+
267+
def test_create_session_pool_configuration():
268+
"""
269+
GIVEN create_session is called
270+
WHEN the session is created
271+
THEN the HTTPAdapter has the correct pool configuration
272+
"""
273+
session = create_session()
274+
275+
adapter = session.get_adapter("https://example.com")
276+
from requests.adapters import HTTPAdapter
277+
278+
# Verify it's an HTTPAdapter instance
279+
assert isinstance(adapter, HTTPAdapter)
280+
281+
# Verify pool configuration by checking the init parameters
282+
# The adapter stores these as private attributes
283+
assert hasattr(adapter, "_pool_connections")
284+
assert hasattr(adapter, "_pool_maxsize")
285+
assert getattr(adapter, "_pool_connections", None) == 20
286+
assert getattr(adapter, "_pool_maxsize", None) == 100
287+
288+
289+
@pytest.mark.parametrize("allow_self_signed", [True, False])
290+
def test_create_session_with_self_signed_option(allow_self_signed: bool):
291+
"""
292+
GIVEN create_session is called with allow_self_signed parameter
293+
WHEN the session is created
294+
THEN HTTPAdapter is mounted regardless of allow_self_signed value
295+
AND verify is set correctly
296+
"""
297+
session = create_session(allow_self_signed=allow_self_signed)
298+
299+
# Verify adapters are mounted
300+
assert "https://" in session.adapters
301+
302+
# Verify SSL verification setting
303+
if allow_self_signed:
304+
assert session.verify is False
305+
else:
306+
assert session.verify is True

0 commit comments

Comments
 (0)