0

We are trying to implement the no.of request to the API using below syntax from the documentation for our API handler

    val app = Javalin.create {
        it.defaultContentType = "application/json"
        it.enableWebjars()
        it.addStaticFiles("", Location.CLASSPATH)
        it.enableCorsForAllOrigins()
        it.dynamicGzip = true
    }
    app.options("/*") { ctx -> ctx.status(405) }    
    app.get("/*"){ctx ->
        RateLimit(ctx).requestPerTimeUnit(5, TimeUnit.MINUTES) // throws if rate limit is exceeded
        ctx.status("Hello, rate-limited World!") }

But end up getting unresolved reference Ratelimit error. any pointers with the syntax here?

We are using Kotlin to implement it.

Maverick
  • 397
  • 1
  • 4
  • 18

1 Answers1

1

It seems the library changed how rate limiting is used without updating the docs.

Use the following code instead:

NaiveRateLimit.requestPerTimeUnit(ctx, 5, TimeUnit.MINUTES)

You can also use a single rate limiter instance like this:

val rateLimiter = RateLimiter(TimeUnit.MINUTES)

app.get("/*"){ ctx ->
    rateLimiter.incrementCounter(ctx, 5) // throws if rate limit is exceeded
    ctx.status("Hello, rate-limited World!")
}
SapuSeven
  • 1,473
  • 17
  • 30