23 use Wikimedia\Assert\Assert;
38 $this->urls = $urlArr;
43 Assert::parameterType( __CLASS__, $update,
'$update' );
44 '@phan-var self $update';
46 $this->urls = array_merge( $this->urls, $update->urls );
59 foreach ( $titles as
$title ) {
60 $urlArr = array_merge( $urlArr,
$title->getCdnUrls() );
76 'urls' => $this->urls,
89 public static function purge( array $urlArr ) {
97 $urlArr = array_unique( $urlArr );
99 wfDebugLog(
'squid', __METHOD__ .
': ' . implode(
' ', $urlArr ) );
102 $ts = microtime(
true );
103 $relayerGroup = MediaWikiServices::getInstance()->getEventRelayerGroup();
104 $relayerGroup->getRelayer(
'cdn-url-purges' )->notifyMulti(
107 function ( $url ) use ( $ts ) {
125 $maxSocketsPerCdn = 8;
128 $urlsPerSocket = 400;
129 $socketsPerCdn = ceil( count( $urlArr ) / $urlsPerSocket );
130 if ( $socketsPerCdn > $maxSocketsPerCdn ) {
131 $socketsPerCdn = $maxSocketsPerCdn;
135 $chunks = array_chunk( $urlArr, ceil( count( $urlArr ) / $socketsPerCdn ) );
137 foreach ( $chunks as $chunk ) {
139 foreach ( $chunk as $url ) {
140 $client->queuePurge( self::expand( $url ) );
142 $pool->addClient( $client );
163 if ( !defined(
"IPPROTO_IP" ) ) {
164 define(
"IPPROTO_IP", 0 );
165 define(
"IP_MULTICAST_LOOP", 34 );
166 define(
"IP_MULTICAST_TTL", 33 );
170 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
172 $errstr = socket_strerror( socket_last_error() );
174 ": Error opening UDP socket: $errstr" );
180 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
183 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
192 foreach ( $urlArr as $url ) {
193 if ( !is_string( $url ) ) {
200 "No HTCP rule configured for URL {$url} , skipping" );
204 if ( isset( $conf[
'host'] ) && isset( $conf[
'port'] ) ) {
208 foreach ( $conf as $subconf ) {
209 if ( !isset( $subconf[
'host'] ) || !isset( $subconf[
'port'] ) ) {
210 throw new MWException(
"Invalid HTCP rule for URL $url\n" );
217 $htcpTransID = current( $ids );
220 $htcpSpecifier = pack(
'na4na*na8n',
221 4,
'HEAD', strlen( $url ), $url,
224 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
225 $htcpLen = 4 + $htcpDataLen + 2;
230 $htcpPacket = pack(
'nxxnCxNxxa*n',
231 $htcpLen, $htcpDataLen, $htcpOpCLR,
232 $htcpTransID, $htcpSpecifier, 2 );
235 "Purging URL $url via HTCP" );
236 foreach ( $conf as $subconf ) {
237 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
238 $subconf[
'host'], $subconf[
'port'] );
268 foreach ( $rules as $regex => $routing ) {
269 if ( $regex ===
'' || preg_match( $regex, $url ) ) {