39 private $urlTuples = [];
41 private $pageTuples = [];
44 private const MAX_REBOUND_DELAY = 300;
52 public function __construct( array $targets, array $options = [] ) {
54 (
int)max( $options[
'reboundDelay'] ?? 0, 0 ),
55 self::MAX_REBOUND_DELAY
58 foreach ( $targets as $target ) {
60 $this->pageTuples[] = [ $target, $delay ];
62 $this->urlTuples[] = [ $target, $delay ];
69 Assert::parameterType( __CLASS__, $update,
'$update' );
70 '@phan-var self $update';
72 $this->urlTuples = array_merge( $this->urlTuples, $update->urlTuples );
73 $this->pageTuples = array_merge( $this->pageTuples, $update->pageTuples );
78 $reboundDelayByUrl = $this->resolveReboundDelayByUrl();
82 $immediatePurgeTimestamp = time();
85 $urlsWithReboundByDelay = [];
86 foreach ( $reboundDelayByUrl as
$url => $delay ) {
88 $urlsWithReboundByDelay[$delay][] =
$url;
93 foreach ( $urlsWithReboundByDelay as $delay => $urls ) {
96 'jobReleaseTimestamp' => $immediatePurgeTimestamp + $delay
109 public static function purge( array $urls ) {
117 $urls = array_unique( $urls );
119 wfDebugLog(
'squid', __METHOD__ .
': ' . implode(
' ', $urls ) );
122 $ts = microtime(
true );
124 $relayerGroup->getRelayer(
'cdn-url-purges' )->notifyMulti(
127 static function (
$url ) use ( $ts ) {
138 if ( $htcpRouting ) {
139 self::HTCPPurge( $urls );
144 self::naivePurge( $urls );
152 return array_keys( $this->resolveReboundDelayByUrl() );
158 private function resolveReboundDelayByUrl() {
163 $lb = $services->getLinkBatchFactory()->newLinkBatch();
164 foreach ( $this->pageTuples as [ $page, ] ) {
165 $lb->addObj( $page );
169 $reboundDelayByUrl = [];
172 $htmlCacheUpdater = $services->getHtmlCacheUpdater();
173 foreach ( $this->pageTuples as [ $page, $delay ] ) {
174 foreach ( $htmlCacheUpdater->getUrls( $page ) as
$url ) {
176 $reboundDelayByUrl[
$url] = max( $reboundDelayByUrl[
$url] ?? 0, $delay );
180 foreach ( $this->urlTuples as [
$url, $delay ] ) {
182 $reboundDelayByUrl[
$url] = max( $reboundDelayByUrl[
$url] ?? 0, $delay );
185 return $reboundDelayByUrl;
193 private static function HTCPPurge( array $urls ) {
200 if ( !defined(
"IPPROTO_IP" ) ) {
201 define(
"IPPROTO_IP", 0 );
202 define(
"IP_MULTICAST_LOOP", 34 );
203 define(
"IP_MULTICAST_TTL", 33 );
207 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
209 $errstr = socket_strerror( socket_last_error() );
211 ": Error opening UDP socket: $errstr" );
217 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
218 if ( $htcpMulticastTTL != 1 ) {
220 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
226 $ids = $idGenerator->newSequentialPerNodeIDs(
232 foreach ( $urls as
$url ) {
233 if ( !is_string(
$url ) ) {
234 throw new InvalidArgumentException(
'Bad purge URL' );
237 $conf = self::getRuleForURL(
$url, $htcpRouting );
240 "No HTCP rule configured for URL {$url} , skipping" );
244 if ( isset( $conf[
'host'] ) && isset( $conf[
'port'] ) ) {
248 foreach ( $conf as $subconf ) {
249 if ( !isset( $subconf[
'host'] ) || !isset( $subconf[
'port'] ) ) {
250 throw new RuntimeException(
"Invalid HTCP rule for URL $url\n" );
257 $htcpTransID = current( $ids );
260 $htcpSpecifier = pack(
'na4na*na8n',
264 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
265 $htcpLen = 4 + $htcpDataLen + 2;
270 $htcpPacket = pack(
'nxxnCxNxxa*n',
271 $htcpLen, $htcpDataLen, $htcpOpCLR,
272 $htcpTransID, $htcpSpecifier, 2 );
275 "Purging URL $url via HTCP" );
276 foreach ( $conf as $subconf ) {
277 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
278 $subconf[
'host'], $subconf[
'port'] );
289 private static function naivePurge( array $urls ) {
293 foreach ( $urls as
$url ) {
296 $urlHost = strlen( $urlInfo[
'port'] ??
'' )
297 ? IPUtils::combineHostAndPort( $urlInfo[
'host'], (
int)$urlInfo[
'port'] )
304 'Connection' =>
'Keep-Alive',
305 'Proxy-Connection' =>
'Keep-Alive',
306 'User-Agent' =>
'MediaWiki/' .
MW_VERSION .
' ' . __CLASS__
309 foreach ( $cdnServers as $server ) {
310 $reqs[] = ( $baseReq + [
'proxy' => $server ] );
315 ->createMultiClient( [
'maxConnsPerHost' => 8,
'usePipelining' =>
true ] );
316 $http->runMulti( $reqs );
333 private static function expand(
$url ) {
343 private static function getRuleForURL(
$url, $rules ) {
344 foreach ( $rules as $regex => $routing ) {
345 if ( $regex ===
'' || preg_match( $regex,
$url ) ) {
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.