coffee-core
Version:
Coffee IT API core library
46 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClientConnector = void 0;
const common_1 = require("@nestjs/common");
class ClientConnector {
config;
client;
serviceName;
context;
constructor(config, client, serviceName, context) {
this.config = config;
this.client = client;
this.serviceName = serviceName;
this.context = context;
}
async connect() {
try {
await this.client.connect();
common_1.Logger.log(`Connected to ${this.serviceName}`, this.context);
}
catch (err) {
await this.retryConnection(0);
}
}
async retryConnection(retries) {
setTimeout(async () => {
const newRetries = retries + 1;
common_1.Logger.log(`Retrying ${this.serviceName} connection: ${newRetries}th time`, this.context);
try {
await this.client.connect();
common_1.Logger.log(`Connected to ${this.serviceName}`, this.context);
}
catch (err) {
await this.retryConnection(newRetries);
}
}, this.getRetryTime(retries));
}
getRetryTime(retries) {
const time = this.config.INITIAL_RETRY_TIMEOUT_MS * 2 ** retries;
return time > this.config.MAX_RETRY_TIME_MS
? this.config.MAX_RETRY_TIME_MS
: time;
}
}
exports.ClientConnector = ClientConnector;
//# sourceMappingURL=client-connector.js.map