-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent unnecessary should_bypass_proxies checks in rebuild_proxies #5894
Prevent unnecessary should_bypass_proxies checks in rebuild_proxies #5894
Conversation
requests/sessions.py
Outdated
@@ -633,7 +633,8 @@ def send(self, request, **kwargs): | |||
kwargs.setdefault('stream', self.stream) | |||
kwargs.setdefault('verify', self.verify) | |||
kwargs.setdefault('cert', self.cert) | |||
kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sigmavirus24 this change is needed because without it we are calling to re-build proxies from the environment even when proxies have been set/provided.
In one use test case I have setup - the time to "retrieve" a cached connection (using the cache control adapter) 10,000 times goes from 9.741 seconds and 25212240 function calls to 9902240 function calls & 6.169 seconds after applying this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this change ->
Ordered by: internal time
List reduced from 543 to 30 due to restriction <30>
ncalls tottime percall cumtime percall filename:lineno(function)
1410001 0.698 0.000 1.729 0.000 /usr/lib/python3.9/os.py:674(__getitem__)
1510005 0.598 0.000 3.054 0.000 /usr/lib/python3.9/_collections_abc.py:848(__iter__)
2760000 0.447 0.000 0.760 0.000 /usr/lib/python3.9/os.py:758(decode)
10000 0.406 0.000 3.401 0.000 /usr/lib/python3.9/urllib/request.py:2486(getproxies_environment)
1410001 0.403 0.000 0.640 0.000 /usr/lib/python3.9/os.py:754(encode)
3010069 0.335 0.000 0.335 0.000 {method 'decode' of 'bytes' objects}
1400000 0.321 0.000 0.689 0.000 /usr/lib/python3.9/os.py:697(__iter__)
1 0.308 0.308 0.308 0.308 {method 'do_handshake' of '_ssl._SSLSocket' objects}
2360120 0.258 0.000 0.380 0.000 {built-in method builtins.isinstance}
70004 0.179 0.000 0.536 0.000 /usr/lib/python3.9/_collections_abc.py:932(update)
1760141 0.165 0.000 0.165 0.000 {method 'lower' of 'str' objects}
1 0.164 0.164 0.164 0.164 {method 'read' of '_ssl._SSLSocket' objects}
1490043/1490041 0.163 0.000 0.163 0.000 {method 'encode' of 'str' objects}
1 0.147 0.147 0.147 0.147 {method 'connect' of '_socket.socket' objects}
...
After this change ->
Ordered by: internal time
List reduced from 535 to 30 due to restriction <30>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.326 0.326 0.326 0.326 {method 'do_handshake' of '_ssl._SSLSocket' objects}
1 0.191 0.191 0.191 0.191 {method 'read' of '_ssl._SSLSocket' objects}
70004 0.185 0.000 0.553 0.000 /usr/lib/python3.9/_collections_abc.py:932(update)
890120 0.170 0.000 0.298 0.000 {built-in method builtins.isinstance}
1 0.150 0.150 0.150 0.150 {method 'connect' of '_socket.socket' objects}
...
@dbaxa The only thing that looks kinda wonky here to me is that |
@omermizr I am not sure I entirely follow what you have said. I'll re-phase what I think you are suggesting here & add some of my own understanding - which might help -
Therefore imho the better fix is to do one of the following:
Not removing |
requests/sessions.py
Outdated
@@ -289,7 +289,9 @@ def rebuild_proxies(self, prepared_request, proxies): | |||
new_proxies = proxies.copy() | |||
no_proxy = proxies.get('no_proxy') | |||
|
|||
bypass_proxy = should_bypass_proxies(url, no_proxy=no_proxy) | |||
bypass_proxy = False | |||
if self.trust_env: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@omermizr / @sigmavirus24 / @nateprewitt - this change fixes the performance regression of #5891 but does not fix #5888. IMHO fixing #5888 is something that you might do by checking if a proxy has been selected as if one has not been selected you likely would not want to leak proxy credentials. Of course it might be easier to suggest that we do not explicitly support setting Proxy-Authorization
as a header and that users must specify proxy credentials in the proxy url.
Before the revised changed ->
After the revised change ->
|
bump cc @sigmavirus24 / @nateprewitt |
@nateprewitt can we merge this ? |
I think this is alright as a minor optimization, but doesn't address either of the original issues. I'd like to get those refactored and resolved before we merge anymore changes into the method. |
@nateprewitt imho it is slightly more than a minor optimisation (for certain use cases) & I have a concern that fixing/re-factoring the related code is likely to be complex and take a fairly long time. I am not sure how much #5894 (comment) applies to what you feel we should have as the result though. Mainly this part ->
|
Sure, I was conveying it doesn't affect the hot path for the average user. There may be larger improvements for more niche uses.
That may be true, I've left my thoughts on moving forward in the issue, but I don't know if we've arrived at a consensus. Either way, #5893 is a blocker for the next release of Requests, so merging this now or later has no effect on when it's available publicly. |
Co-authored-by: David Black <dblack@atlassian.com>
7442f4e
to
0d5347e
Compare
Signed-off-by: David Black dblack@atlassian.com