poggit / libasynql

Asynchronous MySQL access library for PocketMine plugins.
https://poggit.github.io/libasynql
Apache License 2.0
132 stars 44 forks source link

Sync initial connection can cause *very* long waits #60

Open JaxkDev opened 3 years ago

JaxkDev commented 3 years ago

Describe the bug During initial construction of the libasynql if you have a internet connection issue or DB issue you can expect waits of up to 3 minutes PER plugin that uses libasynql.

Sample code

$this->db = libasynql::create($plugin, @yaml_parse_file($plugin->getDataFolder()."mysql.yml"), [
    "sqlite" => "sqlite.sql",
    "mysql" => "mysql.sql"
]);

Expected behavior Smaller timeout or connection made async (not sure how its done backend)

Environment OS: Windows 10 PocketMine version: 3.19.0 PHP: 8.0.3

Log

[17:22:26] [Server thread/INFO]: Loading FusionCore v1.0.0
[17:22:26] [Asynchronous Worker #0 thread/DEBUG]: Set memory limit to 256 MB
status
[17:24:49] [Server thread/CRITICAL]: FusionCraft\Core\vendor9a5a9370\poggit\libasynql\SqlError: "SQL CONNECT error: MySQL server has gone away" (EXCEPTION) in "plugins/FusionCore.phar/src/FusionCraft/Core/vendor9a5a9370/p
oggit/libasynql/libasynql" at line 141
[17:24:49] [Server thread/CRITICAL]: #0 plugins/FusionCore.phar/src/FusionCraft/Core/SharedDatabase(29): FusionCraft\Core\vendor9a5a9370\poggit\libasynql\libasynql::create(string[31] object(FusionCraft\Core\Plugin), array
[4], array[2])
[17:24:49] [Server thread/CRITICAL]: #1 plugins/FusionCore.phar/src/FusionCraft/Core/Plugin(24): FusionCraft\Core\SharedDatabase->__construct(string[31] object(FusionCraft\Core\Plugin))
[17:24:49] [Server thread/CRITICAL]: #2 pmsrc/src/pocketmine/plugin/PluginManager(182): FusionCraft\Core\Plugin->onLoad()
[17:24:49] [Server thread/CRITICAL]: #3 pmsrc/src/pocketmine/plugin/PluginManager(346): pocketmine\plugin\PluginManager->loadPlugin(string[81] C:\Users\Jack\Documents\PocketMine\FusionCraft-PMMP3-PHP8\plugins\FusionCore.p
ha, array[2])
[17:24:49] [Server thread/CRITICAL]: #4 pmsrc/src/pocketmine/Server(1529): pocketmine\plugin\PluginManager->loadPlugins(string[66] C:\Users\Jack\Documents\PocketMine\FusionCraft-PMMP3-PHP8\plugins\)
[17:24:49] [Server thread/CRITICAL]: #5 pmsrc/src/pocketmine/PocketMine(291): pocketmine\Server->__construct(string[23] object(BaseClassLoader), string[35] object(pocketmine\utils\MainLogger), string[58] C:\Users\Jack\Doc
uments\PocketMine\FusionCraft-PMMP3-PHP8\, string[66] C:\Users\Jack\Documents\PocketMine\FusionCraft-PMMP3-PHP8\plugins\)
[17:24:49] [Server thread/CRITICAL]: #6 pmsrc/src/pocketmine/PocketMine(321): pocketmine\server()
[17:24:49] [Server thread/CRITICAL]: #7 pmsrc(11): require(string[113] phar://C:/Users/Jack/Documents/PocketMine/FusionCraft-PMMP3-PHP8/PocketMine-MP.p)
[17:24:49] [Server thread/CRITICAL]: Could not load plugin 'FusionCore'

Take note of timestamps, if several plugins were to have libasynql and they all failed like above during initial connection that could be an extremely long wait before the user can interact with the server (stop command etc)

SOF3 commented 3 years ago

Seems to be this loop https://github.com/poggit/libasynql/blob/50ac73e295869f97a46fad1d55af749608558f6e/libasynql/src/poggit/libasynql/libasynql.php#L137-L139

Could make this async indeed.

Endermanbugzjfc commented 3 years ago

Yes, I used to have a plugin that attempt to reconnect the database every 5 minutes if the connection failed has on enable. And it really did make people think I'm running queries on main thread oof.

JaxkDev commented 3 years ago

Not sure if this is directly related but I'm seeing very long waits on close() as well.

[17:53:10] [Server thread/INFO]: Disabling FusionCore v1.0.0
[17:53:13] [RakLibServer thread/DEBUG]: Closed session for 172.20.10.1 52534
[17:53:13] [RakLibServer thread/DEBUG]: Ignored connected packet from 172.20.10.1 52534 due to no session opened (0xc0)
[17:54:24] [Server thread/INFO]: JaxkDev left the game
[17:54:25] [Server thread/INFO]: JaxkDev[/172.20.10.1:52534] logged out due to Server closed
[17:54:25] [Server thread/DEBUG]: Unloading all worlds
[17:54:25] [Server thread/INFO]: Unloading world "world"
[17:54:25] [Server thread/DEBUG]: Removing event handlers

1m 25s to close, the only thing called on disable is this: $this->db->close(); where $this->db is the data connector from the return of libasynql::create()