mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 16:51:42 +00:00
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.
This commit is contained in:
parent
1d5c741f28
commit
af80aefd45
@ -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)){
|
||||
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user