66
77import java .time .Duration ;
88import java .time .Instant ;
9+ import java .util .HashMap ;
10+ import java .util .Map ;
911import java .util .Map .Entry ;
1012
1113import org .springframework .http .MediaType ;
1214import org .springframework .messaging .rsocket .RSocketRequester ;
1315
1416import 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
1820import io .rsocket .RSocket ;
21+ import io .rsocket .exceptions .RejectedSetupException ;
1922import reactor .core .publisher .Flux ;
2023import 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 ,
0 commit comments