Skip to content

Commit

Permalink
Backport of issue #1021
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Halfar committed Aug 30, 2024
1 parent 7a2ccef commit 75e4baf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
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(mixed $content): mixed

Check failure on line 71 in src/Model/Entity/Panel.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

ReservedWord

src/Model/Entity/Panel.php:71:53: ReservedWord: mixed is a reserved word (see https://psalm.dev/095)
{
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

0 comments on commit 75e4baf

Please sign in to comment.