Skip to content

Commit 92f852a

Browse files
authored
Merge pull request #320 from FlowFuse/319-flush-batching
Batch requests to flush context to backend
2 parents 3b93b2c + 1fb1371 commit 92f852a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/context/FFContextStorage.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,30 @@ class FFContextStorage {
111111
}
112112
}
113113
// update _flushPendingWrites to a real function
114-
self._flushPendingWrites = function () {
114+
self._flushPendingWrites = async function () {
115115
const scopes = Object.keys(self.pendingWrites)
116116
self.pendingWrites = {}
117-
const promises = []
118117
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+
}
130137
delete self._pendingWriteTimeout
131-
return Promise.all(promises)
132138
}
133139
}
134140

0 commit comments

Comments
 (0)