hf400159 commented on code in PR #7581:
URL: https://github.com/apache/apisix/pull/7581#discussion_r934227000


##########
docs/en/latest/plugins/limit-conn.md:
##########
@@ -141,3 +141,79 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 
'X-API-KEY: edd1c9f034335f13
     }
 }'
 ```
+
+## Limit the rate of WebSocket connections
+
+Apache APISIX supports WebSocket proxy, we can use `limit-conn` plugin to 
limit the number of concurrent WebSocket connections.
+
+1. Start WebSocket Server
+
+````shell
+docker run -p 1980:8080 --name websocket-demo casperklein/websocket-demo
+````
+
+2. Register the route, enable the WebSocket proxy on the route and enable the 
limit-conn plugin
+
+````shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: 
edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "uri": "/ws",
+    "enable_websocket":true,
+    "plugins": {
+        "limit-conn": {
+            "conn": 1,
+            "burst": 0,
+            "default_conn_delay": 0.1,
+            "rejected_code": 503,
+            "key_type": "var",
+            "key": "remote_addr"
+        }
+    },
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "127.0.0.1:1980": 1
+        }
+    }
+}'
+````
+
+The above route enables the WebSocket proxy on `/ws`, and limits the number of 
concurrent WebSocket connections to 1. More than 1 concurrent WebSocket 
connection will return 503 to reject the request.
+
+3. Initiate a WebSocket request, and the link is established successfully
+
+````shell
+curl --include \
+     --no-buffer \
+     --header "Connection: Upgrade" \
+     --header "Upgrade: websocket" \
+     --header "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==" \
+     --header "Sec-WebSocket-Version: 13" \
+     --http1.1 \
+     http://127.0.0.1:9080/ws
+
+HTTP/1.1 101 Switching Protocols
+Connection: upgrade
+Upgrade: websocket
+Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
+Server: APISIX/2.15.0
+````
+
+4. Initiate the WebSocket request again in another terminal, the request will 
be rejected
+
+````shell
+HTTP/1.1 503 Service Temporarily Unavailable
+Date: Mon, 01 Aug 2022 03:49:17 GMT
+Content-Type: text/html; charset=utf-8
+Content-Length: 194
+Connection: keep-alive
+Server: APISIX/2.15.0
+
+<html>
+<head><title>503 Service Temporarily Unavailable</title></head>
+<body>
+<center><h1>503 Service Temporarily Unavailable</h1></center>
+<hr><center>openresty</center>
+</body>
+</html>

Review Comment:
   ditto



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to