Skip to content

Commit

Permalink
fixes #27 update doc for rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Feb 17, 2017
1 parent 7d78f47 commit e4d5120
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/content/middleware/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ Sharing (CORS) handler supports single page applications from another domain to
A full request/response log handler to dump everything regarding to request and response
into log file for developers.

* [limit](https://networknt.github.io/light-java/middleware/limit/) -
A rate limiting handler to limit number of concurrent requests on the server.
Once the limit is reached, subsequent requests will be queued for later execution. The
size of the queue is configurable.

67 changes: 67 additions & 0 deletions docs/content/middleware/limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
date: 2017-02-17T14:10:28-05:00
title: Rate Limiting
---

Although our framework can handle potential millions requests per second, for
some public facing APIs, it might be a good idea to enable this handler to
limit the concurrent request to certain level in order to avoid DDOS attacks.

As this handler will impact the overall performance a little bit, it is not
configured as default in the swagger-codegen. You must manually enable it
in your API with the following steps.

## Dependency

In order to use this handler, the following dependency need to be added to
pom.xml in your project.

```
<dependency>
<groupId>com.networknt</groupId>
<artifactId>limit</artifactId>
<version>${version.light-java}</version>
</dependency>
```

## Service

As this middleware is not plugged in by default, we need to add it into
com.networknt.handler.MiddlewareHandler in src/main/resources/META-INF/services
folder. As this rate limiting handler needs to be failed fast, it need to be
put right after ExceptionHandler and MetricsHandler. The reason it is after
MetricsHandler is to capture 513 error code in InfluxDB and Grafana for
monitoring on production.

```
#Traceability Put traceabilityId into response header from request header if it exists
com.networknt.traceability.TraceabilityHandler
#Rate Limiting
com.networknt.limit.LimitHandler
#Metrics In order to calculate response time accurately, this needs to be the second.
com.networknt.metrics.MetricsHandler
#Exception Global exception handler that needs to be called first.
com.networknt.exception.ExceptionHandler
```

## Config

Here is the configuration file for rate limiting.

```
{
"description": "Rate Limit Handler",
"enabled": false,
"concurrentRequest": 1000,
"queueSize": -1
}
```

- enabled true to enable it and false to disable it.
- concurrentRequest number of concurrent request to be limited.
- queueSize -1 unlimited queue size which might use a lot of memory. > 1 integer will limit the requests to be queued and once queue is full, 513 will be returned for new requests.



0 comments on commit e4d5120

Please sign in to comment.