From 191df34815eae1ee53be9b29229eb4aef6032997 Mon Sep 17 00:00:00 2001 From: Nicolas Quinquenel Date: Wed, 29 Jan 2025 11:21:10 +0100 Subject: [PATCH] PR review, fix tests --- .../core/embedded/server/RateLimitFilter.java | 14 ++++++-------- .../java/mediumtest/EmbeddedServerMediumTests.java | 11 ++++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/RateLimitFilter.java b/backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/RateLimitFilter.java index 808e31a251..310f248cce 100644 --- a/backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/RateLimitFilter.java +++ b/backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/RateLimitFilter.java @@ -58,18 +58,16 @@ public void handle(ClassicHttpRequest request, HttpFilterChain.ResponseTrigger r private boolean isRequestAllowed(String origin) { long currentTime = System.currentTimeMillis(); var counter = requestCounters.computeIfAbsent(origin, k -> new RequestCounter(currentTime)); - synchronized (counter) { + requestCounters.compute(origin, (k, v) -> { if (currentTime - counter.timestamp > TIME_FRAME_MS) { counter.timestamp = currentTime; counter.count = 1; - return true; - } else if (counter.count < MAX_REQUESTS_PER_ORIGIN) { - counter.count++; - return true; } else { - return false; + counter.count++; } - } + return counter; + }); + return counter.count <= MAX_REQUESTS_PER_ORIGIN; } private static class RequestCounter { @@ -78,7 +76,7 @@ private static class RequestCounter { RequestCounter(long timestamp) { this.timestamp = timestamp; - this.count = 1; + this.count = 0; } } diff --git a/medium-tests/src/test/java/mediumtest/EmbeddedServerMediumTests.java b/medium-tests/src/test/java/mediumtest/EmbeddedServerMediumTests.java index 7660c2719c..17768307d5 100644 --- a/medium-tests/src/test/java/mediumtest/EmbeddedServerMediumTests.java +++ b/medium-tests/src/test/java/mediumtest/EmbeddedServerMediumTests.java @@ -25,6 +25,7 @@ import java.net.http.HttpResponse; import java.time.Duration; import java.util.List; +import org.apache.commons.lang.RandomStringUtils; import org.eclipse.jetty.http.HttpStatus; import org.sonarsource.sonarlint.core.test.utils.junit5.SonarLintTest; import org.sonarsource.sonarlint.core.test.utils.junit5.SonarLintTestHarness; @@ -153,9 +154,9 @@ void it_should_rate_limit_origin_if_too_many_requests(SonarLintTestHarness harne var embeddedServerPort = backend.getEmbeddedServerPort(); var request = HttpRequest.newBuilder() .uri(URI.create("http://localhost:" + embeddedServerPort + "/sonarlint/api/status")) - .header("Origin", "https://sonar") + .header("Origin", RandomStringUtils.randomAlphabetic(10)) .GET().build(); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 15; i++) { java.net.http.HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); } var response = java.net.http.HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); @@ -189,12 +190,12 @@ void it_should_not_rate_limit_over_time(SonarLintTestHarness harness) throws IOE var embeddedServerPort = backend.getEmbeddedServerPort(); var request = HttpRequest.newBuilder() .uri(URI.create("http://localhost:" + embeddedServerPort + "/sonarlint/api/status")) - .header("Origin", "https://sonar") + .header("Origin", RandomStringUtils.randomAlphabetic(10)) .GET().build(); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 15; i++) { java.net.http.HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); } - await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { + await().atMost(Duration.ofSeconds(15)).untilAsserted(() -> { var response = java.net.http.HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); assertThat(response.statusCode()).isEqualTo(HttpStatus.OK_200); });