Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of issue #1021 #1023

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Model/Entity/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Panel extends Entity
protected $_hidden = ['content'];

/**
* Read the stream contents.
* Read the stream contents or inflate deflated data.
*
* Over certain sizes PDO will return file handles.
* For backwards compatibility and consistency we smooth over that difference here.
Expand All @@ -47,7 +47,34 @@ class Panel extends Entity
protected function _getContent($content)
{
if (is_resource($content)) {
return stream_get_contents($content);
$content = (string)stream_get_contents($content);
}

if (is_string($content) && function_exists('gzinflate')) {
// phpcs:disable
$contentInflated = @gzinflate($content);
// phpcs:enable
if ($contentInflated !== false) {
return $contentInflated;
}
}

return $content;
}

/**
* Deflate the string data before saving it into database
*
* @param mixed $content Content
* @return mixed
*/
protected function _setContent($content)
{
if (is_string($content) && function_exists('gzdeflate')) {
$contentDeflated = gzdeflate($content, 9);
if ($contentDeflated !== false) {
$content = $contentDeflated;
}
}

return $content;
Expand Down
36 changes: 34 additions & 2 deletions src/Model/Table/RequestsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace DebugKit\Model\Table;

use Cake\Core\Configure;
Expand Down Expand Up @@ -86,7 +87,17 @@ public function findRecent(Query $query, array $options)
*/
protected function shouldGc()
{
return rand(1, 100) === 100;
return rand(1, 10) === 10;
}

/**
* Check if garbage collection vacuum should be run
*
* @return bool
*/
protected function shouldGcVacuum(): bool
{
return rand(1, 10) === 10;
}

/**
Expand Down Expand Up @@ -131,7 +142,28 @@ public function gc()

$conn = $this->getConnection();
if ($conn->getDriver() instanceof Sqlite) {
$conn->execute('VACUUM;');
$conn->execute('
PRAGMA auto_vacuum = FULL;
PRAGMA journal_mode = OFF;
PRAGMA synchronous = OFF;
PRAGMA foreign_keys = OFF;
PRAGMA temp_store = MEMORY;
PRAGMA automatic_index = OFF;
');

if (!$this->shouldGcVacuum()) {
return;
}

try {
$conn->execute('VACUUM;');
} catch (PDOException $e) {
Log::warning(
'Unable to run VACUUM on debug kit SQLite database. ' .
'Please manually remove the database file'
);
Log::warning((string)$e);
}
}
} catch (PDOException $e) {
Log::warning('Unable to garbage collect requests table. This is probably due to concurrent requests.');
Expand Down
Loading