22use Wikimedia\Assert\Assert;
35 private const MAX_REBOUND_DELAY = 300;
43 public function __construct( array $targets, array $options = [] ) {
45 (
int)max( $options[
'reboundDelay'] ?? 0, 0 ),
46 self::MAX_REBOUND_DELAY
49 foreach ( $targets as $target ) {
50 if ( $target instanceof
Title ) {
51 $this->titleTuples[] = [ $target, $delay ];
53 $this->urlTuples[] = [ $target, $delay ];
60 Assert::parameterType( __CLASS__, $update,
'$update' );
61 '@phan-var self $update';
63 $this->urlTuples = array_merge( $this->urlTuples, $update->urlTuples );
64 $this->titleTuples = array_merge( $this->titleTuples, $update->titleTuples );
85 $immediatePurgeTimestamp = time();
88 $urlsWithReboundByDelay = [];
89 foreach ( $reboundDelayByUrl as $url => $delay ) {
91 $urlsWithReboundByDelay[$delay][] = $url;
96 foreach ( $urlsWithReboundByDelay as $delay => $urls ) {
99 'jobReleaseTimestamp' => $immediatePurgeTimestamp + $delay
102 JobQueueGroup::singleton()->lazyPush( $jobs );
112 public static function purge( array $urls ) {
120 $urls = array_unique( $urls );
122 wfDebugLog(
'squid', __METHOD__ .
': ' . implode(
' ', $urls ) );
125 $ts = microtime(
true );
126 $relayerGroup = MediaWikiServices::getInstance()->getEventRelayerGroup();
127 $relayerGroup->getRelayer(
'cdn-url-purges' )->notifyMulti(
130 function ( $url ) use ( $ts ) {
165 $lb = MediaWikiServices::getInstance()->getLinkBatchFactory()->newLinkBatch();
166 foreach ( $this->titleTuples as list(
$title, $delay ) ) {
171 $reboundDelayByUrl = [];
174 foreach ( $this->titleTuples as list(
$title, $delay ) ) {
175 foreach (
$title->getCdnUrls() as $url ) {
177 $reboundDelayByUrl[$url] = max( $reboundDelayByUrl[$url] ?? 0, $delay );
181 foreach ( $this->urlTuples as list( $url, $delay ) ) {
183 $reboundDelayByUrl[$url] = max( $reboundDelayByUrl[$url] ?? 0, $delay );
186 return $reboundDelayByUrl;
202 if ( !defined(
"IPPROTO_IP" ) ) {
203 define(
"IPPROTO_IP", 0 );
204 define(
"IP_MULTICAST_LOOP", 34 );
205 define(
"IP_MULTICAST_TTL", 33 );
209 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
211 $errstr = socket_strerror( socket_last_error() );
213 ": Error opening UDP socket: $errstr" );
219 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
222 socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
227 $idGenerator = MediaWikiServices::getInstance()->getGlobalIdGenerator();
228 $ids = $idGenerator->newSequentialPerNodeIDs(
229 'squidhtcppurge', 32,
231 $idGenerator::QUICK_VOLATILE
234 foreach ( $urls as $url ) {
235 if ( !is_string( $url ) ) {
242 "No HTCP rule configured for URL {$url} , skipping" );
246 if ( isset( $conf[
'host'] ) && isset( $conf[
'port'] ) ) {
250 foreach ( $conf as $subconf ) {
251 if ( !isset( $subconf[
'host'] ) || !isset( $subconf[
'port'] ) ) {
252 throw new MWException(
"Invalid HTCP rule for URL $url\n" );
259 $htcpTransID = current( $ids );
262 $htcpSpecifier = pack(
'na4na*na8n',
263 4,
'HEAD', strlen( $url ), $url,
266 $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
267 $htcpLen = 4 + $htcpDataLen + 2;
272 $htcpPacket = pack(
'nxxnCxNxxa*n',
273 $htcpLen, $htcpDataLen, $htcpOpCLR,
274 $htcpTransID, $htcpSpecifier, 2 );
277 "Purging URL $url via HTCP" );
278 foreach ( $conf as $subconf ) {
279 socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
280 $subconf[
'host'], $subconf[
'port'] );
295 foreach ( $urls as $url ) {
298 $urlHost = strlen( $urlInfo[
'port'] ??
null )
299 ? IP::combineHostAndPort( $urlInfo[
'host'], $urlInfo[
'port'] )
306 'Connection' =>
'Keep-Alive',
307 'Proxy-Connection' =>
'Keep-Alive',
308 'User-Agent' =>
'MediaWiki/' .
MW_VERSION .
' ' . __CLASS__
312 $reqs[] = ( $baseReq + [
'proxy' => $server ] );
316 $http = MediaWikiServices::getInstance()->getHttpRequestFactory()
317 ->createMultiClient( [
'maxConnsPerHost' => 8,
'usePipelining' =>
true ] );
318 $http->runMulti( $reqs );
346 foreach ( $rules as $regex => $routing ) {
347 if ( $regex ===
'' || preg_match( $regex, $url ) ) {
$wgHTCPRouting
Routing configuration for HTCP multicast purging.
$wgCdnServers
List of proxy servers to purge on changes; default port is 80.
$wgHTCPMulticastTTL
HTCP multicast TTL.
const MW_VERSION
The running version of MediaWiki.
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified 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.
Handles purging the appropriate CDN objects given a list of URLs or Title instances.
__construct(array $targets, array $options=[])
array[] $urlTuples
List of (URL, rebound purge delay) tuples.
static purge(array $urls)
Purges a list of CDN nodes defined in $wgCdnServers.
static newFromTitles( $titles, $urls=[])
Create an update object from an array of Title objects, or a TitleArray object.
static getRuleForURL( $url, $rules)
Find the HTCP routing rule to use for a given URL.
static HTCPPurge(array $urls)
Send Hyper Text Caching Protocol (HTCP) CLR requests.
doUpdate()
Perform the actual work.
resolveReboundDelayByUrl()
static expand( $url)
Expand local URLs to fully-qualified URLs using the internal protocol and host defined in $wgInternal...
merge(MergeableUpdate $update)
Merge this update with $update.
array[] $titleTuples
List of (Title, rebound purge delay) tuples.
static naivePurge(array $urls)
Send HTTP PURGE requests for each of the URLs to all of the cache servers.
Job to purge a set of URLs from CDN.
Represents a title within MediaWiki.
Interface that deferrable updates should implement.
Interface that deferrable updates can implement to signal that updates can be combined.