当服务器处于紧张状态时,自动用静态页面替换较重的页面,从而优雅地降级。
django-scaler的Python项目详细描述
Django Scaler
在服务器处于紧张状态时自动用静态页替换重页,从而优雅地降级。
Installation
- 在python路径中安装或添加django-scaler。
- 将scaler添加到INSTALLED_APPS设置中。
- 将scaler.middleware.ScalerMiddleware添加到MIDDLEWARE_CLASSES设置的顶部。
- 将(r“^scaler/”,include('scaler.url')添加到urlpatterns。
Overview
由于各种原因,服务器有时可能会过载。当那 碰巧你不想让昂贵的请求破坏你的整个网站。这个 站点必须将昂贵的请求重定向到“服务器忙”页,而服务器 处于加载状态,然后在加载后自动删除重定向 掉得够多了。
django-scaler
用两种方式解决这种情况。首先,它知道
请求自行重定向。其次,可以指示它重新定向
n最昂贵的要求。它将响应时间数据存储在内存缓存中
使它能够做出这些决定。
Usage
粘贴自test_settings.py:
DJANGO_SCALER = { 'server_busy_url_name': 'server-busy', # How many response times to consider for an URL. A small value means slow # response times are quickly acted upon, but it may be overly aggressive. # A large value means an URL must be slow for a number of requests before # it is acted upon. The default is 100. 'trend_size': 10, # How much slower than average the trend must be before redirection kicks # in. The default is 4.0. 'slow_threshold': 2.0, # How many seconds to keep redirecting an URL before serving normally. The # default is 60. 'redirect_for': 10, # A function that returns how many of the slowest URLs must be redirected. # Depending on the site, data and load on the server this may be a large # number. This allows external processes to instruct the middleware to # redirect. The default is 0. 'redirect_n_slowest_function': lambda: 0, # A function that returns what percentage of the slowest URLs must be # redirected. Depending on the site, data and load on the server this may # approach 100. The default is 0. 'redirect_percentage_slowest_function': lambda: 0, # A function that returns a list of regexes. URLs matching the regexes are # redirected. Each regex is a simple string. Do not prefix with r''. The # default is an empty list. 'redirect_regexes_function': lambda: [], }