From af80aefd458f64aa68fc4dcadf8483659264c790 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 12 Jul 2018 19:31:00 +0100 Subject: [PATCH] Remove async config save (#2298) As discussed in #2297: Honestly I don't see a fit purpose for async saving at all. It should either always be synchronous or always asynchronous, and at the user's own option. However, this isn't currently possible because Config doesn't enable you to get the serialized content without writing it to disk. Consider the following code: ```php for($i = 0, $size = $this->getServer()->getAsyncPool()->getSize(); $i < $size; ++$i){ $this->getServer()->getAsyncPool()->submitTask(new class extends AsyncTask{ public function onRun(){ sleep(5); } }); } $config = $this->getConfig(); $config->set("steve", "hi"); $config->save(true); $config->set("steve", "bye"); $config->save(false); ``` Output: ```yml --- steve: hi ... ``` Expected output: ```yml --- steve: bye ... ``` Additionally, if your configs are causing you performance issues when you're saving, it's a clear sign that a) you're saving too much b) you're abusing configs and should consider using a database. Configs should be used for _simple_ data which does not change much. Configuration is such that the _user_ is expected to be able to modify it. As such, it should never be an issue to save synchronously. In the future, something like ReactPHP may be introduced to allow proper async saving. When this happens, async saving would always be sequential but non blocking. Using threads for this makes no sense. --- src/pocketmine/Server.php | 6 +++--- src/pocketmine/utils/Config.php | 11 ++--------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 94b4305f1..e1fc23261 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -1300,7 +1300,7 @@ class Server{ if(($player = $this->getPlayerExact($name)) !== null){ $player->recalculatePermissions(); } - $this->operators->save(true); + $this->operators->save(); } /** @@ -1320,7 +1320,7 @@ class Server{ */ public function addWhitelist(string $name){ $this->whitelist->set(strtolower($name), true); - $this->whitelist->save(true); + $this->whitelist->save(); } /** @@ -1691,7 +1691,7 @@ class Server{ } if($this->properties->hasChanged()){ - $this->properties->save(true); + $this->properties->save(); } if(!($this->getDefaultLevel() instanceof Level)){ diff --git a/src/pocketmine/utils/Config.php b/src/pocketmine/utils/Config.php index c8609afdc..30ab3dadd 100644 --- a/src/pocketmine/utils/Config.php +++ b/src/pocketmine/utils/Config.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace pocketmine\utils; -use pocketmine\scheduler\FileWriteTask; use pocketmine\Server; @@ -187,11 +186,9 @@ class Config{ } /** - * @param bool $async - * * @return bool */ - public function save(bool $async = false) : bool{ + public function save() : bool{ if($this->correct){ try{ $content = null; @@ -216,11 +213,7 @@ class Config{ throw new \InvalidStateException("Config type is unknown, has not been set or not detected"); } - if($async){ - Server::getInstance()->getAsyncPool()->submitTask(new FileWriteTask($this->file, $content)); - }else{ - file_put_contents($this->file, $content); - } + file_put_contents($this->file, $content); }catch(\Throwable $e){ $logger = Server::getInstance()->getLogger(); $logger->critical("Could not save Config " . $this->file . ": " . $e->getMessage());