HAProxy:检查棒表包含所需的密钥

问题描述 投票:0回答:1

如果活跃用户数量超过所需阈值,我想设置新用户连接限制。用户通过http连接到负载均衡器。我使用 Stick-table 来限制用户数量(通过唯一的标头):

frontend echo
    bind *:8881
    mode http

    # user is active if seds any request every 30s or often
    stick-table type string size 100k expire 30s store gpc0_rate(10s) 
    # track keys if table size less than limit
    http-request track-sc0 req.hdr(Client-ID) if { table_cnt lt 100 } 
    # limit of new not active users
    http-request deny deny_status 429 if { table_cnt ge 100 } { <❓check that "Client-ID" keys's value exists in the table> }

    use_backend echo

主要思想是,如果表中不存在

Client-ID
标头的值,则拒绝所有新用户的请求。有可能吗?

haproxy haproxy-ingress
1个回答
0
投票

您已经接近解决方案了。您需要in_table转换器:

使用输入样本的字符串表示形式来执行查找 指定的表。如果在表中找不到该键,则返回布尔值 false 被返回。否则返回布尔值 true。这可以用来验证 表中存在某个键来跟踪某些元素(例如是否 或者尚未看到源 IP 地址或授权标头)。

在您的情况下,这将是一个多 ACL:

acl clientid_in_table req.hdr(client-id),in_table
并且您的配置将变为(为了清楚起见,我更喜欢命名我的 ACL):

frontend echo
    bind *:8881
    mode http

    # user is active if seds any request every 30s or often
    stick-table type string size 100k expire 30s store gpc0_rate(10s) 
    # ACL to check if client-id is in table
    acl clientid_in_table req.hdr(client-id),in_table
    # track keys if table size less than limit
    http-request track-sc0 req.hdr(Client-ID) if { table_cnt lt 100 } 
    # limit of new not active users
    http-request deny deny_status 429 if { table_cnt ge 100 } !clientid_in_table

    use_backend echo

不知道如果请求中缺少标头会做什么,但如果需要,您也可以在此基础上拒绝它。

© www.soinside.com 2019 - 2024. All rights reserved.