Skip to content
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

Added a rate limiter for load reduction on the website #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Read a config file to set parameters:
***You can overide (or add for list) any parameters define in the config.json***

>>> python main.py --config config/config.json
More configuration options can be found in config.py:
- Set custom xml tags for the sitemap
- Set an user agent
- Configure the crawling rate

#### Enable debug:

Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
xml_footer = "</urlset>"

crawler_user_agent = 'Sitemap crawler'

number_calls = 1 # number of requests per call period
call_period = 15 # time in seconds per number of requests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default should be no rate limiting, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No limit would be in line with how the original code works, yes. I could not find a default option in the ratelimit package, you could set the default to a very high number as a workaround though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possibility would be having something like this in crawler.py:

if number_calls is None or call_period is None:
    rate_limit_decorator = lambda func: func
else:
    rate_limit_decorator = limits(calls=config.number_calls, period=config.call_period)

Haven't dealt with @sleep_and_retry, but I believe should be possible to combine that into rate_limit_decorator. Of course, this strategy comes at the cost of increasing the complexity of the code.

6 changes: 3 additions & 3 deletions crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import mimetypes
import os
from ratelimit import limits, sleep_and_retry

class IllegalArgumentError(ValueError):
pass
Expand All @@ -24,7 +25,6 @@ class Crawler:
output = None
report = False

config = None
domain = ""

exclude = []
Expand Down Expand Up @@ -144,8 +144,8 @@ async def crawl_all_pending_urls(self, executor):
logging.debug('all crawl tasks have completed nicely')
return



@sleep_and_retry
@limits(calls=config.number_calls, period=config.call_period)
def __crawl(self, current_url):
url = urlparse(current_url)
logging.info("Crawling #{}: {}".format(self.num_crawled, url.geturl()))
Expand Down