Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/main/java/hse/java/lectures/lesson7/limiter/RateLimiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,36 @@ public class RateLimiter {
* (скользящее окно 1 секунда или 1 минута)
* @param maxRequests максимум успешных {@link #check()} за окно (должно быть > 0)
*/

long[] times;
long window;
int maxRequests, head, size;

public RateLimiter(ChronoUnit unit, int maxRequests) {
throw new UnsupportedOperationException("Not implemented");
if (maxRequests <= 0) throw new IllegalArgumentException();
if (unit != ChronoUnit.SECONDS && unit != ChronoUnit.MINUTES) throw new IllegalArgumentException();
this.maxRequests = maxRequests;
times = new long[maxRequests];
window = unit == ChronoUnit.MINUTES ? 60000L : 1000L;
}

/**
* Регистрирует попытку и возвращает, разрешена ли она в пределах лимита.
*/
public boolean check() {
throw new UnsupportedOperationException("Not implemented");
long now = System.currentTimeMillis();
while (size > 0 && now - times[head] >= window) {
if (++head == maxRequests) head = 0;
size -= 1;
}

if (size == maxRequests) return false;

int position = head + size;
if (position >= maxRequests) position -= maxRequests;
times[position] = now;
size += 1;
return true;
}

}
31 changes: 31 additions & 0 deletions src/test/java/hse/java/lectures/lesson7/RateLimiterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hse.java.lectures.lesson7.limiter;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("limiter")
public class RateLimiterTest {
@Test
void badArgs() {
assertThrows(IllegalArgumentException.class, () -> new RateLimiter(null, 1));
assertThrows(IllegalArgumentException.class, () -> new RateLimiter(ChronoUnit.HOURS, 1));
assertThrows(IllegalArgumentException.class, () -> new RateLimiter(ChronoUnit.SECONDS, 0));
assertThrows(IllegalArgumentException.class, () -> new RateLimiter(ChronoUnit.MINUTES, -1));
}

@Test
void floatingSecond() throws Exception {
RateLimiter limiter = new RateLimiter(ChronoUnit.SECONDS, 2);
assertTrue(limiter.check());
Thread.sleep(700);
assertTrue(limiter.check());
Thread.sleep(400);
assertTrue(limiter.check());
assertFalse(limiter.check());
}

}
Loading