@@ -2,7 +2,6 @@ import { createReadStream } from "fs";
2
2
import { NextResponse } from "next/server" ;
3
3
import readline from "readline" ;
4
4
5
- import { getSetSimulationMaxTime } from "@/app/queries" ;
6
5
import { IServerMessage } from "@/components/Graph/types" ;
7
6
import { ISimulationAggregatedDataState } from "@/contexts/GraphContext/types" ;
8
7
import { messagesPath } from "../../utils" ;
@@ -78,7 +77,7 @@ export async function GET(req: Request, res: Response) {
78
77
let interval : Timer | undefined ;
79
78
const eventBuffer : string [ ] = [ ] ;
80
79
let simulationDone = false ;
81
- const maxTime = await getSetSimulationMaxTime ( ) ;
80
+ let isProcessing = false ;
82
81
83
82
const stream = new ReadableStream ( {
84
83
cancel ( ) {
@@ -94,6 +93,11 @@ export async function GET(req: Request, res: Response) {
94
93
} ;
95
94
96
95
interval = setInterval ( ( ) => {
96
+ if ( isProcessing ) {
97
+ return ;
98
+ }
99
+
100
+ isProcessing = true ;
97
101
if ( eventBuffer . length === 0 && simulationDone ) {
98
102
clearInterval ( interval ) ;
99
103
rl . close ( ) ;
@@ -104,7 +108,7 @@ export async function GET(req: Request, res: Response) {
104
108
105
109
try {
106
110
// Process 10k events at a time.
107
- const batch = eventBuffer . splice ( 0 , 10000 ) ;
111
+ const batch = eventBuffer . splice ( 0 , 100000 ) ;
108
112
for ( const line of batch ) {
109
113
const data : IServerMessage = JSON . parse ( line ) ;
110
114
processMessage ( data , aggregatedData ) ;
@@ -117,9 +121,12 @@ export async function GET(req: Request, res: Response) {
117
121
} ;
118
122
119
123
controller . enqueue ( `data: ${ JSON . stringify ( serializedData ) } \n\n` ) ;
124
+ isProcessing = false ;
120
125
} catch ( e ) {
121
126
controller . error ( e ) ;
127
+ isProcessing = false ;
122
128
}
129
+
123
130
} , 100 ) ;
124
131
125
132
rl . on ( "line" , ( line : string ) => {
0 commit comments