33 private $urlTuples = [];
35 private $pageTuples = [];
38 private const MAX_REBOUND_DELAY = 300;
46 public function __construct( array $targets, array $options = [] ) {
48 (
int)max( $options[
'reboundDelay'] ?? 0, 0 ),
49 self::MAX_REBOUND_DELAY
52 foreach ( $targets as $target ) {
54 $this->pageTuples[] = [ $target, $delay ];
56 $this->urlTuples[] = [ $target, $delay ];
63 Assert::parameterType( __CLASS__, $update,
'$update' );
64 '@phan-var self $update';
66 $this->urlTuples = array_merge( $this->urlTuples, $update->urlTuples );
67 $this->pageTuples = array_merge( $this->pageTuples, $update->pageTuples );
72 $reboundDelayByUrl = $this->resolveReboundDelayByUrl();
76 $immediatePurgeTimestamp = time();
79 $urlsWithReboundByDelay = [];
80 foreach ( $reboundDelayByUrl as $url => $delay ) {
82 $urlsWithReboundByDelay[$delay][] = $url;
87 foreach ( $urlsWithReboundByDelay as $delay => $urls ) {
90 'jobReleaseTimestamp' => $immediatePurgeTimestamp + $delay
93 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $jobs );
103 public static function purge( array $urls ) {
104 $cdnServers = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CdnServers );
105 $htcpRouting = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPRouting );
111 $urls = array_unique( $urls );
113 wfDebugLog(
'squid', __METHOD__ .
': ' . implode(
' ', $urls ) );
116 $ts = microtime(
true );
117 $relayerGroup = MediaWikiServices::getInstance()->getEventRelayerGroup();
118 $relayerGroup->getRelayer(
'cdn-url-purges' )->notifyMulti(
121 static function ( $url ) use ( $ts ) {
132 if ( $htcpRouting ) {
133 self::HTCPPurge( $urls );
138 self::naivePurge( $urls );
146 return array_keys( $this->resolveReboundDelayByUrl() );
152 private function resolveReboundDelayByUrl() {
153 $services = MediaWikiServices::getInstance();
157 $lb = $services->getLinkBatchFactory()->newLinkBatch();
158 foreach ( $this->pageTuples as [ $page, ] ) {
159 $lb->addObj( $page );
163 $reboundDelayByUrl = [];
166 $htmlCacheUpdater = $services->getHtmlCacheUpdater();
167 foreach ( $this->pageTuples as [ $page, $delay ] ) {
168 foreach ( $htmlCacheUpdater->getUrls( $page ) as $url ) {
170 $reboundDelayByUrl[$url] = max( $reboundDelayByUrl[$url] ?? 0, $delay );
174 foreach ( $this->urlTuples as [ $url, $delay ] ) {
176 $reboundDelayByUrl[$url] = max( $reboundDelayByUrl[$url] ?? 0, $delay );
179 return $reboundDelayByUrl;
188 private static function HTCPPurge( array $urls ) {
189 $htcpRouting = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPRouting );
190 $htcpMulticastTTL = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPMulticastTTL );
195 if ( !defined(
"IPPROTO_IP" ) ) {
196 define(
"IPPROTO_IP", 0 );
197 define(
"IP_MULTICAST_LOOP", 34 );
198 define(
"IP_MULTICAST_TTL", 33 );
202 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
204 $errstr = socket_strerror( socket_last_error() );
206 ": Error opening UDP socket: $errstr" );
212 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
213 if ( $htcpMulticastTTL != 1 ) {
215 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
220 $idGenerator = MediaWikiServices::getInstance()->getGlobalIdGenerator();
221 $ids = $idGenerator->newSequentialPerNodeIDs(
227 foreach ( $urls as $url ) {
228 if ( !is_string( $url ) ) {
231 $url = self::expand( $url );
232 $conf = self::getRuleForURL( $url, $htcpRouting );
235 "No HTCP rule configured for URL {$url} , skipping" );
239 if ( isset( $conf[
'host'] ) && isset( $conf[
'port'] ) ) {
243 foreach ( $conf as $subconf ) {
244 if ( !isset( $subconf[
'host'] ) || !isset( $subconf[
'port'] ) ) {
245 throw new MWException(
"Invalid HTCP rule for URL $url\n" );
252 $htcpTransID = current( $ids );
255 $htcpSpecifier = pack(
'na4na*na8n',
256 4,
'HEAD', strlen( $url ), $url,
259 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
260 $htcpLen = 4 + $htcpDataLen + 2;
265 $htcpPacket = pack(
'nxxnCxNxxa*n',
266 $htcpLen, $htcpDataLen, $htcpOpCLR,
267 $htcpTransID, $htcpSpecifier, 2 );
270 "Purging URL $url via HTCP" );
271 foreach ( $conf as $subconf ) {
272 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
273 $subconf[
'host'], $subconf[
'port'] );
284 private static function naivePurge( array $urls ) {
285 $cdnServers = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CdnServers );
288 foreach ( $urls as $url ) {
289 $url = self::expand( $url );
291 $urlHost = strlen( $urlInfo[
'port'] ??
'' )
292 ? IPUtils::combineHostAndPort( $urlInfo[
'host'], (
int)$urlInfo[
'port'] )
299 'Connection' =>
'Keep-Alive',
300 'Proxy-Connection' =>
'Keep-Alive',
301 'User-Agent' =>
'MediaWiki/' .
MW_VERSION .
' ' . __CLASS__
304 foreach ( $cdnServers as $server ) {
305 $reqs[] = ( $baseReq + [
'proxy' => $server ] );
309 $http = MediaWikiServices::getInstance()->getHttpRequestFactory()
310 ->createMultiClient( [
'maxConnsPerHost' => 8,
'usePipelining' =>
true ] );
311 $http->runMulti( $reqs );
328 private static function expand( $url ) {
338 private static function getRuleForURL( $url, $rules ) {
339 foreach ( $rules as $regex => $routing ) {
340 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.