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;
187 private static function HTCPPurge( array $urls ) {
188 $htcpRouting = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPRouting );
189 $htcpMulticastTTL = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HTCPMulticastTTL );
194 if ( !defined(
"IPPROTO_IP" ) ) {
195 define(
"IPPROTO_IP", 0 );
196 define(
"IP_MULTICAST_LOOP", 34 );
197 define(
"IP_MULTICAST_TTL", 33 );
201 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
203 $errstr = socket_strerror( socket_last_error() );
205 ": Error opening UDP socket: $errstr" );
211 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
212 if ( $htcpMulticastTTL != 1 ) {
214 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
219 $idGenerator = MediaWikiServices::getInstance()->getGlobalIdGenerator();
220 $ids = $idGenerator->newSequentialPerNodeIDs(
226 foreach ( $urls as $url ) {
227 if ( !is_string( $url ) ) {
228 throw new InvalidArgumentException(
'Bad purge URL' );
230 $url = self::expand( $url );
231 $conf = self::getRuleForURL( $url, $htcpRouting );
234 "No HTCP rule configured for URL {$url} , skipping" );
238 if ( isset( $conf[
'host'] ) && isset( $conf[
'port'] ) ) {
242 foreach ( $conf as $subconf ) {
243 if ( !isset( $subconf[
'host'] ) || !isset( $subconf[
'port'] ) ) {
244 throw new RuntimeException(
"Invalid HTCP rule for URL $url\n" );
251 $htcpTransID = current( $ids );
254 $htcpSpecifier = pack(
'na4na*na8n',
255 4,
'HEAD', strlen( $url ), $url,
258 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
259 $htcpLen = 4 + $htcpDataLen + 2;
264 $htcpPacket = pack(
'nxxnCxNxxa*n',
265 $htcpLen, $htcpDataLen, $htcpOpCLR,
266 $htcpTransID, $htcpSpecifier, 2 );
269 "Purging URL $url via HTCP" );
270 foreach ( $conf as $subconf ) {
271 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
272 $subconf[
'host'], $subconf[
'port'] );
283 private static function naivePurge( array $urls ) {
284 $cdnServers = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::CdnServers );
287 foreach ( $urls as $url ) {
288 $url = self::expand( $url );
290 $urlHost = strlen( $urlInfo[
'port'] ??
'' )
291 ? IPUtils::combineHostAndPort( $urlInfo[
'host'], (
int)$urlInfo[
'port'] )
298 'Connection' =>
'Keep-Alive',
299 'Proxy-Connection' =>
'Keep-Alive',
300 'User-Agent' =>
'MediaWiki/' .
MW_VERSION .
' ' . __CLASS__
303 foreach ( $cdnServers as $server ) {
304 $reqs[] = ( $baseReq + [
'proxy' => $server ] );
308 $http = MediaWikiServices::getInstance()->getHttpRequestFactory()
309 ->createMultiClient( [
'maxConnsPerHost' => 8,
'usePipelining' =>
true ] );
310 $http->runMulti( $reqs );
327 private static function expand( $url ) {
328 return (
string)MediaWikiServices::getInstance()->getUrlUtils()->expand( $url,
PROTO_INTERNAL );
337 private static function getRuleForURL( $url, $rules ) {
338 foreach ( $rules as $regex => $routing ) {
339 if ( $regex ===
'' || preg_match( $regex, $url ) ) {