From f71cc5317915adca2c496063f23d273781db0b68 Mon Sep 17 00:00:00 2001 From: Artemis Mucaj Date: Mon, 29 May 2017 17:03:56 +0200 Subject: [PATCH] Fix openstack client multiple concurrent authentications issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Store pending authentication callbacks - Only call `authorize` for the first query - Dispatch callbacks once authentication is done --- lib/pkgcloud/openstack/client.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/pkgcloud/openstack/client.js b/lib/pkgcloud/openstack/client.js index 2f0259318..76bac729a 100644 --- a/lib/pkgcloud/openstack/client.js +++ b/lib/pkgcloud/openstack/client.js @@ -70,6 +70,7 @@ var Client = exports.Client = function (options) { }); this._serviceUrl = null; + this._pendingAuthentications = []; }; util.inherits(Client, base.Client); @@ -141,6 +142,18 @@ Client.prototype.successCodes = { 204: 'No content' }; +/** + * Client.handleAuthentications + * + * @description This function dispatches authentication callbacks + * + * @param arg + */ +Client.prototype.handleAuthentications = function (arg) { + var fns = this._pendingAuthentications.splice(0); + fns.forEach(fn => fn(arg)); +}; + /** * Client.auth * @@ -157,9 +170,19 @@ Client.prototype.auth = function (callback) { return; } + self._pendingAuthentications.push(callback); + + if (self._isAuthenticating != null && self._isAuthenticating) { + return; + } + + self._isAuthenticating = true; + self._identity.authorize(function(err) { + self._isAuthenticating = false; if (err) { - return callback(err); + self.handleAuthentications(err); + return; } var options = { @@ -176,15 +199,16 @@ Client.prototype.auth = function (callback) { serviceUrl: self._serviceUrl, options: options }); - - callback(); + self.handleAuthentications(); + return; } catch (e) { self.emit('log::error', 'Unable to select endpoint for service', { error: e.toString(), options: options }); - callback(e); + self.handleAuthentications(e); + return; } }); };