Bucket4j not running, to rate limit api

1k views Asked by At

Pom.xml file

        <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
        <artifactId>bucket4j-spring-boot-starter</artifactId>
        <version>0.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>jcache</artifactId>
        <version>2.8.2</version>
    </dependency>

application-local.yml file

spring:
  cache:
    cache-names:
      - rate-limit-buckets
    caffeine:
      spec: maximumSize=100000,expireAfterAccess=3600s

bucket4j:
  enabled: true
  filters:
  - cache-name: rate-limit-buckets
    filter-method: servlet
    strategy: first
    url: /patient
    http-response-body: "{ \"status\": 429, \"error\": \"Too Many Requests\" }"
    rate-limits:
      - expression: getRemoteAddr()
        bandwidths:
        - capacity: 1
          time: 30
          unit: seconds

I have set the capacity to 1 and time to 30s so that I receive "Too Many Requests" error upon 2 api hits with in 30s, but I am not receiving any error or anything, basically bucket4j rate limiting is not even running as it seems.

Is my application-local.yml even being loaded? earlier I had the configurations in application-local.properties file and rate limiter was not working by that either, after exploring internet some people suggested using .yml file to load bucket4j configs so I did that too but even now it is not working.

1

There are 1 answers

2
dauthDaert06 On

I believe you also referred to this guide while configuring bucket4j. The only clear differences that I could spot between my configuration and yours are versions. Might I suggest you to upgrade your dependencies. I'm using Spring Boot 3.0.4

application.yml file

spring:
  cache:
    cache-names:
      - rate-limit-buckets
    caffeine:
      spec: maximumSize=100000,expireAfterAccess=3600s

bucket4j:
  enabled: true
  filters:
    - cache-name: rate-limit-buckets
      url: .*
      http-status-code: TOO_MANY_REQUESTS
      http-response-body: "{ \"error\": true, \"message\": \"You have exhausted your API Request Quota\" }"
      rate-limits:
        - cache-key: getRemoteAddr()
          bandwidths:
            - capacity: 1
              time: 30
              unit: seconds

pom.xml file

<dependencies>
    <dependency>
        <groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
        <artifactId>bucket4j-spring-boot-starter</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>jcache</artifactId>
        <version>3.1.5</version>
    </dependency>
</dependencies>

NOTE

  • expression: getRemoteAddr() got changed to - cache-key: getRemoteAddr() in newer version.

You might also wanna check out the github page.

Hope this helps.