@@ -51,8 +51,8 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
51
51
private currentClient : WebSocketEx | undefined ;
52
52
private subscriptionNames = new Map < string , string > ( ) ;
53
53
private queue = Promise . resolve ( ) ;
54
- private mostRecentBatchTimestamp = new Date ( ) ;
55
- private mostRecentACKTimestamp = new Date ( ) ;
54
+ private mostRecentCompletedBatchTimestamp = new Date ( ) ;
55
+ private mostRecentDispatchedBatchTimestamp = new Date ( ) ;
56
56
57
57
constructor (
58
58
protected readonly logger : Logger ,
@@ -134,16 +134,9 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
134
134
135
135
// Record metrics
136
136
this . metrics . setEventBatchSize ( batch . events . length ) ;
137
- let timestamp = new Date ( ) ;
138
- this . logger . log (
139
- 'Recording batch interval of ' +
140
- ( timestamp . getTime ( ) - this . mostRecentBatchTimestamp . getTime ( ) ) +
141
- ' milliseconds' ,
142
- ) ;
143
- this . metrics . observeBatchInterval (
144
- timestamp . getTime ( ) - this . mostRecentBatchTimestamp . getTime ( ) ,
145
- ) ;
146
- this . mostRecentBatchTimestamp = timestamp ;
137
+ let batchIntervalMs = new Date ( ) . getTime ( ) - this . mostRecentCompletedBatchTimestamp . getTime ( ) ;
138
+ this . logger . log ( `Recording batch interval of ${ batchIntervalMs } milliseconds` ) ;
139
+ this . metrics . observeBatchInterval ( batchIntervalMs ) ;
147
140
148
141
const messages : WebSocketMessage [ ] = [ ] ;
149
142
for ( const event of batch . events ) {
@@ -176,6 +169,10 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
176
169
} ;
177
170
this . awaitingAck . push ( message ) ;
178
171
this . currentClient ?. send ( JSON . stringify ( message ) ) ;
172
+
173
+ // Set the most-recent batch dispatch time to now so when the next ACK comes back from FF
174
+ // we can set metrics accordingly
175
+ this . mostRecentDispatchedBatchTimestamp = new Date ( ) ;
179
176
}
180
177
181
178
private async getSubscriptionName ( ctx : Context , subId : string ) {
@@ -210,16 +207,10 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
210
207
return ;
211
208
}
212
209
213
- let timestamp = new Date ( ) ;
214
- this . logger . log (
215
- 'Recording batch ACK interval of ' +
216
- ( timestamp . getTime ( ) - this . mostRecentACKTimestamp . getTime ( ) ) +
217
- ' milliseconds' ,
218
- ) ;
219
- this . metrics . observeBatchAckInterval (
220
- timestamp . getTime ( ) - this . mostRecentACKTimestamp . getTime ( ) ,
221
- ) ;
222
- this . mostRecentACKTimestamp = timestamp ;
210
+ let timeWaitingForACKms =
211
+ new Date ( ) . getTime ( ) - this . mostRecentDispatchedBatchTimestamp . getTime ( ) ;
212
+ this . logger . log ( `Recording batch ACK interval of ${ timeWaitingForACKms } milliseconds` ) ;
213
+ this . metrics . observeBatchAckInterval ( timeWaitingForACKms ) ;
223
214
224
215
const inflight = this . awaitingAck . find ( msg => msg . id === data . id ) ;
225
216
this . logger . log ( `Received ack ${ data . id } inflight=${ ! ! inflight } ` ) ;
@@ -237,5 +228,9 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
237
228
this . socket . ack ( inflight . batchNumber ) ;
238
229
}
239
230
}
231
+
232
+ // Set the most-recent batch time to now - so when the next batch comes we can calculate
233
+ // time between sending our ACK to the current batch and receiving the new one
234
+ this . mostRecentCompletedBatchTimestamp = new Date ( ) ;
240
235
}
241
236
}
0 commit comments