PrepZone Logo
PrepZone
Back to AI Coding

AI Coding · Medium

Review this AI-generated in-memory rate limiter used by many request threads.

AI CodingCode ReviewSenior
java
public class RateLimiter {
    private final Map<String, Integer> counts = new HashMap<>();
    private final int limit;

    public RateLimiter(int limit) { this.limit = limit; }

    public boolean allow(String clientId) {
        int current = counts.getOrDefault(clientId, 0);
        if (current >= limit) {
            return false;
        }
        counts.put(clientId, current + 1);
        return true;
    }
}