Skip to content

Commit 359d309

Browse files
committed
HistoryController: tweak buffering for stream
1 parent 47609a4 commit 359d309

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

application/controllers/HistoryController.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,48 @@ protected function sendHistory(string $table, string $tsColumn)
117117

118118
protected function sendQueryResultAsResponse(Select $query)
119119
{
120-
$firstRow = true;
121120
$statement = $this->db()->query($query);
121+
$this->getViewRenderer()->disable();
122+
$firstRow = true;
123+
$cnt = 0;
124+
$flushes = 0;
125+
$strings = [];
122126
while ($row = $statement->fetch()) {
123127
unset($row->activities);
124128
if ($firstRow) {
125129
$this->sendJsonResponseHeaders();
126130
echo '{ "objects": [';
131+
if (! ob_get_level()) {
132+
ob_start();
133+
}
127134
$firstRow = false;
128-
} else {
129-
echo ", ";
130135
}
131-
echo JsonString::encode(
136+
$strings[] = JsonString::encode(
132137
SerializationHelper::serializeProperties((array)$row),
133138
JSON_PRETTY_PRINT | JSON_INVALID_UTF8_SUBSTITUTE
134139
);
140+
$cnt++;
141+
if ($cnt === 100) {
142+
if ($flushes > 0) {
143+
echo ', ';
144+
}
145+
echo implode(', ', $strings);
146+
$cnt = 0;
147+
$strings = [];
148+
$flushes++;
149+
ob_end_flush();
150+
ob_start();
151+
}
135152
}
136153
if ($firstRow) {
137154
$this->sendJsonResponseHeaders();
138155
echo '{ "objects": []}' . "\n";
139156
} else {
140157
echo "]}\n";
141158
}
142-
$this->getViewRenderer()->disable();
159+
if (ob_get_level()) {
160+
ob_end_flush();
161+
}
143162
exit;
144163
}
145164
protected function prepareQueryString(

0 commit comments

Comments
 (0)