From a8ec2f8daf4ed06fbea09b215b5f0b27e71a113a Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 8 Oct 2015 00:36:22 -0700 Subject: [PATCH] Move SquidPurgeClient under /clientpool Each class has its own file now too. Change-Id: I11593d6efbfce8e3981895e84edb4e0dea3998e4 --- autoload.php | 4 +- .../{ => clientpool}/SquidPurgeClient.php | 88 -------------- includes/clientpool/SquidPurgeClientPool.php | 108 ++++++++++++++++++ 3 files changed, 110 insertions(+), 90 deletions(-) rename includes/{ => clientpool}/SquidPurgeClient.php (80%) create mode 100644 includes/clientpool/SquidPurgeClientPool.php diff --git a/autoload.php b/autoload.php index a2f432f6fd..269b1427d6 100644 --- a/autoload.php +++ b/autoload.php @@ -1212,8 +1212,8 @@ $wgAutoloadLocalClasses = array( 'SqliteInstaller' => __DIR__ . '/includes/installer/SqliteInstaller.php', 'SqliteMaintenance' => __DIR__ . '/maintenance/sqlite.php', 'SqliteUpdater' => __DIR__ . '/includes/installer/SqliteUpdater.php', - 'SquidPurgeClient' => __DIR__ . '/includes/SquidPurgeClient.php', - 'SquidPurgeClientPool' => __DIR__ . '/includes/SquidPurgeClient.php', + 'SquidPurgeClient' => __DIR__ . '/includes/clientpool/SquidPurgeClient.php', + 'SquidPurgeClientPool' => __DIR__ . '/includes/clientpool/SquidPurgeClientPool.php', 'SquidUpdate' => __DIR__ . '/includes/deferred/SquidUpdate.php', 'SrConverter' => __DIR__ . '/languages/classes/LanguageSr.php', 'StatsOutput' => __DIR__ . '/maintenance/language/StatOutputs.php', diff --git a/includes/SquidPurgeClient.php b/includes/clientpool/SquidPurgeClient.php similarity index 80% rename from includes/SquidPurgeClient.php rename to includes/clientpool/SquidPurgeClient.php index ca8f11aef1..91100e9270 100644 --- a/includes/SquidPurgeClient.php +++ b/includes/clientpool/SquidPurgeClient.php @@ -394,91 +394,3 @@ class SquidPurgeClient { wfDebugLog( 'squid', __CLASS__ . " ($this->host): $msg" ); } } - -class SquidPurgeClientPool { - /** @var array Array of SquidPurgeClient */ - protected $clients = array(); - - /** @var int */ - protected $timeout = 5; - - /** - * @param array $options - */ - function __construct( $options = array() ) { - if ( isset( $options['timeout'] ) ) { - $this->timeout = $options['timeout']; - } - } - - /** - * @param SquidPurgeClient $client - * @return void - */ - public function addClient( $client ) { - $this->clients[] = $client; - } - - public function run() { - $done = false; - $startTime = microtime( true ); - while ( !$done ) { - $readSockets = $writeSockets = array(); - /** - * @var $client SquidPurgeClient - */ - foreach ( $this->clients as $clientIndex => $client ) { - $sockets = $client->getReadSocketsForSelect(); - foreach ( $sockets as $i => $socket ) { - $readSockets["$clientIndex/$i"] = $socket; - } - $sockets = $client->getWriteSocketsForSelect(); - foreach ( $sockets as $i => $socket ) { - $writeSockets["$clientIndex/$i"] = $socket; - } - } - if ( !count( $readSockets ) && !count( $writeSockets ) ) { - break; - } - $exceptSockets = null; - $timeout = min( $startTime + $this->timeout - microtime( true ), 1 ); - MediaWiki\suppressWarnings(); - $numReady = socket_select( $readSockets, $writeSockets, $exceptSockets, $timeout ); - MediaWiki\restoreWarnings(); - if ( $numReady === false ) { - wfDebugLog( 'squid', __METHOD__ . ': Error in stream_select: ' . - socket_strerror( socket_last_error() ) . "\n" ); - break; - } - // Check for timeout, use 1% tolerance since we aimed at having socket_select() - // exit at precisely the overall timeout - if ( microtime( true ) - $startTime > $this->timeout * 0.99 ) { - wfDebugLog( 'squid', __CLASS__ . ": timeout ({$this->timeout}s)\n" ); - break; - } elseif ( !$numReady ) { - continue; - } - - foreach ( $readSockets as $key => $socket ) { - list( $clientIndex, ) = explode( '/', $key ); - $client = $this->clients[$clientIndex]; - $client->doReads(); - } - foreach ( $writeSockets as $key => $socket ) { - list( $clientIndex, ) = explode( '/', $key ); - $client = $this->clients[$clientIndex]; - $client->doWrites(); - } - - $done = true; - foreach ( $this->clients as $client ) { - if ( !$client->isIdle() ) { - $done = false; - } - } - } - foreach ( $this->clients as $client ) { - $client->close(); - } - } -} diff --git a/includes/clientpool/SquidPurgeClientPool.php b/includes/clientpool/SquidPurgeClientPool.php new file mode 100644 index 0000000000..feb80df9f8 --- /dev/null +++ b/includes/clientpool/SquidPurgeClientPool.php @@ -0,0 +1,108 @@ +timeout = $options['timeout']; + } + } + + /** + * @param SquidPurgeClient $client + * @return void + */ + public function addClient( $client ) { + $this->clients[] = $client; + } + + public function run() { + $done = false; + $startTime = microtime( true ); + while ( !$done ) { + $readSockets = $writeSockets = array(); + /** + * @var $client SquidPurgeClient + */ + foreach ( $this->clients as $clientIndex => $client ) { + $sockets = $client->getReadSocketsForSelect(); + foreach ( $sockets as $i => $socket ) { + $readSockets["$clientIndex/$i"] = $socket; + } + $sockets = $client->getWriteSocketsForSelect(); + foreach ( $sockets as $i => $socket ) { + $writeSockets["$clientIndex/$i"] = $socket; + } + } + if ( !count( $readSockets ) && !count( $writeSockets ) ) { + break; + } + $exceptSockets = null; + $timeout = min( $startTime + $this->timeout - microtime( true ), 1 ); + MediaWiki\suppressWarnings(); + $numReady = socket_select( $readSockets, $writeSockets, $exceptSockets, $timeout ); + MediaWiki\restoreWarnings(); + if ( $numReady === false ) { + wfDebugLog( 'squid', __METHOD__ . ': Error in stream_select: ' . + socket_strerror( socket_last_error() ) . "\n" ); + break; + } + // Check for timeout, use 1% tolerance since we aimed at having socket_select() + // exit at precisely the overall timeout + if ( microtime( true ) - $startTime > $this->timeout * 0.99 ) { + wfDebugLog( 'squid', __CLASS__ . ": timeout ({$this->timeout}s)\n" ); + break; + } elseif ( !$numReady ) { + continue; + } + + foreach ( $readSockets as $key => $socket ) { + list( $clientIndex, ) = explode( '/', $key ); + $client = $this->clients[$clientIndex]; + $client->doReads(); + } + foreach ( $writeSockets as $key => $socket ) { + list( $clientIndex, ) = explode( '/', $key ); + $client = $this->clients[$clientIndex]; + $client->doWrites(); + } + + $done = true; + foreach ( $this->clients as $client ) { + if ( !$client->isIdle() ) { + $done = false; + } + } + } + foreach ( $this->clients as $client ) { + $client->close(); + } + } +} -- 2.20.1