mirror of https://github.com/scrapy/scrapy.git
Merge pull request #1132 from sibiryakov/dns-options2
[MRG+1] Dns cache size and timeout options
This commit is contained in:
commit
fb85bd4b10
|
|
@ -290,6 +290,24 @@ Default: ``True``
|
|||
|
||||
Whether to enable DNS in-memory cache.
|
||||
|
||||
.. setting:: DNSCACHE_SIZE
|
||||
|
||||
DNSCACHE_SIZE
|
||||
----------------
|
||||
|
||||
Default: ``10000``
|
||||
|
||||
DNS in-memory cache size.
|
||||
|
||||
.. setting:: DNS_TIMEOUT
|
||||
|
||||
DNS_TIMEOUT
|
||||
----------------
|
||||
|
||||
Default: ``60``
|
||||
|
||||
Timeout for processing of DNS queries in seconds. Float is supported.
|
||||
|
||||
.. setting:: DOWNLOADER
|
||||
|
||||
DOWNLOADER
|
||||
|
|
|
|||
|
|
@ -161,9 +161,9 @@ class CrawlerProcess(CrawlerRunner):
|
|||
return
|
||||
d.addBoth(lambda _: self._stop_reactor())
|
||||
|
||||
if self.settings.getbool('DNSCACHE_ENABLED'):
|
||||
reactor.installResolver(CachingThreadedResolver(reactor))
|
||||
|
||||
cache_size = self.settings.getint('DNSCACHE_SIZE') if self.settings.getbool('DNSCACHE_ENABLED') else 0
|
||||
reactor.installResolver(CachingThreadedResolver(reactor, cache_size,
|
||||
self.settings.getfloat('DNS_TIMEOUT')))
|
||||
tp = reactor.getThreadPool()
|
||||
tp.adjustPoolsize(maxthreads=self.settings.getint('REACTOR_THREADPOOL_MAXSIZE'))
|
||||
reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
|
||||
|
|
|
|||
|
|
@ -4,15 +4,20 @@ from twisted.internet.base import ThreadedResolver
|
|||
from scrapy.utils.datatypes import LocalCache
|
||||
|
||||
# TODO: cache misses
|
||||
# TODO: make cache size a setting
|
||||
|
||||
dnscache = LocalCache(10000)
|
||||
|
||||
class CachingThreadedResolver(ThreadedResolver):
|
||||
def __init__(self, reactor, cache_size, timeout):
|
||||
super(CachingThreadedResolver, self).__init__(reactor)
|
||||
dnscache.limit = cache_size
|
||||
self.timeout = timeout
|
||||
|
||||
def getHostByName(self, name, timeout = (1, 3, 11, 45)):
|
||||
def getHostByName(self, name, timeout=None):
|
||||
if name in dnscache:
|
||||
return defer.succeed(dnscache[name])
|
||||
if not timeout:
|
||||
timeout = self.timeout
|
||||
d = super(CachingThreadedResolver, self).getHostByName(name, timeout)
|
||||
d.addCallback(self._cache_result, name)
|
||||
return d
|
||||
|
|
|
|||
|
|
@ -52,6 +52,8 @@ DEPTH_STATS = True
|
|||
DEPTH_PRIORITY = 0
|
||||
|
||||
DNSCACHE_ENABLED = True
|
||||
DNSCACHE_SIZE = 10000
|
||||
DNS_TIMEOUT = 60
|
||||
|
||||
DOWNLOAD_DELAY = 0
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue