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 ->setCaller( __METHOD__ );
165 foreach ( $this->pageTuples as [ $page, ] ) {
166 $lb->addObj( $page );
170 $reboundDelayByUrl = [];
173 $htmlCacheUpdater = $services->getHtmlCacheUpdater();
174 foreach ( $this->pageTuples as [ $page, $delay ] ) {
175 foreach ( $htmlCacheUpdater->getUrls( $page ) as
$url ) {
177 $reboundDelayByUrl[
$url] = max( $reboundDelayByUrl[
$url] ?? 0, $delay );
181 foreach ( $this->urlTuples as [
$url, $delay ] ) {
183 $reboundDelayByUrl[
$url] = max( $reboundDelayByUrl[
$url] ?? 0, $delay );
186 return $reboundDelayByUrl;
194 private static function HTCPPurge( array $urls ) {
201 if ( !defined(
"IPPROTO_IP" ) ) {
202 define(
"IPPROTO_IP", 0 );
203 define(
"IP_MULTICAST_LOOP", 34 );
204 define(
"IP_MULTICAST_TTL", 33 );
208 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
210 $errstr = socket_strerror( socket_last_error() );
212 ": Error opening UDP socket: $errstr" );
218 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
219 if ( $htcpMulticastTTL != 1 ) {
221 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
227 $ids = $idGenerator->newSequentialPerNodeIDs(
233 foreach ( $urls as
$url ) {
234 if ( !is_string(
$url ) ) {
235 throw new InvalidArgumentException(
'Bad purge URL' );
238 $conf = self::getRuleForURL(
$url, $htcpRouting );
241 "No HTCP rule configured for URL {$url} , skipping" );
245 if ( isset( $conf[
'host'] ) && isset( $conf[
'port'] ) ) {
249 foreach ( $conf as $subconf ) {
250 if ( !isset( $subconf[
'host'] ) || !isset( $subconf[
'port'] ) ) {
251 throw new RuntimeException(
"Invalid HTCP rule for URL $url\n" );
258 $htcpTransID = current( $ids );
261 $htcpSpecifier = pack(
'na4na*na8n',
265 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
266 $htcpLen = 4 + $htcpDataLen + 2;
271 $htcpPacket = pack(
'nxxnCxNxxa*n',
272 $htcpLen, $htcpDataLen, $htcpOpCLR,
273 $htcpTransID, $htcpSpecifier, 2 );
276 "Purging URL $url via HTCP" );
277 foreach ( $conf as $subconf ) {
278 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
279 $subconf[
'host'], $subconf[
'port'] );
290 private static function naivePurge( array $urls ) {
294 foreach ( $urls as
$url ) {
297 $urlHost = strlen( $urlInfo[
'port'] ??
'' )
298 ? IPUtils::combineHostAndPort( $urlInfo[
'host'], (
int)$urlInfo[
'port'] )
305 'Connection' =>
'Keep-Alive',
306 'Proxy-Connection' =>
'Keep-Alive',
307 'User-Agent' =>
'MediaWiki/' .
MW_VERSION .
' ' . __CLASS__
310 foreach ( $cdnServers as $server ) {
311 $reqs[] = ( $baseReq + [
'proxy' => $server ] );
316 ->createMultiClient( [
'maxConnsPerHost' => 8,
'usePipelining' =>
true ] );
317 $http->runMulti( $reqs );
334 private static function expand(
$url ) {
344 private static function getRuleForURL(
$url, $rules ) {
345 foreach ( $rules as $regex => $routing ) {
346 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.