From 49f28e922aa1b68284093f7a11e6ac86875c0217 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 18 Jun 2023 19:27:52 +0300 Subject: [PATCH] Add mutex for saving process --- composer.json | 1 + src/Debugger.php | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d68c1e75..fc324147 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "yiisoft/di": "^1.0", "yiisoft/files": "^2.0", "yiisoft/json": "^1.0", + "yiisoft/mutex": "^1.1", "yiisoft/profiler": "^3.0", "yiisoft/proxy": "^1.0.1", "yiisoft/strings": "^2.0", diff --git a/src/Debugger.php b/src/Debugger.php index bc3bebbb..18f98008 100644 --- a/src/Debugger.php +++ b/src/Debugger.php @@ -5,6 +5,7 @@ namespace Yiisoft\Yii\Debug; use Psr\Http\Message\ServerRequestInterface; +use Yiisoft\Mutex\Synchronizer; use Yiisoft\Strings\WildcardPattern; use Yiisoft\Yii\Console\Event\ApplicationStartup; use Yiisoft\Yii\Debug\Collector\CollectorInterface; @@ -13,12 +14,15 @@ final class Debugger { + public const SAVING_MUTEX_NAME = self::class; + private bool $skipCollect = false; private bool $active = false; public function __construct( private DebuggerIdGenerator $idGenerator, private StorageInterface $target, + private Synchronizer $synchronizer, /** * @var CollectorInterface[] */ @@ -64,7 +68,9 @@ public function shutdown(): void try { if (!$this->skipCollect) { - $this->target->flush(); + $this->synchronizer->execute(self::SAVING_MUTEX_NAME . $this->idGenerator->getId(), function () { + $this->target->flush(); + }); } } finally { foreach ($this->collectors as $collector) {