@@ -71,10 +71,8 @@ const app = websockify(new Koa());
7171
7272app . ws . use ( ( ctx : Koa . Context , next : Koa . Next ) => {
7373 const connectionId = subscriptionService . generateId ( ) ;
74- const requestId = uuid ( ) ;
7574 ctx . websocket . id = connectionId ;
76- ctx . websocket . requestId = requestId ;
77- return context . run ( { requestId, connectionId } , next ) ;
75+ next ( ) ;
7876} ) ;
7977
8078app . ws . use ( async ( ctx : Koa . Context ) => {
@@ -86,12 +84,6 @@ app.ws.use(async (ctx: Koa.Context) => {
8684 ctx . websocket . limiter = limiter ;
8785 ctx . websocket . wsMetricRegistry = wsMetricRegistry ;
8886
89- const requestDetails = new RequestDetails ( {
90- requestId : ctx . websocket . requestId ,
91- ipAddress : ctx . request . ip ,
92- connectionId : ctx . websocket . id ,
93- } ) ;
94-
9587 logger . info (
9688 // @ts -ignore
9789 `New connection established. Current active connections: ${ ctx . app . server . _connections } ` ,
@@ -115,9 +107,17 @@ app.ws.use(async (ctx: Koa.Context) => {
115107 limiter . applyLimits ( ctx ) ;
116108
117109 // listen on message event
118- ctx . websocket . on (
119- 'message' ,
120- AsyncResource . bind ( async ( msg ) => {
110+ ctx . websocket . on ( 'message' , async ( msg ) => {
111+ const requestId = uuid ( ) ;
112+ ctx . websocket . requestId = requestId ;
113+
114+ const requestDetails = new RequestDetails ( {
115+ requestId,
116+ ipAddress : ctx . request . ip ,
117+ connectionId : ctx . websocket . id ,
118+ } ) ;
119+
120+ context . run ( { requestId, connectionId : requestDetails . connectionId ! } , async ( ) => {
121121 // Increment the total messages counter for each message received
122122 wsMetricRegistry . getCounter ( 'totalMessageCounter' ) . inc ( ) ;
123123
@@ -225,8 +225,8 @@ app.ws.use(async (ctx: Koa.Context) => {
225225 // Update the connection duration histogram with the calculated duration
226226 const methodLabel = Array . isArray ( request ) ? WS_CONSTANTS . BATCH_REQUEST_METHOD_NAME : request . method ;
227227 wsMetricRegistry . getHistogram ( 'messageDuration' ) . labels ( methodLabel ) . observe ( msgDurationInMiliSeconds ) ;
228- } ) ,
229- ) ;
228+ } ) ;
229+ } ) ;
230230
231231 if ( pingInterval > 0 ) {
232232 setInterval ( async ( ) => {
0 commit comments