Skip to content

Commit

Permalink
[HttpKernel] FileProfilerStorage remove expired profiles mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
alamirault authored and nicolas-grekas committed Dec 16, 2022
1 parent 68725da commit 600596f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate parameters `container.dumper.inline_factories` and `container.dumper.inline_class_loader`, use `.container.dumper.inline_factories` and `.container.dumper.inline_class_loader` instead
* `FileProfilerStorage` removes profiles automatically after one day

6.2
---
Expand Down
Expand Up @@ -165,11 +165,15 @@ public function write(Profile $profile): bool
$profile->getIp(),
$profile->getMethod(),
$profile->getUrl(),
$profile->getTime(),
$profile->getTime() ?: time(),
$profile->getParentToken(),
$profile->getStatusCode(),
]);
fclose($file);

if (1 === mt_rand(1, 10)) {
$this->removeExpiredProfiles();
}
}

return true;
Expand Down Expand Up @@ -289,4 +293,29 @@ private function doRead($token, Profile $profile = null): ?Profile

return $this->createProfileFromData($token, $data, $profile);
}

private function removeExpiredProfiles()
{
$minimalProfileTimestamp = time() - 2 * 86400;
$file = $this->getIndexFilename();
$handle = fopen($file, 'r');

if ($offset = is_file($file.'.offset') ? (int) file_get_contents($file.'.offset') : 0) {
fseek($handle, $offset);
}

while ($line = fgets($handle)) {
[$csvToken, , , , $csvTime] = str_getcsv($line);

if ($csvTime >= $minimalProfileTimestamp) {
break;
}

@unlink($this->getFilename($csvToken));
$offset += \strlen($line);
}
fclose($handle);

file_put_contents($file.'.offset', $offset);
}
}
Expand Up @@ -344,6 +344,57 @@ public function testMultiRowIndexFile()
$this->assertFalse(fgetcsv($handle));
}

/**
* @dataProvider provideExpiredProfiles
*/
public function testRemoveExpiredProfiles(string $index, string $expectedOffset)
{
$file = $this->tmpDir.'/index.csv';
file_put_contents($file, $index);

$r = new \ReflectionMethod($this->storage, 'removeExpiredProfiles');
$r->invoke($this->storage);

$this->assertSame($expectedOffset, file_get_contents($this->tmpDir.'/index.csv.offset'));
}

public static function provideExpiredProfiles()
{
$oneHourAgo = new \DateTimeImmutable('-1 hour');

yield 'One unexpired profile' => [
<<<CSV
token0,127.0.0.0,,http://foo.bar/0,{$oneHourAgo->getTimestamp()},,
CSV,
'0',
];

$threeDaysAgo = new \DateTimeImmutable('-3 days');

yield 'One expired profile' => [
<<<CSV
token0,127.0.0.0,,http://foo.bar/0,{$threeDaysAgo->getTimestamp()},,
CSV,
'48',
];

$fourDaysAgo = new \DateTimeImmutable('-4 days');
$threeDaysAgo = new \DateTimeImmutable('-3 days');
$oneHourAgo = new \DateTimeImmutable('-1 hour');

yield 'Multiple expired profiles' => [
<<<CSV
token0,127.0.0.0,,http://foo.bar/0,{$fourDaysAgo->getTimestamp()},,
token1,127.0.0.1,,http://foo.bar/1,{$threeDaysAgo->getTimestamp()},,
token2,127.0.0.2,,http://foo.bar/2,{$oneHourAgo->getTimestamp()},,
CSV,
'96',
];
}

public function testReadLineFromFile()
{
$r = new \ReflectionMethod($this->storage, 'readLineFromFile');
Expand Down

0 comments on commit 600596f

Please sign in to comment.