Skip to content

Commit

Permalink
Use pmmp/Snooze instead of pinging threads every tick
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed May 14, 2020
1 parent 0b12eb2 commit e862a0e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/tebexio/pocketmine/BaseTebexAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ abstract class BaseTebexAPI{
/** @var SSLConfiguration */
private $ssl_config;

public function __construct(TebexPlugin $plugin, string $secret, SSLConfiguration $ssl_config, int $workers){
$this->pool = new TebexThreadPool($plugin);
public function __construct(string $secret, SSLConfiguration $ssl_config, int $workers){
$this->pool = new TebexThreadPool();
$this->ssl_config = $ssl_config;
for($i = 0; $i < $workers; $i++){
$this->pool->addWorker(new TebexThread($secret, $ssl_config));
$this->pool->addWorker(new TebexThread($this->pool->getNotifier(), $secret, $ssl_config));
}
$this->pool->start();
}
Expand Down
2 changes: 1 addition & 1 deletion src/tebexio/pocketmine/TebexPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setSecret(string $secret) : TebexInformation{
/** @var TebexInformation|TebexException $result */
$result = null;

$api = new TebexAPI($this, $secret, SSLConfiguration::recommended(), $this->worker_limit);
$api = new TebexAPI($secret, SSLConfiguration::recommended(), $this->worker_limit);
$api->getInformation(new TebexResponseHandler(
static function(TebexInformation $information) use(&$result) : void{ $result = $information; },
static function(TebexException $e) use(&$result) : void{ $result = $e; }
Expand Down
12 changes: 9 additions & 3 deletions src/tebexio/pocketmine/thread/TebexThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tebexio\pocketmine\thread;

use pocketmine\snooze\SleeperNotifier;
use function is_string;
use Logger;
use pocketmine\utils\MainLogger;
Expand All @@ -30,6 +31,9 @@ final class TebexThread extends Thread{
/** @var int */
private static $handler_ids = 0;

/** @var SleeperNotifier */
private $notifier;

/** @var Threaded<string> */
private $incoming;

Expand All @@ -51,7 +55,8 @@ final class TebexThread extends Thread{
/** @var string */
private $ca_path;

public function __construct(string $secret, SSLConfiguration $ssl_config){
public function __construct(SleeperNotifier $notifier, string $secret, SSLConfiguration $ssl_config){
$this->notifier = $notifier;
$this->ca_path = $ssl_config->getCAInfoPath();
$this->incoming = new Threaded();
$this->outgoing = new Threaded();
Expand All @@ -65,7 +70,7 @@ public function push(TebexRequest $request, TebexResponseHandler $handler) : voi
self::$handlers[$handler_id] = $handler;
++$this->busy_score;
$this->synchronized(function() : void{
$this->notify();
$this->notifyOne();
});
}

Expand Down Expand Up @@ -145,6 +150,7 @@ public function run() : void{
$this->outgoing[] = igbinary_serialize($response_holder);
}

$this->notifier->wakeupSleeper();
$this->sleep();
}
}
Expand All @@ -170,7 +176,7 @@ public function stop() : void{
*
* @return Generator<float>
*/
public function collect() : Generator{
public function collectPending() : Generator{
while(($holder = $this->outgoing->shift()) !== null){
/** @var TebexResponseHolder $holder */
$holder = igbinary_unserialize($holder);
Expand Down
17 changes: 11 additions & 6 deletions src/tebexio/pocketmine/thread/TebexThreadPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace tebexio\pocketmine\thread;

use tebexio\pocketmine\TebexPlugin;
use pocketmine\scheduler\ClosureTask;
use pocketmine\Server;
use pocketmine\snooze\SleeperNotifier;
use UnderflowException;

final class TebexThreadPool{
Expand All @@ -16,12 +16,17 @@ final class TebexThreadPool{
/** @var float */
private $latency = 0.0;

public function __construct(TebexPlugin $plugin){
$plugin->getScheduler()->scheduleRepeatingTask(new ClosureTask(function(int $currentTick) : void{
public function __construct(){
$this->notifier = new SleeperNotifier();
Server::getInstance()->getTickSleeper()->addNotifier($this->notifier, function() : void{
foreach($this->workers as $thread){
$this->collectThread($thread);
}
}), 1);
});
}

public function getNotifier() : SleeperNotifier{
return $this->notifier;
}

/**
Expand Down Expand Up @@ -78,7 +83,7 @@ public function waitAll(int $sleep_duration_ms) : void{
* @param TebexThread<mixed> $thread
*/
private function collectThread(TebexThread $thread) : void{
foreach($thread->collect() as $latency){
foreach($thread->collectPending() as $latency){
$this->latency = $latency;
}
}
Expand Down

0 comments on commit e862a0e

Please sign in to comment.