RateControl { public void validateRate(String group, String clientIP) { logger.debug("acquire, group={}, clientIP={}", group, clientIP); boolean acquired = acquire(group, clientIP); if (!acquired) { throw new TooManyRequestsException("rate exceeded"); } } RateControl(int maxEntries); void config(String group, int maxPermits, int fillRate, TimeUnit unit); void validateRate(String group, String clientIP); public Map<String, RateConfig> config; }
@Test void validateRate() { RateControl control = new RateControl(1); control.config("group", 1, 1, TimeUnit.DAYS); control.validateRate("group", "10.0.0.1"); assertThatThrownBy(() -> control.validateRate("group", "10.0.0.1")) .isInstanceOf(TooManyRequestsException.class) .hasMessageContaining("exceeded"); }