You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/09/23 12:57:51 UTC

[GitHub] [apisix] membphis commented on a change in pull request #2270: feat: The "limit-req" plugin adds the "consumer_name" method to limit the request speed

membphis commented on a change in pull request #2270:
URL: https://github.com/apache/apisix/pull/2270#discussion_r493539848



##########
File path: apisix/plugins/limit-req.lua
##########
@@ -67,7 +67,16 @@ function _M.access(conf, ctx)
         return 500
     end
 
-    local key = (ctx.var[conf.key] or "") .. ctx.conf_type .. ctx.conf_version
+    local key
+    if conf.key == "consumer_name" then
+        if not ctx.consumer_id then
+            core.log.error("The username of consumer is nil.")
+            return 401, { message = "Missing consumer's username."}

Review comment:
       the response code should be `500`. 
   
   This problem occurs without authentication plug-ins. We can write the reason to the error log, it can help the user to confirm where is the problem.

##########
File path: apisix/plugins/limit-req.lua
##########
@@ -67,7 +67,16 @@ function _M.access(conf, ctx)
         return 500
     end
 
-    local key = (ctx.var[conf.key] or "") .. ctx.conf_type .. ctx.conf_version
+    local key
+    if conf.key == "consumer_name" then
+        if not ctx.consumer_id then
+            core.log.error("The username of consumer is nil.")
+            return 401, { message = "Missing consumer's username."}
+        end
+        key = ctx.consumer_id .. ctx.conf_type .. ctx.conf_version
+    else

Review comment:
       add a blank line before `else`

##########
File path: doc/plugins/limit-req.md
##########
@@ -104,6 +107,78 @@ Server: APISIX web server
 
 This means that the limit req plugin is in effect.
 
+## How to enable on the `consumer`
+
+To enable the `limit-req` plugin on the consumer, it needs to be used together with the authorization plugin. Here, the key-auth authorization plugin is taken as an example.
+
+1. Bind the `limit-req` plugin to the consumer
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "username": "limit_req_consumer_name",

Review comment:
       we should use a real name like `Jack` or `Lee`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org