Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/166740

Change subject: Cache instance lists for 1 minute when called via API
......................................................................

Cache instance lists for 1 minute when called via API

- No invalidation, but that's ok. Invalidation is hard
  because not all operations have to go through wikitech.
- This is primarily an anti DDoS measure.

Change-Id: I1b56ba9688315b5b4ca17ad527db5e8f5e9c2393
---
M api/ApiListNovaInstances.php
1 file changed, 27 insertions(+), 16 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/OpenStackManager 
refs/changes/40/166740/1

diff --git a/api/ApiListNovaInstances.php b/api/ApiListNovaInstances.php
index b369339..5350a20 100644
--- a/api/ApiListNovaInstances.php
+++ b/api/ApiListNovaInstances.php
@@ -17,6 +17,7 @@
        public function run() {
                global $wgOpenStackManagerLDAPUsername;
                global $wgOpenStackManagerLDAPUserPassword;
+               global $wgMemc;
 
                $params = $this->extractRequestParams();
                $project = OpenStackNovaProject::getProjectByName( 
$params['project'] );
@@ -25,25 +26,35 @@
                        $this->dieUsage( 'Invalid project specified.', 
'badproject' );
                }
 
-               $user = new OpenStackNovaUser( $wgOpenStackManagerLDAPUsername 
);
-               $userNova = OpenStackNovaController::newFromUser( $user );
-               $userNova->authenticate( $wgOpenStackManagerLDAPUsername, 
$wgOpenStackManagerLDAPUserPassword );
+               $key = wfMemcKey( 'openstackmanager', 'apilistnovainstances', 
$params['region'], $params['project'] );
+               $instancesInfo = $wgMemc->get( $key );
+               if ( $instancesInfo === false ) {
+                       $user = new OpenStackNovaUser( 
$wgOpenStackManagerLDAPUsername );
+                       $userNova = OpenStackNovaController::newFromUser( $user 
);
+                       $userNova->authenticate( 
$wgOpenStackManagerLDAPUsername, $wgOpenStackManagerLDAPUserPassword );
 
-               $userNova->setProject( $project->getName() );
-               $userNova->setRegion( $params['region'] ); // validated by API
+                       $userNova->setProject( $project->getName() );
+                       $userNova->setRegion( $params['region'] ); // validated 
by API
 
-               $instances = $userNova->getInstances();
-               foreach ( $instances as $instance ) {
-                       $info = array(
-                               'name' => $instance->getInstanceName(),
-                               'state' => $instance->getInstanceState(),
-                               'ip' => $instance->getInstancePrivateIPs(),
-                               'id' => $instance->getInstanceId(),
-                               'floatingip' => 
$instance->getInstancePublicIPs(),
-                               'securitygroups' => 
$instance->getSecurityGroups(),
-                               'imageid' => $instance->getImageId(),
-                       );
+                       $instances = $userNova->getInstances();
+                       $instancesInfo = array();
+                       foreach ( $instances as $instance ) {
+                               $instancesInfo[ ] = array(
+                                       'name' => $instance->getInstanceName(),
+                                       'state' => 
$instance->getInstanceState(),
+                                       'ip' => 
$instance->getInstancePrivateIPs(),
+                                       'id' => $instance->getInstanceId(),
+                                       'floatingip' => 
$instance->getInstancePublicIPs(),
+                                       'securitygroups' => 
$instance->getSecurityGroups(),
+                                       'imageid' => $instance->getImageId(),
+                               );
+                       }
+               }
 
+               // Cache info for 1 minute, not caching for longer since we do 
not invalidate
+               $wgMemc->set( $key, $instancesInfo, 1 * 60 );
+
+               foreach ( $instancesInfo as $info ) {
                        // UGH I hate XML
                        $this->getResult()->setIndexedTagName( 
$info['securitygroups'], 'group' );
                        $this->getResult()->setIndexedTagName( $info['ip'], 
'ip' );

-- 
To view, visit https://gerrit.wikimedia.org/r/166740
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1b56ba9688315b5b4ca17ad527db5e8f5e9c2393
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/OpenStackManager
Gerrit-Branch: wmf/1.25wmf2
Gerrit-Owner: Yuvipanda <yuvipa...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to