feat(xen-api): implements Fibonacci backoff (#17)

This commit is contained in:
Julien Fontanet 2017-10-28 13:14:15 +02:00 committed by GitHub
parent 50581c539c
commit ea7c44b544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 12 deletions

View File

@ -38,6 +38,7 @@
"event-to-promise": "^0.8.0",
"exec-promise": "^0.7.0",
"http-request-plus": "^0.4.0",
"iterable-backoff": "^0.0.0",
"json-rpc-protocol": "^0.11.2",
"kindof": "^2.0.0",
"lodash": "^4.17.4",

View File

@ -5,6 +5,7 @@ import ms from 'ms'
import httpRequest from 'http-request-plus'
import { BaseError } from 'make-error'
import { EventEmitter } from 'events'
import { fibonacci } from 'iterable-backoff'
import { filter, forEach, isArray, isObject, map, noop, omit, reduce, startsWith } from 'lodash'
import {
Cancel,
@ -147,8 +148,6 @@ const getTaskResult = (task, onSuccess, onFailure) => {
// -------------------------------------------------------------------
const MAX_TRIES = 5
const CONNECTED = 'connected'
const CONNECTING = 'connecting'
const DISCONNECTED = 'disconnected'
@ -866,20 +865,24 @@ Xapi.prototype._transportCall = reduce([
})
},
call => function () {
let tries = 1
let iterator // lazily created
const loop = () => call.apply(this, arguments)
::pCatch(isNetworkError, isXapiNetworkError, error => {
debug('%s: network error %s', this._humanId, error.code)
if (++tries < MAX_TRIES) {
// TODO: ability to cancel the connection
// TODO: ability to force immediate reconnection
// TODO: implement back-off
return pDelay(5e3).then(loop)
if (iterator === undefined) {
iterator = fibonacci().clamp(undefined, 60).take(10).toMs()
}
debug('%s too many network errors (%s), give up', this._humanId, tries)
const cursor = iterator.next()
if (!cursor.done) {
// TODO: ability to cancel the connection
// TODO: ability to force immediate reconnection
const delay = cursor.value
debug('%s: network error %s, next try in %s ms', this._humanId, error.code, delay)
return pDelay(delay).then(loop)
}
debug('%s: network error %s, aborting', this._humanId, error.code)
// mark as disconnected
this.disconnect()::pCatch(noop)

View File

@ -2201,6 +2201,10 @@ istanbul-reports@^1.1.1:
dependencies:
handlebars "^4.0.3"
iterable-backoff@^0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/iterable-backoff/-/iterable-backoff-0.0.0.tgz#e49660be7e1727fc52ee5c86c67cc3a427b4e109"
jest-changed-files@^20.0.3:
version "20.0.3"
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-20.0.3.tgz#9394d5cc65c438406149bef1bf4d52b68e03e3f8"