Merge "Use adaptive CDN TTLs for page views"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 21 Oct 2016 21:55:52 +0000 (21:55 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 21 Oct 2016 21:55:52 +0000 (21:55 +0000)
includes/OutputPage.php
includes/page/Article.php
includes/page/CategoryPage.php

index dea5292..863a426 100644 (file)
@@ -1933,6 +1933,36 @@ class OutputPage extends ContextSource {
                $this->setCdnMaxage( $this->mCdnMaxage );
        }
 
+       /**
+        * Get TTL in [$minTTL,$maxTTL] in pass it to lowerCdnMaxage()
+        *
+        * This sets and returns $minTTL if $mtime is false or null. Otherwise,
+        * the TTL is higher the older the $mtime timestamp is. Essentially, the
+        * TTL is 90% of the age of the object, subject to the min and max.
+        *
+        * @param string|integer|float|bool|null $mtime Last-Modified timestamp
+        * @param integer $minTTL Mimimum TTL in seconds [default: 1 minute]
+        * @param integer $maxTTL Maximum TTL in seconds [default: $wgSquidMaxage]
+        * @return integer TTL in seconds
+        * @since 1.28
+        */
+       public function adaptCdnTTL( $mtime, $minTTL = 0, $maxTTL = 0 ) {
+               $minTTL = $minTTL ?: IExpiringStore::TTL_MINUTE;
+               $maxTTL = $maxTTL ?: $this->getConfig()->get( 'SquidMaxage' );
+
+               if ( $mtime === null || $mtime === false ) {
+                       return $minTTL; // entity does not exist
+               }
+
+               $age = time() - wfTimestamp( TS_UNIX, $mtime );
+               $adaptiveTTL = max( .9 * $age, $minTTL );
+               $adaptiveTTL = min( $adaptiveTTL, $maxTTL );
+
+               $this->lowerCdnMaxage( (int)$adaptiveTTL );
+
+               return $adaptiveTTL;
+       }
+
        /**
         * Use enableClientCache(false) to force it to send nocache headers
         *
index 548e533..15f5238 100644 (file)
@@ -706,6 +706,10 @@ class Article implements Page {
                        }
                }
 
+               # Use adaptive TTLs for CDN so delayed/failed purges are noticed less often.
+               # This could use getTouched(), but that could be scary for major template edits.
+               $outputPage->adaptCdnTTL( $this->mPage->getTimestamp(), IExpiringStore::TTL_DAY );
+
                # Check for any __NOINDEX__ tags on the page using $pOutput
                $policy = $this->getRobotPolicy( 'view', $pOutput );
                $outputPage->setIndexPolicy( $policy['index'] );
@@ -715,7 +719,6 @@ class Article implements Page {
                $this->mPage->doViewUpdates( $user, $oldid );
 
                $outputPage->addModules( 'mediawiki.action.view.postEdit' );
-
        }
 
        /**
index 865471c..6d2be51 100644 (file)
@@ -68,6 +68,10 @@ class CategoryPage extends Article {
                if ( $title->inNamespace( NS_CATEGORY ) ) {
                        $this->closeShowCategory();
                }
+
+               # Use adaptive TTLs for CDN so delayed/failed purges are noticed less often
+               $outputPage = $this->getContext()->getOutput();
+               $outputPage->adaptCdnTTL( $this->mPage->getTouched(), IExpiringStore::TTL_MINUTE );
        }
 
        function openShowCategory() {