@@ -71,10 +71,8 @@ const app = websockify(new Koa());
71
71
72
72
app . ws . use ( ( ctx : Koa . Context , next : Koa . Next ) => {
73
73
const connectionId = subscriptionService . generateId ( ) ;
74
- const requestId = uuid ( ) ;
75
74
ctx . websocket . id = connectionId ;
76
- ctx . websocket . requestId = requestId ;
77
- return context . run ( { requestId, connectionId } , next ) ;
75
+ next ( ) ;
78
76
} ) ;
79
77
80
78
app . ws . use ( async ( ctx : Koa . Context ) => {
@@ -86,12 +84,6 @@ app.ws.use(async (ctx: Koa.Context) => {
86
84
ctx . websocket . limiter = limiter ;
87
85
ctx . websocket . wsMetricRegistry = wsMetricRegistry ;
88
86
89
- const requestDetails = new RequestDetails ( {
90
- requestId : ctx . websocket . requestId ,
91
- ipAddress : ctx . request . ip ,
92
- connectionId : ctx . websocket . id ,
93
- } ) ;
94
-
95
87
logger . info (
96
88
// @ts -ignore
97
89
`New connection established. Current active connections: ${ ctx . app . server . _connections } ` ,
@@ -115,9 +107,17 @@ app.ws.use(async (ctx: Koa.Context) => {
115
107
limiter . applyLimits ( ctx ) ;
116
108
117
109
// 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 ( ) => {
121
121
// Increment the total messages counter for each message received
122
122
wsMetricRegistry . getCounter ( 'totalMessageCounter' ) . inc ( ) ;
123
123
@@ -225,8 +225,8 @@ app.ws.use(async (ctx: Koa.Context) => {
225
225
// Update the connection duration histogram with the calculated duration
226
226
const methodLabel = Array . isArray ( request ) ? WS_CONSTANTS . BATCH_REQUEST_METHOD_NAME : request . method ;
227
227
wsMetricRegistry . getHistogram ( 'messageDuration' ) . labels ( methodLabel ) . observe ( msgDurationInMiliSeconds ) ;
228
- } ) ,
229
- ) ;
228
+ } ) ;
229
+ } ) ;
230
230
231
231
if ( pingInterval > 0 ) {
232
232
setInterval ( async ( ) => {
0 commit comments