@@ -111,24 +111,30 @@ class FFContextStorage {
111
111
}
112
112
}
113
113
// update _flushPendingWrites to a real function
114
- self . _flushPendingWrites = function ( ) {
114
+ self . _flushPendingWrites = async function ( ) {
115
115
const scopes = Object . keys ( self . pendingWrites )
116
116
self . pendingWrites = { }
117
- const promises = [ ]
118
117
const newContext = self . cache . _export ( )
119
- scopes . forEach ( function ( scope ) {
120
- const context = newContext [ scope ] || { }
121
- const stringifiedContext = stringify ( context )
122
- if ( stringifiedContext . circular && ! self . knownCircularRefs [ scope ] ) {
123
- console . warn ( 'context.flowforge.error-circular' , scope )
124
- self . knownCircularRefs [ scope ] = true
125
- } else {
126
- delete self . knownCircularRefs [ scope ]
127
- }
128
- promises . push ( self . _writeCache ( scope , stringifiedContext . json ) )
129
- } )
118
+ // Batch requests into a maximum of 5 at a time to avoid contention
119
+ // on the file-server
120
+ const chunkSize = 5
121
+ while ( scopes . length > 0 ) {
122
+ // Grab a chunk of the pending scopes
123
+ const batch = scopes . splice ( 0 , chunkSize )
124
+ // For each one, trigger the flush call and await all in the batch to complete
125
+ await Promise . all ( batch . map ( ( scope ) => {
126
+ const context = newContext [ scope ] || { }
127
+ const stringifiedContext = stringify ( context )
128
+ if ( stringifiedContext . circular && ! self . knownCircularRefs [ scope ] ) {
129
+ console . warn ( 'context.flowforge.error-circular' , scope )
130
+ self . knownCircularRefs [ scope ] = true
131
+ } else {
132
+ delete self . knownCircularRefs [ scope ]
133
+ }
134
+ return self . _writeCache ( scope , stringifiedContext . json )
135
+ } ) )
136
+ }
130
137
delete self . _pendingWriteTimeout
131
- return Promise . all ( promises )
132
138
}
133
139
}
134
140
0 commit comments