Krate is a rate limiter library for Kotlin.
- Token Bucket Algorithm: Krate uses the token bucket algorithm, a widely-used technique for rate limiting, to ensure efficient and fair resource allocation.
- Burst Support: Krate allows for bursty request patterns, letting clients make a certain number of requests in a short time before being rate-limited.
- Redis Support: Krate offers support for Redis as a backend for storing rate limit information, enabling distributed rate limiting across multiple instances of your application.
- Keys eviction support: Krate automatically evicts unused keys, optimizing memory usage and ensuring optimal performance.
implementation 'io.github.lpicanco:krate-core:${krateVersion}'
implementation("io.github.lpicanco:krate-core:${krateVersion}")
<dependency>
<groupId>io.github.lpicanco</groupId>
<artifactId>krate-core</artifactId>
<version>${krateVersion}</version>
</dependency>
import com.neutrine.krate.rateLimiter
// Create a rate limiter with a rate of 5 per second.
val rateLimiter = rateLimiter(maxRate = 5)
// Use the rate limiter.
val taken: Boolean = rateLimiter.tryTake()
// Create a rate limiter with a rate of 5 per minute and a max burst of 10.
val rateLimiter = rateLimiter(maxRate = 5) {
maxBurst = 10
maxRateTimeUnit = ChronoUnit.MINUTES
}
// Use the rate limiter.
val taken: Boolean = rateLimiter.tryTake()
// Create a rate limiter with a rate of 5 per second.
val rateLimiter = rateLimiter(maxRate = 5)
// Wait until a token is available.
rateLimiter.awaitUntilTake()
import com.neutrine.krate.rateLimiter
// Create a rate limiter with a rate of 5 per second. Unused keys will be evicted after 2 hours.
val rateLimiter = rateLimiter(maxRate = 5) {
stateStorage = memoryStateStorageWithEviction {
ttlAfterLastAccess = 2.hours
}
}
// Use the rate limiter.
val taken: Boolean = rateLimiter.tryTake("myKey")
import com.neutrine.krate.rateLimiter
// Create a rate limiter with a rate of 5 per second. Unused keys will be evicted after 2 hours.
val rateLimiter = rateLimiter(maxRate = 10) {
stateStorage = memoryCaffeineStateStorage {
expireAfterAccess = 2.hours
}
}
// Use the rate limiter.
val taken: Boolean = rateLimiter.tryTake("myKey")
// Create a rate limiter with a rate of 5 per second and redis.
val rateLimiter = rateLimiter(maxRate = 5) {
stateStorage = redisStateStorage {
host = "my-custom-uri.endpoint"
}
}
// Use the rate limiter.
val taken: Boolean = rateLimiter.tryTake()