mirror of
https://github.com/frappe/bench.git
synced 2025-01-22 22:58:31 +00:00
[Minor] Add rate limiting for bench
This commit is contained in:
parent
20c24a3923
commit
f9067f3d7a
@ -23,6 +23,7 @@ def make_nginx_conf(bench_path, yes=False):
|
||||
"socketio_port": config.get('socketio_port'),
|
||||
"bench_name": bench_name,
|
||||
"bench_name_hash": bench_name_hash,
|
||||
"limit_conn_shared_memory": get_limit_conn_shared_memory(),
|
||||
"error_pages": get_error_pages(),
|
||||
|
||||
# for nginx map variable
|
||||
@ -160,3 +161,10 @@ def get_error_pages():
|
||||
return {
|
||||
502: os.path.join(templates, '502.html')
|
||||
}
|
||||
|
||||
def get_limit_conn_shared_memory():
|
||||
"""Allocate 2 percent of total virtual memory as shared memory for nginx limit_conn_zone"""
|
||||
import psutil
|
||||
total_vm = (psutil.virtual_memory().total) / (1024 * 1024) # in MB
|
||||
|
||||
return int(0.02 * total_vm)
|
||||
|
@ -127,5 +127,9 @@ def reload_supervisor():
|
||||
pass
|
||||
|
||||
def reload_nginx():
|
||||
subprocess.check_output(['sudo', find_executable('nginx'), '-t'])
|
||||
try:
|
||||
subprocess.check_output(['sudo', find_executable('nginx'), '-t'])
|
||||
except:
|
||||
raise
|
||||
|
||||
service('nginx', 'reload')
|
||||
|
@ -21,6 +21,8 @@ server {
|
||||
|
||||
root {{ sites_path }};
|
||||
|
||||
limit_conn per_host_{{ bench_name_hash }} 8;
|
||||
|
||||
{% if ssl_certificate and ssl_certificate_key %}
|
||||
ssl on;
|
||||
ssl_certificate {{ ssl_certificate }};
|
||||
@ -139,7 +141,7 @@ upstream {{ bench_name}}-socketio-server {
|
||||
server 127.0.0.1:{{ socketio_port or 3000 }} fail_timeout=0;
|
||||
}
|
||||
|
||||
limit_conn_zone $host zone=per_host_{{ bench_name_hash }}:3000m;
|
||||
limit_conn_zone $host zone=per_host_{{ bench_name_hash }}:{{ limit_conn_shared_memory }}m;
|
||||
|
||||
# setup maps
|
||||
{%- set site_name_variable="$host" %}
|
||||
|
@ -3,6 +3,7 @@ jinja2
|
||||
virtualenv
|
||||
requests
|
||||
honcho
|
||||
psutil
|
||||
python-crontab
|
||||
semantic_version
|
||||
GitPython==0.3.2.rc1
|
||||
|
Loading…
x
Reference in New Issue
Block a user