Skip to content

Commit e94a197

Browse files
committed
fix: combine rules, use one tracking counter
1 parent c29e87c commit e94a197

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

salt/haproxy/config/haproxy.cfg.jinja

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,20 @@ frontend main
116116
bind :::80
117117
bind 127.0.0.1:19001 # This is our TLS socket.
118118

119-
# Apply rate limits per srvice
119+
# Define a stick table for all services
120+
stick-table type ip size 100k expire 30s store http_req_rate(10s)
121+
# Track all requests using a single counter
122+
# We could use the 3 available (sc0,1,2) to maybe tier requests
123+
# into say <=100, 101-500, >= 501 if we needed to?
124+
http-request track-sc0 src
125+
# then create the ACL for services in haproxy.sls that have a 'rate_limit' key,
126+
# constrained to the host header using the domain key in haproxy.sls
127+
# then adds a rule to deny via HTTP 429 if the respective ACL is matched and the stick table http request rate
128+
# is higher than the 'rate_limit' from haproxy.sls pillar date
120129
{% for service, config in haproxy.services.items() %}
121-
{% if config.get('rate_limit') and loop.index <= 2 %}
122-
stick-table type ip size 100k expire 30s store http_req_rate(1s)
123-
{% endif %}
124-
{% endfor %}
125-
126-
# Apply rate limits
127-
{% for service, config in haproxy.services.items() %}
128-
{% if config.get('rate_limit') and loop.index <= 2 %}
129-
acl is_{{ service }} hdr(host) -i {% for domain in config.domains %}{{ domain }} {% endfor %}
130-
http-request track-sc{{ loop.index }} src if is_{{ service }}
131-
http-request deny deny_status 429 if is_{{ service }} { sc{{ loop.index }}_http_req_rate() gt {{ config.rate_limit }} }
130+
{% if config.get('rate_limit') %}
131+
acl is_{{ service }} hdr(host) -i {% for domain in config.domains %}{{ domain }}{% endfor %}
132+
http-request deny deny_status 429 if is_{{ service }} { sc0_http_req_rate() gt {{ config.rate_limit }} }
132133
{% endif %}
133134
{% endfor %}
134135

0 commit comments

Comments
 (0)