Skip to content

Commit

Permalink
PR review, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel committed Jan 29, 2025
1 parent 87f6e97 commit 191df34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -78,7 +76,7 @@ private static class RequestCounter {

RequestCounter(long timestamp) {
this.timestamp = timestamp;
this.count = 1;
this.count = 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 191df34

Please sign in to comment.