connection pooling do not work when using proxy
See original GitHub issueScrapy create a new TCP4ClientEndpoint
for each request when using proxy in ScrapyAgent
while ProxyAgent
(twisted) use key = ("http-proxy", self._proxyEndpoint)
as connection pool key.
It causes creating new connection for each request when using proxy,
will get errno99: cannot assign requested address
when all ports has been used (socket TIME_WAIT).
scrapy/core/downloader/handlers/http11.py
class ScrapyAgent(object):
def _get_agent(self, request, timeout):
bindaddress = request.meta.get('bindaddress') or self._bindAddress
proxy = request.meta.get('proxy')
if proxy:
_, _, proxyHost, proxyPort, proxyParams = _parse(proxy)
scheme = _parse(request.url)[0]
proxyHost = to_unicode(proxyHost)
omitConnectTunnel = b'noconnect' in proxyParams
if scheme == b'https' and not omitConnectTunnel:
proxyConf = (proxyHost, proxyPort,
request.headers.get(b'Proxy-Authorization', None))
return self._TunnelingAgent(reactor, proxyConf,
contextFactory=self._contextFactory, connectTimeout=timeout,
bindAddress=bindaddress, pool=self._pool)
else:
endpoint = TCP4ClientEndpoint(reactor, proxyHost, proxyPort,
timeout=timeout, bindAddress=bindaddress)
return self._ProxyAgent(endpoint)
return self._Agent(reactor, contextFactory=self._contextFactory,
connectTimeout=timeout, bindAddress=bindaddress, pool=self._pool)
twisted/web/client.py
@implementer(IAgent)
class ProxyAgent(_AgentBase):
"""
An HTTP agent able to cross HTTP proxies.
@ivar _proxyEndpoint: The endpoint used to connect to the proxy.
@since: 11.1
"""
def __init__(self, endpoint, reactor=None, pool=None):
if reactor is None:
from twisted.internet import reactor
_AgentBase.__init__(self, reactor, pool)
self._proxyEndpoint = endpoint
def request(self, method, uri, headers=None, bodyProducer=None):
"""
Issue a new request via the configured proxy.
"""
# Cache *all* connections under the same key, since we are only
# connecting to a single destination, the proxy:
key = ("http-proxy", self._proxyEndpoint)
# To support proxying HTTPS via CONNECT, we will use key
# ("http-proxy-CONNECT", scheme, host, port), and an endpoint that
# wraps _proxyEndpoint with an additional callback to do the CONNECT.
return self._requestWithEndpoint(key, self._proxyEndpoint, method,
URI.fromBytes(uri), headers,
bodyProducer, uri)
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Advanced Connection Pooling with the Heimdall Proxy
The application is aware pooling will be used, and does not leave connections idle, but instead opens and closes them as needed. All...
Read more >Connection Pool and Proxy Users - Oracle Communities
Individually proxy works (a client user can connect with a proxy id) and pooling works without a proxy user, but they don't work...
Read more >Does a web request with proxy always require a new ...
Yes, absolutely. This is called connection pooling and it is common in practice. The proxy opens a pool of persistent connections with each ......
Read more >Postgres Connection Pooling and Proxies - Arctype
This technique can improve the performance of an application by reducing the number of open connections to a database. Another related term is...
Read more >Connection Pooling for MySQL with the Heimdall Proxy
The application is aware pooling will be used, and does not leave connections idle, but instead opens and closes them as needed;; All ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I used monkey patch to fix the proxy connection pooling problem. But it causes a lot of timeout(30s) errors when I am sure the request didn’t reach 30s. When I removed monkey patch, it changed to normal.
Here is the patch code.
@redapple My problem is solved with your patch after solving self-implemented proxy server problem