File tree Expand file tree Collapse file tree 2 files changed +16
-5
lines changed
Expand file tree Collapse file tree 2 files changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ haproxy:
1717 - docs.python.org
1818 - doc.python.org
1919 check: " HEAD /_check HTTP/1.1\\r\\nHost:\\ docs.python.org"
20+ rate_limit: 100
2021
2122 downloads:
2223 domains:
@@ -75,6 +76,7 @@ haproxy:
7576 - {{ config.server_name }}
7677 verify_host: bugs.psf.io
7778 check: " HEAD / HTTP/1.1\\r\\nHost:\\ {{ config.server_name }}"
79+ rate_limit: {{ config.get(' rate_limit' , 10 ) }}
7880 {% endfor %}
7981
8082 moin:
Original file line number Diff line number Diff line change @@ -52,6 +52,12 @@ global
5252 # Lower the amount of space we reserve for header rewriting
5353 tune.maxrewrite 1024
5454
55+ # rate limits only if there is a rate_limit in haproxy.sls
56+ {% for service , config in haproxy .services .items () %}
57+ {% if config .get ('rate_limit' ) %}
58+ stick-table type ip size 100k expire 30s store http_req_rate(1s) name {{ service }}_ratelimit
59+ {% endif %}
60+ {% endfor %}
5561
5662defaults
5763 log global
@@ -117,11 +123,14 @@ frontend main
117123 bind :::80
118124 bind 127.0.0.1:19001 # This is our TLS socket.
119125
120- # Client rate limiting
121- # See: https://www.haproxy.com/blog/four-examples-of-haproxy-rate-limiting
122- stick-table type ip size 100k expire 15s store http_req_rate(10s)
123- http-request track-sc0 src
124- http-request deny deny_status 429 if { sc_http_req_rate(0) gt 20 }
126+ # Apply rate limits per srvice
127+ {% for service , config in haproxy .services .items () %}
128+ {% if config .get ('rate_limit' ) %}
129+ acl is_{{ service }} hdr(host) -i {% for domain in config .domains %} {{ domain }} {% endfor %}
130+ http-request track-sc{{ loop.index }} src table {{ service }}_ratelimit if is_{{ service }}
131+ http-request deny deny_status 429 if is_{{ service }} { sc{{ loop.index }}_http_req_rate() gt {{ config.rate_limit }} }
132+ {% endif %}
133+ {% endfor %}
125134
126135 # Custom logging format, this is the same as the normal "httplog" in
127136 # HAProxy except information about the TLS connection is included.
You can’t perform that action at this time.
0 commit comments