From 7e3be0448f407f45444c7f126b7081f6c9fccc3d Mon Sep 17 00:00:00 2001 From: Gustavo Henke Date: Tue, 9 Sep 2014 12:43:53 -0300 Subject: [PATCH 1/2] perf(plook): cache available versions when possible To speed up version lookup, we'll cache them together with the (already cached) slug. This enables for a quicker resolution of available versions. --- lib/plook.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/plook.js b/lib/plook.js index 517c8e1..84aa091 100644 --- a/lib/plook.js +++ b/lib/plook.js @@ -139,22 +139,34 @@ Plook.prototype.findURLs = function( name, version, file ) { version = latest ? version : version.replace( /^v/, "" ); return this.lookup( name ).then(function( slug ) { + var cached = plook._cache.get( name ); + var hasVersion = function() { + return cached.versions && !!~cached.versions.indexOf( version ); + }; + var createURLs = function() { + return [ "v", "" ].map(function( prefix ) { + return utils.githubUrl( slug, prefix + version, file ); + }); + }; + return new Promise(function( resolve, reject ) { - var cmd = bower.commands.info( name ); + var cmd; + + if ( !latest && hasVersion() ) { + return resolve( createURLs() ); + } + + cmd = bower.commands.info( name ); cmd.on( "end", function( pkg ) { - var urls; + cached.versions = pkg.versions; version = latest ? pkg.versions[ 0 ] : version; - if ( !~pkg.versions.indexOf( version ) ) { + if ( !hasVersion() ) { plook.logger.error( "version not found: %s#%s", name, version ); return reject( utils.createHttpError( 404, "Version not found" ) ); } - urls = [ "v", "" ].map(function( prefix ) { - return utils.githubUrl( slug, prefix + version, file ); - }); - - resolve( urls ); + resolve( createURLs() ); }); }); }); From baa9cd57c29789548e8679ba1f880fe065a7fe47 Mon Sep 17 00:00:00 2001 From: Gustavo Henke Date: Tue, 9 Sep 2014 13:47:09 -0300 Subject: [PATCH 2/2] perf(plook): use custom HTTP agent By using a custom HTTP agent, we are able to increase the maxSockets. This allows for much greater performance when doing ApacheBench tests, for example. --- lib/plook.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/plook.js b/lib/plook.js index 84aa091..b9308cb 100644 --- a/lib/plook.js +++ b/lib/plook.js @@ -34,6 +34,10 @@ function Plook( root ) { return new Plook(); } + // Create a custom socket for when getting files + this._agent = new https.Agent(); + this._agent.maxSockets = 10; + Object.defineProperty( this, "_cache", { value: root instanceof Plook ? root._cache : LRU({ max: 500 @@ -193,6 +197,7 @@ Plook.prototype.get = function( name, version, file, etag ) { promise = this.findURLs( name, version, file ).call( "map", function( reqUrl ) { var options = url.parse( reqUrl ); + options.agent = plook._agent; options.headers = { "if-none-match": etag };