| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\PoolCounter; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Status\Status; |
| 25 |
use Wikimedia\IPUtils; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class PoolCounterConnectionManager { |
| 34 |
|
| 35 |
public $hostNames; |
| 36 |
|
| 37 |
public $conns = []; |
| 38 |
|
| 39 |
public $refCounts = []; |
| 40 |
|
| 41 |
public $timeout; |
| 42 |
|
| 43 |
public $connect_timeout; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public $host; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public $port; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct( $conf ) { |
| 61 |
if ( !count( $conf['servers'] ) ) { |
| 62 |
throw new InvalidArgumentException( __METHOD__ . ': no servers configured' ); |
| 63 |
} |
| 64 |
$this->hostNames = $conf['servers']; |
| 65 |
$this->timeout = $conf['timeout'] ?? 0.1; |
| 66 |
$this->connect_timeout = $conf['connect_timeout'] ?? 0; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function get( $key ) { |
| 74 |
$hashes = []; |
| 75 |
foreach ( $this->hostNames as $hostName ) { |
| 76 |
$hashes[$hostName] = md5( $hostName . $key ); |
| 77 |
} |
| 78 |
asort( $hashes ); |
| 79 |
$errno = 0; |
| 80 |
$errstr = ''; |
| 81 |
$hostName = ''; |
| 82 |
$conn = null; |
| 83 |
foreach ( $hashes as $hostName => $hash ) { |
| 84 |
if ( isset( $this->conns[$hostName] ) ) { |
| 85 |
$this->refCounts[$hostName]++; |
| 86 |
return Status::newGood( |
| 87 |
[ 'conn' => $this->conns[$hostName], 'hostName' => $hostName ] ); |
| 88 |
} |
| 89 |
$parts = IPUtils::splitHostAndPort( $hostName ); |
| 90 |
if ( $parts === false ) { |
| 91 |
$errstr = '\'servers\' config incorrectly configured.'; |
| 92 |
return Status::newFatal( 'poolcounter-connection-error', $errstr, $hostName ); |
| 93 |
} |
| 94 |
|
| 95 |
$this->host = IPUtils::isValidIPv6( $parts[0] ) ? '[' . $parts[0] . ']' : $parts[0]; |
| 96 |
$this->port = $parts[1] ?: 7531; |
| 97 |
|
| 98 |
$conn = @$this->open( $this->host, $this->port, $errno, $errstr ); |
| 99 |
if ( $conn ) { |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
if ( !$conn ) { |
| 104 |
return Status::newFatal( 'poolcounter-connection-error', $errstr, $hostName ); |
| 105 |
} |
| 106 |
|
| 107 |
wfDebug( "Connected to pool counter server: $hostName\n" ); |
| 108 |
$this->conns[$hostName] = $conn; |
| 109 |
$this->refCounts[$hostName] = 1; |
| 110 |
return Status::newGood( [ 'conn' => $conn, 'hostName' => $hostName ] ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private function open( $host, $port, &$errno, &$errstr ) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if ( $this->connect_timeout > 0 ) { |
| 127 |
$tries = 2; |
| 128 |
$timeout = $this->connect_timeout; |
| 129 |
} else { |
| 130 |
$tries = 1; |
| 131 |
$timeout = $this->timeout; |
| 132 |
} |
| 133 |
|
| 134 |
$fp = null; |
| 135 |
while ( true ) { |
| 136 |
$fp = fsockopen( $host, $port, $errno, $errstr, $timeout ); |
| 137 |
if ( $fp !== false || --$tries < 1 ) { |
| 138 |
break; |
| 139 |
} |
| 140 |
usleep( 1000 ); |
| 141 |
} |
| 142 |
|
| 143 |
return $fp; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function close( $conn ) { |
| 150 |
foreach ( $this->conns as $hostName => $otherConn ) { |
| 151 |
if ( $conn === $otherConn ) { |
| 152 |
if ( $this->refCounts[$hostName] ) { |
| 153 |
$this->refCounts[$hostName]--; |
| 154 |
} |
| 155 |
if ( !$this->refCounts[$hostName] ) { |
| 156 |
fclose( $conn ); |
| 157 |
unset( $this->conns[$hostName] ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |