perf(impit): optimize client and connection reuse across Sessions - #2140
Open
Mantisus wants to merge 2 commits into
Open
perf(impit): optimize client and connection reuse across Sessions#2140Mantisus wants to merge 2 commits into
Mantisus wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors ImpitHttpClient to reuse underlying impit.AsyncClient instances across sessions (per-proxy), while taking over redirect handling to process redirects hop-by-hop so per-session cookie jars can be applied and updated at each hop.
Changes:
- Moved cookie extraction / cookie header construction into
SessionCookies, and integrated it intoImpitHttpClienton each redirect hop. - Implemented WHATWG Fetch–style redirect behavior (method/body/header adjustments + cross-origin header dropping) and added a
TooManyRedirectsError. - Updated dependency constraints/lockfile and added unit tests for cookies and redirect semantics.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| uv.lock | Bumps impit locked version to 0.13.2. |
| pyproject.toml | Raises minimum impit version to >=0.13.2. |
| src/crawlee/sessions/_cookies.py | Adds get_cookie_string() and extract_cookie_from_header() helpers on SessionCookies. |
| src/crawlee/http_clients/_impit.py | Adds hop-by-hop redirect handling, cross-origin/body-header logic, and client reuse per proxy. |
| src/crawlee/errors.py | Introduces TooManyRedirectsError for redirect limit enforcement. |
| tests/unit/sessions/test_cookies.py | Adds coverage for Set-Cookie parsing and Cookie header construction. |
| tests/unit/server.py | Adds test endpoints for redirect loops and method/body echoing. |
| tests/unit/http_clients/test_impit.py | Adds unit tests for session cookie isolation, redirect behavior, and redirect limits. |
Suppressed comments (1)
src/crawlee/http_clients/_impit.py:335
- After switching to a counted redirect loop, add an explicit guard before following the next hop so that
max_redirectstruly limits the number of redirects followed (otherwise an endless chain can still be followed one hop too far).
next_url = current_url.join(URL(location))
if next_url.scheme not in _HTTP_SCHEMES:
return response
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
impittoImpitHttpClient. Clients are now reused per proxy instead of being recreated for everySession, so a single client keeps its connection pool alive across all of them. The result is higher performance than that ofHttpxHttpClient.Testing