Reedy has uploaded a new change for review.
https://gerrit.wikimedia.org/r/298380
Change subject: Split PoolCounter_ConnectionManager into seperate file
......................................................................
Split PoolCounter_ConnectionManager into seperate file
Change-Id: I5b375819cfe10b577aba63646a98dc76042a7bd5
---
M PoolCounterClient_body.php
A PoolCounter_ConnectionManager.php
M extension.json
3 files changed, 109 insertions(+), 108 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PoolCounter
refs/changes/80/298380/1
diff --git a/PoolCounterClient_body.php b/PoolCounterClient_body.php
index 18e080a..4e76bac 100644
--- a/PoolCounterClient_body.php
+++ b/PoolCounterClient_body.php
@@ -1,112 +1,5 @@
<?php
-class PoolCounter_ConnectionManager {
- public $hostNames;
- public $conns = [];
- public $refCounts = [];
-
- /**
- * @param array $conf
- * @throws MWException
- */
- function __construct( $conf ) {
- $this->hostNames = $conf['servers'];
- $this->timeout = isset( $conf['timeout'] ) ? $conf['timeout'] :
0.1;
- $this->connect_timeout = isset( $conf['connect_timeout'] ) ?
- $conf['connect_timeout'] : 0;
- if ( !count( $this->hostNames ) ) {
- throw new MWException( __METHOD__ . ': no servers
configured' );
- }
- }
-
- /**
- * @param $key
- * @return Status
- */
- function get( $key ) {
- $hashes = [];
- foreach ( $this->hostNames as $hostName ) {
- $hashes[$hostName] = md5( $hostName . $key );
- }
- asort( $hashes );
- $errno = $errstr = '';
- $conn = null;
- foreach ( $hashes as $hostName => $hash ) {
- if ( isset( $this->conns[$hostName] ) ) {
- $this->refCounts[$hostName]++;
- return Status::newGood( $this->conns[$hostName]
);
- }
- $parts = explode( ':', $hostName, 2 );
- if ( count( $parts ) < 2 ) {
- $parts[] = 7531;
- }
- MediaWiki\suppressWarnings();
- $conn = $this->open( $parts[0], $parts[1], $errno,
$errstr );
- MediaWiki\restoreWarnings();
- if ( $conn ) {
- break;
- }
- }
- if ( !$conn ) {
- return Status::newFatal(
'poolcounter-connection-error', $errstr );
- }
- wfDebug( "Connected to pool counter server: $hostName\n" );
- $this->conns[$hostName] = $conn;
- $this->refCounts[$hostName] = 1;
- return Status::newGood( $conn );
- }
-
- /**
- * Open a socket. Just a wrapper for fsockopen()
- * @param string $host
- * @param int $port
- * @param $errno
- * @param $errstr
- * @return null|resource
- */
- private function open( $host, $port, &$errno, &$errstr ) {
- // If connect_timeout is set, we try to open the socket twice.
- // You usually want to set the connection timeout to a very
- // small value so that in case of failure of a server the
- // connection to poolcounter is not a SPOF.
- if ( $this->connect_timeout > 0 ) {
- $tries = 2;
- $timeout = $this->connect_timeout;
- } else {
- $tries = 1;
- $timeout = $this->timeout;
- }
-
- $fp = null;
- while ( true ) {
- $fp = fsockopen( $host, $port, $errno, $errstr,
$timeout );
- if ( $fp !== false || --$tries < 1 ) {
- break;
- }
- usleep( 1000 );
- }
-
- return $fp;
- }
-
- /**
- * @param $conn
- */
- function close( $conn ) {
- foreach ( $this->conns as $hostName => $otherConn ) {
- if ( $conn === $otherConn ) {
- if ( $this->refCounts[$hostName] ) {
- $this->refCounts[$hostName]--;
- }
- if ( !$this->refCounts[$hostName] ) {
- fclose( $conn );
- unset( $this->conns[$hostName] );
- }
- }
- }
- }
-}
-
class PoolCounter_Client extends PoolCounter {
/**
* @var resource the socket connection to the poolcounter. Closing this
diff --git a/PoolCounter_ConnectionManager.php
b/PoolCounter_ConnectionManager.php
new file mode 100644
index 0000000..a1f5b62
--- /dev/null
+++ b/PoolCounter_ConnectionManager.php
@@ -0,0 +1,108 @@
+<?php
+
+class PoolCounter_ConnectionManager {
+ public $hostNames;
+ public $conns = [];
+ public $refCounts = [];
+
+ /**
+ * @param array $conf
+ * @throws MWException
+ */
+ function __construct( $conf ) {
+ $this->hostNames = $conf['servers'];
+ $this->timeout = isset( $conf['timeout'] ) ? $conf['timeout'] :
0.1;
+ $this->connect_timeout = isset( $conf['connect_timeout'] ) ?
+ $conf['connect_timeout'] : 0;
+ if ( !count( $this->hostNames ) ) {
+ throw new MWException( __METHOD__ . ': no servers
configured' );
+ }
+ }
+
+ /**
+ * @param $key
+ * @return Status
+ */
+ function get( $key ) {
+ $hashes = [];
+ foreach ( $this->hostNames as $hostName ) {
+ $hashes[$hostName] = md5( $hostName . $key );
+ }
+ asort( $hashes );
+ $errno = $errstr = '';
+ $conn = null;
+ foreach ( $hashes as $hostName => $hash ) {
+ if ( isset( $this->conns[$hostName] ) ) {
+ $this->refCounts[$hostName]++;
+ return Status::newGood( $this->conns[$hostName]
);
+ }
+ $parts = explode( ':', $hostName, 2 );
+ if ( count( $parts ) < 2 ) {
+ $parts[] = 7531;
+ }
+ MediaWiki\suppressWarnings();
+ $conn = $this->open( $parts[0], $parts[1], $errno,
$errstr );
+ MediaWiki\restoreWarnings();
+ if ( $conn ) {
+ break;
+ }
+ }
+ if ( !$conn ) {
+ return Status::newFatal(
'poolcounter-connection-error', $errstr );
+ }
+ wfDebug( "Connected to pool counter server: $hostName\n" );
+ $this->conns[$hostName] = $conn;
+ $this->refCounts[$hostName] = 1;
+ return Status::newGood( $conn );
+ }
+
+ /**
+ * Open a socket. Just a wrapper for fsockopen()
+ * @param string $host
+ * @param int $port
+ * @param $errno
+ * @param $errstr
+ * @return null|resource
+ */
+ private function open( $host, $port, &$errno, &$errstr ) {
+ // If connect_timeout is set, we try to open the socket twice.
+ // You usually want to set the connection timeout to a very
+ // small value so that in case of failure of a server the
+ // connection to poolcounter is not a SPOF.
+ if ( $this->connect_timeout > 0 ) {
+ $tries = 2;
+ $timeout = $this->connect_timeout;
+ } else {
+ $tries = 1;
+ $timeout = $this->timeout;
+ }
+
+ $fp = null;
+ while ( true ) {
+ $fp = fsockopen( $host, $port, $errno, $errstr,
$timeout );
+ if ( $fp !== false || --$tries < 1 ) {
+ break;
+ }
+ usleep( 1000 );
+ }
+
+ return $fp;
+ }
+
+ /**
+ * @param $conn
+ */
+ function close( $conn ) {
+ foreach ( $this->conns as $hostName => $otherConn ) {
+ if ( $conn === $otherConn ) {
+ if ( $this->refCounts[$hostName] ) {
+ $this->refCounts[$hostName]--;
+ }
+ if ( !$this->refCounts[$hostName] ) {
+ fclose( $conn );
+ unset( $this->conns[$hostName] );
+ }
+ }
+ }
+ }
+}
diff --git a/extension.json b/extension.json
index c1f325d..a9722b4 100644
--- a/extension.json
+++ b/extension.json
@@ -12,7 +12,7 @@
},
"AutoloadClasses": {
"PoolCounter_Client": "PoolCounterClient_body.php",
- "PoolCounter_ConnectionManager": "PoolCounterClient_body.php"
+ "PoolCounter_ConnectionManager":
"PoolCounter_ConnectionManager.php"
},
"config": {
"PoolCountClientConf": {
--
To view, visit https://gerrit.wikimedia.org/r/298380
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5b375819cfe10b577aba63646a98dc76042a7bd5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PoolCounter
Gerrit-Branch: master
Gerrit-Owner: Reedy <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits