This is an automated email from the ASF dual-hosted git repository.

tzssangglass pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 7b69a37  feat(plugin): add limit quota header switch for limit-count 
(#4782)
7b69a37 is described below

commit 7b69a374ac5b31dc0dedc74291fc96f1e7d05a26
Author: okaybase <[email protected]>
AuthorDate: Tue Aug 10 10:54:34 2021 +0800

    feat(plugin): add limit quota header switch for limit-count (#4782)
    
    Co-authored-by: 罗泽轩 <[email protected]>
---
 apisix/plugins/limit-count.lua        |  9 ++++--
 docs/en/latest/plugins/limit-count.md |  1 +
 docs/zh/latest/plugins/limit-count.md |  1 +
 t/plugin/limit-count-redis2.t         | 55 +++++++++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/apisix/plugins/limit-count.lua b/apisix/plugins/limit-count.lua
index ac0f603..482db82 100644
--- a/apisix/plugins/limit-count.lua
+++ b/apisix/plugins/limit-count.lua
@@ -50,7 +50,8 @@ local schema = {
             enum = {"local", "redis", "redis-cluster"},
             default = "local",
         },
-        allow_degradation = {type = "boolean", default = false}
+        allow_degradation = {type = "boolean", default = false},
+        show_limit_quota_header = {type = "boolean", default = true}
     },
     required = {"count", "time_window"},
     dependencies = {
@@ -184,8 +185,10 @@ function _M.access(conf, ctx)
         return 500, {error_msg = "failed to limit count: " .. err}
     end
 
-    core.response.set_header("X-RateLimit-Limit", conf.count,
-                             "X-RateLimit-Remaining", remaining)
+    if conf.show_limit_quota_header then
+        core.response.set_header("X-RateLimit-Limit", conf.count,
+            "X-RateLimit-Remaining", remaining)
+    end
 end
 
 
diff --git a/docs/en/latest/plugins/limit-count.md 
b/docs/en/latest/plugins/limit-count.md
index 9b921be..8f5258c 100644
--- a/docs/en/latest/plugins/limit-count.md
+++ b/docs/en/latest/plugins/limit-count.md
@@ -43,6 +43,7 @@ Limit request rate by a fixed number of requests in a given 
time window.
 | rejected_code       | integer | optional                                | 
503           | [200,...,599]                                                   
                                        | The HTTP status code returned when 
the request exceeds the threshold is rejected, default 503.                     
                                                                                
                                                                                
                        |
 | policy              | string  | optional                                | 
"local"       | ["local", "redis", "redis-cluster"]                             
                                        | The rate-limiting policies to use for 
retrieving and incrementing the limits. Available values are `local`(the 
counters will be stored locally in-memory on the node), `redis`(counters are 
stored on a Redis server and will be shared across the nodes, usually use it to 
do the global speed limit) [...]
 | allow_degradation              | boolean  | optional                         
       | false       |                                                          
           | Whether to enable plugin degradation when the limit-count function 
is temporarily unavailable(e.g. redis timeout). Allow requests to continue when 
the value is set to true, default false. |
+| show_limit_quota_header              | boolean  | optional                   
             | true       |                                                     
                | Whether show `X-RateLimit-Limit` and `X-RateLimit-Remaining` 
(which mean the total number of requests and the remaining number of requests 
that can be sent) in the response header, default true. |
 | redis_host          | string  | required for `redis`                    |    
           |                                                                    
                                     | When using the `redis` policy, this 
property specifies the address of the Redis server.                             
                                                                                
                                                                                
                       |
 | redis_port          | integer | optional                                | 
6379          | [1,...]                                                         
                                        | When using the `redis` policy, this 
property specifies the port of the Redis server.                                
                                                                                
                                                                                
                       |
 | redis_password      | string  | optional                                |    
           |                                                                    
                                     | When using the `redis`  or 
`redis-cluster` policy, this property specifies the password of the Redis 
server.                                                                         
                                                                                
                                  [...]
diff --git a/docs/zh/latest/plugins/limit-count.md 
b/docs/zh/latest/plugins/limit-count.md
index 4e97832..ce0c806 100644
--- a/docs/zh/latest/plugins/limit-count.md
+++ b/docs/zh/latest/plugins/limit-count.md
@@ -46,6 +46,7 @@ title: limit-count
 | rejected_code       | integer | 可选                               | 503       
    | [200,...,599]                                                             
                              | 当请求超过阈值被拒绝时,返回的 HTTP 状态码                        
                                                                                
                                                                                
                                                                                
              [...]
 | policy              | string  | 可选                               | "local"   
    | ["local", "redis", "redis-cluster"]                                       
                              | 
用于检索和增加限制的速率限制策略。可选的值有:`local`(计数器被以内存方式保存在节点本地,默认选项) 和 `redis`(计数器保存在 Redis 
服务节点上,从而可以跨节点共享结果,通常用它来完成全局限速);以及`redis-cluster`,跟 redis 功能一样,只是使用 redis 集群方式。  
                                                                                
                                                                      |
 | allow_degradation              | boolean  | 可选                               
 | false       |                                                                
     | 当限流插件功能临时不可用时(例如,Redis 超时)是否允许请求继续。当值设置为 true 时则自动允许请求继续,默认值是 false。|
+| show_limit_quota_header              | boolean  | 可选                         
       | true       |                                                           
          | 是否在响应头中显示 `X-RateLimit-Limit` 和 `X-RateLimit-Remaining` 
(限制的总请求数和剩余还可以发送的请求数),默认值是 true。 |
 | redis_host          | string  | `redis` 必须                       |           
    |                                                                           
                              | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的地址。           
                                                                                
                                                                                
                                                                                
              [...]
 | redis_port          | integer | 可选                               | 6379      
    | [1,...]                                                                   
                              | 当使用 `redis` 限速策略时,该属性是 Redis 服务节点的端口            
                                                                                
                                                                                
                                                                                
              [...]
 | redis_password      | string  | 可选                               |           
    |                                                                           
                              | 当使用 `redis`  或者 `redis-cluster`  限速策略时,该属性是 
Redis 服务节点的密码。                                                                  
                                                                                
                                                                                
                  [...]
diff --git a/t/plugin/limit-count-redis2.t b/t/plugin/limit-count-redis2.t
index d620ef0..59bafda 100644
--- a/t/plugin/limit-count-redis2.t
+++ b/t/plugin/limit-count-redis2.t
@@ -243,3 +243,58 @@ passed
 GET /hello
 --- response_body
 hello world
+
+
+
+=== TEST 8: set route, with don't show limit quota header
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "plugins": {
+                        "limit-count": {
+                            "count": 2,
+                            "time_window": 60,
+                            "rejected_code": 503,
+                            "key": "remote_addr",
+                            "policy": "redis",
+                            "show_limit_quota_header": false,
+                            "redis_host": "127.0.0.1",
+                            "redis_port": 6379,
+                            "redis_database": 1,
+                            "redis_timeout": 1001
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 9: don't show limit quota header for TEST 8
+--- request
+GET /hello
+--- raw_response_headers_unlike eval
+qr/X-RateLimit-Limit/

Reply via email to