@creditkarma/consul-client
Version:
A client for Hashicorp Consul written in TypeScript
42 lines • 1.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseClient = void 0;
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const logger = require("./logger");
const RETRY_INTERVAL = 1000;
class BaseClient {
constructor(destinations = [constants_1.DEFAULT_HOST]) {
this.currentIndex = 0;
this.destinations = destinations.map((next) => {
return (0, utils_1.ensureProtocol)((0, utils_1.removeLeadingTrailingSlash)(next));
});
this.currentDestination = this.destinations[this.currentIndex];
}
send(req, options = {}) {
const dest = this.currentDestination;
return this.processRequest(req, options).catch((err) => this.runRetry(req, options, dest, err));
}
runRetry(req, options, dest, err) {
return new Promise((resolve, reject) => {
if (this.currentIndex < this.destinations.length - 1 ||
dest !== this.currentDestination) {
logger.warn(`Request failed on host[${this.currentDestination}]. ${err.message}. Trying next host. `);
if (dest === this.currentDestination) {
this.currentIndex += 1;
this.currentDestination =
this.destinations[this.currentIndex];
}
setTimeout(() => {
resolve(this.send(req, options));
}, RETRY_INTERVAL);
}
else {
this.currentIndex = 0;
return reject(err);
}
});
}
}
exports.BaseClient = BaseClient;
//# sourceMappingURL=BaseClient.js.map