<commit_before>Spring Cloud Zuul RateLimit [![Build Status](https://travis-ci.org/marcosbarbero/spring-cloud-zuul-ratelimit.svg?branch=master)](https://travis-ci.org/marcosbarbero/spring-cloud-zuul-ratelimit) [![Coverage Status](https://coveralls.io/repos/github/marcosbarbero/spring-cloud-zuul-ratelimit/badge.svg?branch=master)](https://coveralls.io/github/marcosbarbero/spring-cloud-zuul-ratelimit?branch=master)
---
Module to enable rate limit per service in Netflix Zuul.  
There are five built-in rate limit approaches:
 - Authenticated User
   - Uses the authenticated username or 'anonymous'
 - Request Origin 
   - Uses the user origin request
 - URL
   - Uses the request path of the upstream service
 - Global configuration per service: 
   - This one does not validate the request Origin, Authenticated User or URI
   - To use this approach just don't set param 'type'
   
>Note: It is possible to combine Authenticated User, Request Origin and URL just adding 
multiple values to the list

Usage
---
>This project is available on maven central

Add the dependency on pom.xml
```
<dependency>
    <groupId>com.marcosbarbero.cloud</groupId>
    <artifactId>spring-cloud-zuul-ratelimit</artifactId>
    <version>1.3.3.RELEASE</version>
</dependency>
```

In case you are using Redis there will be needed to add the following dependency
```
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
```

In case you are using Consul there will be needed to add the following dependency
```
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul</artifactId>
</dependency>
```

In case you are using Spring Data there will be needed to add the following dependency
```
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```

Sample configuration
```
zuul:
  ratelimit:
    key-prefix: your-prefix 
    enabled: true 
    repository: REDIS 
    behind-proxy: true
    default-policy: #optional - will apply unless specific policy exists
      limit: 10 #optional - request number limit per refresh interval window
      quota: 1000 #optional - request time limit per refresh interval window (in seconds)
      refresh-interval: 60 #default value (in seconds)
      type: #optional
        - user
        - origin
        - url
    policies:
      myServiceId:
        limit: 10 #optional - request number limit per refresh interval window
        quota: 1000 #optional - request time limit per refresh interval window (in seconds)
        refresh-interval: 60 #default value (in seconds)
        type: #optional
          - user
          - origin
          - url
```

Available implementations
---
There are three implementations provided:  
 * `InMemoryRateLimiter` - uses ConcurrentHashMap as data storage
 * `ConsulRateLimiter` - uses [Consul](https://www.consul.io/) as data storage
 * `RedisRateLimiter` - uses [Redis](https://redis.io/) as data storage
 * `SpringDataRateLimiter` - uses [Spring Data](https://projects.spring.io/spring-data-jpa/) as data storage
 
Common application properties
---
Property namespace: __zuul.ratelimit__

|Property name| Values |Default Value|
|-------------|:-------|:-------------:|
|enabled|true/false|false|
|behind-proxy|true/false|false|
|key-prefix|String|${spring.application.name:rate-limit-application}|
|repository|CONSUL, REDIS, JPA, IN_MEMORY|IN_MEMORY|
|default-policy|[Policy](https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit/blob/master/spring-cloud-zuul-ratelimit-core/src/main/java/com/marcosbarbero/cloud/autoconfigure/zuul/ratelimit/config/properties/RateLimitProperties.java#L64)| - |
|policies|List of [Policy](https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit/blob/master/spring-cloud-zuul-ratelimit-core/src/main/java/com/marcosbarbero/cloud/autoconfigure/zuul/ratelimit/config/properties/RateLimitProperties.java#L64)| - |

Policy properties:

|Property name| Values |Default Value|
|-------------|:-------|:-------------:|
|limit|number of calls| - |
|quota|time of calls| - |
|refresh-interval|seconds|60|
|type| [ORIGIN, USER, URL] | [] |

Further Customization
---

If your application needs to control the key strategy beyond the options offered by the type property then you can 
supply a custom `RateLimitKeyGenerator` implementation adding further qualifiers or something entirely different:

    @Bean
    public RateLimitKeyGenerator rateLimitKeyGenerator(final RateLimitProperties properties) {
        return new DefaultRateLimitKeyGenerator(properties) {<commit_msg>Apply markdown format<commit_after>Spring Cloud Zuul RateLimit [![Build Status](https://travis-ci.org/marcosbarbero/spring-cloud-zuul-ratelimit.svg?branch=master)](https://travis-ci.org/marcosbarbero/spring-cloud-zuul-ratelimit) [![Coverage Status](https://coveralls.io/repos/github/marcosbarbero/spring-cloud-zuul-ratelimit/badge.svg?branch=master)](https://coveralls.io/github/marcosbarbero/spring-cloud-zuul-ratelimit?branch=master)
---
Module to enable rate limit per service in Netflix Zuul.  
There are five built-in rate limit approaches:
 - Authenticated User
   - Uses the authenticated username or 'anonymous'
 - Request Origin 
   - Uses the user origin request
 - URL
   - Uses the request path of the upstream service
 - Global configuration per service: 
   - This one does not validate the request Origin, Authenticated User or URI
   - To use this approach just don't set param 'type'
   
>Note: It is possible to combine Authenticated User, Request Origin and URL just adding 
multiple values to the list

Usage
---
>This project is available on maven central

Add the dependency on pom.xml
```
<dependency>
    <groupId>com.marcosbarbero.cloud</groupId>
    <artifactId>spring-cloud-zuul-ratelimit</artifactId>
    <version>1.3.3.RELEASE</version>
</dependency>
```

In case you are using Redis there will be needed to add the following dependency
```
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
```

In case you are using Consul there will be needed to add the following dependency
```
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul</artifactId>
</dependency>
```

In case you are using Spring Data there will be needed to add the following dependency
```
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```

Sample configuration
```
zuul:
  ratelimit:
    key-prefix: your-prefix 
    enabled: true 
    repository: REDIS 
    behind-proxy: true
    default-policy: #optional - will apply unless specific policy exists
      limit: 10 #optional - request number limit per refresh interval window
      quota: 1000 #optional - request time limit per refresh interval window (in seconds)
      refresh-interval: 60 #default value (in seconds)
      type: #optional
        - user
        - origin
        - url
    policies:
      myServiceId:
        limit: 10 #optional - request number limit per refresh interval window
        quota: 1000 #optional - request time limit per refresh interval window (in seconds)
        refresh-interval: 60 #default value (in seconds)
        type: #optional
          - user
          - origin
          - url
```

Available implementations
---
There are three implementations provided:  
 * `InMemoryRateLimiter` - uses ConcurrentHashMap as data storage
 * `ConsulRateLimiter` - uses [Consul](https://www.consul.io/) as data storage
 * `RedisRateLimiter` - uses [Redis](https://redis.io/) as data storage
 * `SpringDataRateLimiter` - uses [Spring Data](https://projects.spring.io/spring-data-jpa/) as data storage
 
Common application properties
---
Property namespace: __zuul.ratelimit__

|Property name| Values |Default Value|
|-------------|:-------|:-------------:|
|enabled       |true/false                   |false|
|behind-proxy  |true/false                   |false|
|key-prefix    |String                       |${spring.application.name:rate-limit-application}|
|repository    |CONSUL, REDIS, JPA, IN_MEMORY|IN_MEMORY|
|default-policy|[Policy](https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit/blob/master/spring-cloud-zuul-ratelimit-core/src/main/java/com/marcosbarbero/cloud/autoconfigure/zuul/ratelimit/config/properties/RateLimitProperties.java#L64)| - |
|policies      |List of [Policy](https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit/blob/master/spring-cloud-zuul-ratelimit-core/src/main/java/com/marcosbarbero/cloud/autoconfigure/zuul/ratelimit/config/properties/RateLimitProperties.java#L64)| - |

Policy properties:

|Property name| Values |Default Value|
|-------------|:-------|:-------------:|
|limit           |number of calls      |  - |
|quota           |time of calls        |  - |
|refresh-interval|seconds              | 60 |
|type            | [ORIGIN, USER, URL] | [] |

Further Customization
---

If your application needs to control the key strategy beyond the options offered by the type property then you can 
supply a custom `RateLimitKeyGenerator` implementation adding further qualifiers or something entirely different:

    @Bean
    public RateLimitKeyGenerator rateLimitKeyGenerator(final RateLimitProperties properties) {
        return new DefaultRateLimitKeyGenerator(properties) {
{'fraction_non_alphanumeric': 0.11873295573736102, 'fraction_numerical': 0.005244388504300398, 'mean_word_length': 3.5996140858658947, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 59, 'https://': 18, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '585394', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2713, 'n_words': 757}