@@ -19,11 +19,7 @@ import { WebsocketServerTransport } from './transport/WebSocketServerTransport.j
1919import { errors , logger } from '@powersync/lib-services-framework' ;
2020
2121export class ReactiveSocketRouter < C > {
22- protected activeConnections : number ;
23-
24- constructor ( protected options ?: ReactiveSocketRouterOptions < C > ) {
25- this . activeConnections = 0 ;
26- }
22+ constructor ( protected options ?: ReactiveSocketRouterOptions < C > ) { }
2723
2824 reactiveStream < I , O > ( path : string , stream : IReactiveStreamInput < I , O , C > ) : IReactiveStream < I , O , C > {
2925 return {
@@ -60,11 +56,16 @@ export class ReactiveSocketRouter<C> {
6056 acceptor : {
6157 accept : async ( payload ) => {
6258 const { max_concurrent_connections } = this . options ?? { } ;
63- if ( max_concurrent_connections && this . activeConnections >= max_concurrent_connections ) {
64- throw new errors . JourneyError ( {
65- code : '429' ,
59+ // wss.clients.size includes this connection, so we check for greater than
60+ // TODO: Share connection limit between this and http stream connections
61+ if ( max_concurrent_connections && wss . clients . size > max_concurrent_connections ) {
62+ const err = new errors . JourneyError ( {
63+ status : 429 ,
64+ code : 'SERVER_BUSY' ,
6665 description : `Maximum active concurrent connections limit has been reached`
6766 } ) ;
67+ logger . warn ( err ) ;
68+ throw err ;
6869 }
6970
7071 // Throwing an exception in this context will be returned to the client side request
@@ -80,16 +81,14 @@ export class ReactiveSocketRouter<C> {
8081 requestStream : ( payload , initialN , responder ) => {
8182 const observer = new SocketRouterObserver ( ) ;
8283
84+ // TODO: Consider limiting the number of active streams per connection to prevent abuse
8385 handleReactiveStream ( context , { payload, initialN, responder } , observer , params ) . catch ( ( ex ) => {
8486 logger . error ( ex ) ;
8587 responder . onError ( ex ) ;
8688 responder . onComplete ( ) ;
8789 } ) ;
88-
89- this . activeConnections ++ ;
9090 return {
9191 cancel : ( ) => {
92- this . activeConnections -- ;
9392 observer . triggerCancel ( ) ;
9493 } ,
9594 onExtension : ( ) => observer . triggerExtension ( ) ,
0 commit comments