Skip to content

Commit c61c873

Browse files
authored
Limit connections from the same place (#56)
* connections * Update fly.toml * update jdk
1 parent fba28ca commit c61c873

File tree

8 files changed

+46
-28
lines changed

8 files changed

+46
-28
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Setup Java
2323
uses: actions/setup-java@v1
2424
with:
25-
java-version: "19"
25+
java-version: "20"
2626
java-package: jdk
2727
architecture: x64
2828

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM amazoncorretto:19-alpine-jdk as jreBuilder
1+
FROM amazoncorretto:20-alpine-jdk as jreBuilder
22

33
RUN apk add binutils
44
RUN jlink \

fly.toml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
# fly.toml file generated for manga-spring on 2022-08-25T19:39:19-04:00
1+
# fly.toml app configuration file generated for manga-spring on 2023-05-26T14:02:59-04:00
2+
#
3+
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
4+
#
25

36
app = "manga-spring"
7+
primary_region = "iad"
48
kill_signal = "SIGINT"
5-
kill_timeout = 5
6-
processes = []
7-
8-
[env]
9+
kill_timeout = "5s"
910

1011
[experimental]
11-
allowed_public_ports = []
1212
auto_rollback = true
1313

1414
[[services]]
15-
http_checks = []
15+
protocol = "tcp"
1616
internal_port = 8080
1717
processes = ["app"]
18-
protocol = "tcp"
19-
script_checks = []
20-
[services.concurrency]
21-
hard_limit = 25
22-
soft_limit = 20
23-
type = "connections"
2418

2519
[[services.ports]]
26-
force_https = true
27-
handlers = ["http"]
2820
port = 80
21+
handlers = ["http"]
22+
force_https = true
2923

3024
[[services.ports]]
31-
handlers = ["tls", "http"]
3225
port = 443
26+
handlers = ["tls", "http"]
27+
[services.concurrency]
28+
type = "connections"
29+
hard_limit = 25
30+
soft_limit = 20
3331

3432
[[services.tcp_checks]]
35-
grace_period = "1s"
3633
interval = "15s"
37-
restart_limit = 0
3834
timeout = "2s"
35+
grace_period = "1s"
36+
restart_limit = 0

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<name>MangaBackend</name>
1414
<description>RSocket API for Manga-Spring Site</description>
1515
<properties>
16-
<java.version>19</java.version>
17-
<avaje.inject.version>8.12</avaje.inject.version>
16+
<java.version>20</java.version>
17+
<avaje.inject.version>9.2</avaje.inject.version>
1818
<mainClass>com.mangasite.MangaBackendApplication</mainClass>
1919
<start-class>com.mangasite.MangaBackendApplication</start-class>
2020
</properties>

src/main/java/com/mangasite/record/DeviceInfo.java renamed to src/main/java/com/mangasite/records/DeviceInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mangasite.record;
1+
package com.mangasite.records;
22

33
import com.fasterxml.jackson.annotation.JsonAlias;
44
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

src/main/java/com/mangasite/record/ServerMessage.java renamed to src/main/java/com/mangasite/records/ServerMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package com.mangasite.record;
1+
package com.mangasite.records;
22

33
public record ServerMessage(String message) {}

src/main/java/com/mangasite/services/ConnectService.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

77
import java.time.Duration;
88
import java.time.Instant;
9+
import java.util.HashMap;
10+
import java.util.Map;
911
import java.util.Map.Entry;
1012

1113
import org.springframework.http.MediaType;
1214
import org.springframework.messaging.rsocket.RSocketRequester;
1315

1416
import com.mangasite.domain.MangaChapters;
15-
import com.mangasite.record.DeviceInfo;
16-
import com.mangasite.record.ServerMessage;
17+
import com.mangasite.records.DeviceInfo;
18+
import com.mangasite.records.ServerMessage;
1719

1820
import io.rsocket.RSocket;
21+
import io.rsocket.exceptions.RejectedSetupException;
1922
import reactor.core.publisher.Flux;
2023
import reactor.core.publisher.Mono;
2124

@@ -29,6 +32,7 @@ public final class ConnectService {
2932
private ConnectService() {}
3033

3134
private static final String CLIENT = "Client: ";
35+
private static final Map<String, Integer> maxNumberOfConnections = new HashMap<>();
3236

3337
/**
3438
* Logs Rsocket Connect/Disconnect events
@@ -38,6 +42,15 @@ private ConnectService() {}
3842
*/
3943
public static void startConnectionLog(RSocketRequester rSocketRequester, String clientName) {
4044
final var startTime = Instant.now();
45+
46+
var sameClientConnections =
47+
maxNumberOfConnections.compute(clientName, (k, v) -> v == null ? 0 : v + 1);
48+
49+
if (sameClientConnections > 1) {
50+
throw new RejectedSetupException(
51+
"too many connections from the same place, leave some for the rest of us");
52+
}
53+
4154
rSocketRequester
4255
.rsocketClient()
4356
.source()
@@ -60,6 +73,13 @@ public static void startConnectionLog(RSocketRequester rSocketRequester, String
6073
"Total Remaining Connections: " + ACTIVE_CONNECTIONS.decrementAndGet());
6174
CLIENT_REQUESTER_MAP.remove(clientName);
6275
CLIENT_MANGA_MAP.remove(clientName);
76+
var frequency = maxNumberOfConnections.get(clientName);
77+
78+
if (frequency == 0) {
79+
maxNumberOfConnections.remove(clientName);
80+
} else {
81+
maxNumberOfConnections.put(clientName, frequency - 1);
82+
}
6383
})
6484
.subscribe(
6585
null,

src/main/resources/META-INF/native-image/reflect-config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,15 @@
379379
"allPublicConstructors": true
380380
},
381381
{
382-
"name": "com.mangasite.record.DeviceInfo",
382+
"name": "com.mangasite.records.DeviceInfo",
383383
"allDeclaredFields": true,
384384
"allDeclaredConstructors": true,
385385
"allPublicConstructors": true,
386386
"allDeclaredMethods": true,
387387
"allPublicMethods": true
388388
},
389389
{
390-
"name": "com.mangasite.record.ServerMessage",
390+
"name": "com.mangasite.records.ServerMessage",
391391
"allDeclaredFields": true,
392392
"allDeclaredMethods": true,
393393
"allDeclaredConstructors": true,

0 commit comments

Comments
 (0)