From 36c1e2cc731f7dee1ab0dd33361c19f0f890f4f7 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 14 Apr 2015 17:57:31 +0200 Subject: [PATCH] Limit tries in case of transport errors (ugly). --- packages/xen-api/src/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/xen-api/src/index.js b/packages/xen-api/src/index.js index 494effb33..12556ede8 100644 --- a/packages/xen-api/src/index.js +++ b/packages/xen-api/src/index.js @@ -100,6 +100,8 @@ notConnectedPromise.catch(noop) // =================================================================== +const MAX_TRIES = 5 + export class Xapi extends EventEmitter { constructor (opts) { super() @@ -108,6 +110,7 @@ export class Xapi extends EventEmitter { this._auth = opts.auth this._sessionId = notConnectedPromise + this._tries = 0 this._init() @@ -261,6 +264,10 @@ export class Xapi extends EventEmitter { .catch(isNetworkError, isXapiNetworkError, () => { debug('%s: a network error happened', this._humanId) + if (++this._tries > MAX_TRIES) { + throw new Error('will not try anymore') + } + // TODO: ability to cancel the connection // TODO: ability to force immediate reconnection // TODO: implement back-off @@ -268,7 +275,9 @@ export class Xapi extends EventEmitter { return Bluebird.delay(5e3).then(() => { // TODO: handling not responding host. - return this._transportCall(method, args) + return this._transportCall(method, args).then(() => { + this._tries = 0 + }) }) }) .cancellable()