-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor WebSocket message handling, introduce rate limiters fo…
…r API requests and add new WS messages
- Loading branch information
Showing
56 changed files
with
739 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
common/src/main/java/dev/lotnest/sequoia/core/http/HttpClients.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright © sequoia-mod 2025. | ||
* This file is released under LGPLv3. See LICENSE for full license details. | ||
*/ | ||
package dev.lotnest.sequoia.core.http; | ||
|
||
import dev.lotnest.sequoia.core.http.clients.MojangApiHttpClient; | ||
import dev.lotnest.sequoia.core.http.clients.WynncraftApiHttpClient; | ||
|
||
public final class HttpClients { | ||
public static final WynncraftApiHttpClient WYNNCRAFT_API = WynncraftApiHttpClient.newHttpClient(); | ||
public static final MojangApiHttpClient MOJANG_API = MojangApiHttpClient.newHttpClient(); | ||
|
||
private HttpClients() {} | ||
} |
43 changes: 43 additions & 0 deletions
43
common/src/main/java/dev/lotnest/sequoia/core/http/clients/MojangApiHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright © sequoia-mod 2025. | ||
* This file is released under LGPLv3. See LICENSE for full license details. | ||
*/ | ||
package dev.lotnest.sequoia.core.http.clients; | ||
|
||
import dev.lotnest.sequoia.core.http.HttpClient; | ||
import dev.lotnest.sequoia.core.http.ratelimiter.RateLimiters; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class MojangApiHttpClient extends HttpClient { | ||
private MojangApiHttpClient() { | ||
super(); | ||
} | ||
|
||
public static MojangApiHttpClient newHttpClient() { | ||
return new MojangApiHttpClient(); | ||
} | ||
|
||
@Override | ||
public HttpResponse<String> get(String url) { | ||
RateLimiters.MOJANG_API.acquire(); | ||
return super.get(url); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<HttpResponse<String>> getAsync(String url) { | ||
return CompletableFuture.runAsync(RateLimiters.MOJANG_API::acquire).thenCompose(v -> super.getAsync(url)); | ||
} | ||
|
||
@Override | ||
public HttpResponse<String> post(String url, String body) { | ||
RateLimiters.MOJANG_API.acquire(); | ||
return super.post(url, body); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<HttpResponse<String>> postAsync(String url, String body) { | ||
return CompletableFuture.runAsync(RateLimiters.MOJANG_API::acquire) | ||
.thenCompose(v -> super.postAsync(url, body)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
common/src/main/java/dev/lotnest/sequoia/core/http/clients/WynncraftApiHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright © sequoia-mod 2025. | ||
* This file is released under LGPLv3. See LICENSE for full license details. | ||
*/ | ||
package dev.lotnest.sequoia.core.http.clients; | ||
|
||
import dev.lotnest.sequoia.core.http.HttpClient; | ||
import dev.lotnest.sequoia.core.http.ratelimiter.RateLimiters; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class WynncraftApiHttpClient extends HttpClient { | ||
private WynncraftApiHttpClient() { | ||
super(); | ||
} | ||
|
||
public static WynncraftApiHttpClient newHttpClient() { | ||
return new WynncraftApiHttpClient(); | ||
} | ||
|
||
@Override | ||
public HttpResponse<String> get(String url) { | ||
RateLimiters.WYNNCRAFT_API.acquire(); | ||
return super.get(url); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<HttpResponse<String>> getAsync(String url) { | ||
return CompletableFuture.runAsync(RateLimiters.WYNNCRAFT_API::acquire).thenCompose(v -> super.getAsync(url)); | ||
} | ||
|
||
@Override | ||
public HttpResponse<String> post(String url, String body) { | ||
RateLimiters.WYNNCRAFT_API.acquire(); | ||
return super.post(url, body); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<HttpResponse<String>> postAsync(String url, String body) { | ||
return CompletableFuture.runAsync(RateLimiters.WYNNCRAFT_API::acquire) | ||
.thenCompose(v -> super.postAsync(url, body)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
common/src/main/java/dev/lotnest/sequoia/core/http/ratelimiter/RateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright © sequoia-mod 2025. | ||
* This file is released under LGPLv3. See LICENSE for full license details. | ||
*/ | ||
package dev.lotnest.sequoia.core.http.ratelimiter; | ||
|
||
import dev.lotnest.sequoia.SequoiaMod; | ||
import java.util.concurrent.Semaphore; | ||
|
||
public class RateLimiter { | ||
private final Semaphore concurrencySemaphore; | ||
private final double capacity; | ||
private double tokens; | ||
private final double refillRate; | ||
private long lastRefillTime; | ||
|
||
public RateLimiter(int maxConcurrentRequests, double requestsPerMinute) { | ||
this.concurrencySemaphore = new Semaphore(maxConcurrentRequests); | ||
this.capacity = requestsPerMinute; | ||
this.tokens = requestsPerMinute; | ||
this.refillRate = requestsPerMinute / 60000.0; | ||
this.lastRefillTime = System.currentTimeMillis(); | ||
} | ||
|
||
private void waitForToken() throws InterruptedException { | ||
while (true) { | ||
long sleepTime; | ||
synchronized (this) { | ||
refillTokens(); | ||
if (tokens >= 1) { | ||
tokens -= 1; | ||
return; | ||
} | ||
double missingTokens = 1 - tokens; | ||
sleepTime = (long) Math.ceil(missingTokens / refillRate); | ||
} | ||
Thread.sleep(sleepTime); | ||
} | ||
} | ||
|
||
private synchronized void refillTokens() { | ||
long now = System.currentTimeMillis(); | ||
long elapsed = now - lastRefillTime; | ||
if (elapsed > 0) { | ||
tokens = Math.min(capacity, tokens + elapsed * refillRate); | ||
lastRefillTime = now; | ||
} | ||
} | ||
|
||
/** | ||
* Acquires permission for a request. This call blocks until both a token is available | ||
* (ensuring we respect the global rate) and a concurrency permit is available (ensuring | ||
* only a limited number of concurrent requests). | ||
*/ | ||
public void acquire() { | ||
try { | ||
waitForToken(); | ||
concurrencySemaphore.acquire(); | ||
} catch (InterruptedException exception) { | ||
Thread.currentThread().interrupt(); | ||
SequoiaMod.warn("Interrupted while waiting for rate limiter", exception); | ||
} | ||
} | ||
|
||
/** | ||
* Releases a previously acquired concurrency permit. | ||
*/ | ||
public void release() { | ||
concurrencySemaphore.release(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
common/src/main/java/dev/lotnest/sequoia/core/http/ratelimiter/RateLimiters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright © sequoia-mod 2025. | ||
* This file is released under LGPLv3. See LICENSE for full license details. | ||
*/ | ||
package dev.lotnest.sequoia.core.http.ratelimiter; | ||
|
||
public final class RateLimiters { | ||
public static final RateLimiter WYNNCRAFT_API = new RateLimiter(5, 180); | ||
public static final RateLimiter MOJANG_API = new RateLimiter(5, 60); | ||
|
||
private RateLimiters() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
common/src/main/java/dev/lotnest/sequoia/core/websocket/WSConstants.java
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
...v/lotnest/sequoia/core/websocket/messages/discordchatbridge/SChannelMessageWSMessage.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.