From cd78ba5438442d969c934a162fd33be8a9f92a4d Mon Sep 17 00:00:00 2001 From: Aleksey Konstantinov Date: Thu, 17 Oct 2024 13:08:44 +0300 Subject: [PATCH] Fixed NATSManager --- package-lock.json | 4 ++-- package.json | 2 +- src/common.js | 4 ++++ src/nats-manager.js | 11 ++++++++++- src/ws-transport.js | 4 +--- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 66eab07..d65b58e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@ulms/api-clients", - "version": "7.23.0", + "version": "7.23.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@ulms/api-clients", - "version": "7.23.0", + "version": "7.23.1", "license": "MIT", "dependencies": { "axios": "1.6.2", diff --git a/package.json b/package.json index 6dd92ab..8b4cd5b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ulms/api-clients", - "version": "7.23.0", + "version": "7.23.1", "description": "JavaScript API clients for ULMS platform", "keywords": [], "homepage": "https://github.com/foxford/ulms-api-clients-js#readme", diff --git a/src/common.js b/src/common.js index 9b3b45c..ca3e44d 100644 --- a/src/common.js +++ b/src/common.js @@ -53,6 +53,10 @@ export function enterRoom(client, roomId, agentId, timeout = 5000) { }) } +export function generateRandomId() { + return Math.random().toString(36).slice(2, 8) +} + export const sleep = async (ms) => new Promise((resolve) => { setTimeout(resolve, ms) diff --git a/src/nats-manager.js b/src/nats-manager.js index 8760367..ca8b1af 100644 --- a/src/nats-manager.js +++ b/src/nats-manager.js @@ -1,12 +1,13 @@ /* eslint-disable no-await-in-loop */ import Backoff from './backoff' -import { sleep, timeout } from './common' +import { generateRandomId, sleep, timeout } from './common' class NatsManager { constructor(client, gatekeeper, nsm, vsm) { this.client = client this.gatekeeper = gatekeeper this.gatekeeperBackoff = new Backoff() + this.id = undefined // local connection identifier (prevent simultaneous connections) this.nsm = nsm // NetworkStatusMonitor instance this.vsm = vsm // VisibilityStateMonitor instance @@ -48,6 +49,9 @@ class NatsManager { } async start(classId, name, accountLabel) { + const rid = generateRandomId() + + this.id = rid this.forcedStop = false // eslint-disable-next-line no-constant-condition @@ -86,6 +90,11 @@ class NatsManager { if (this.forcedStop) { break } + + // prevent multiple connections + if (this.id !== rid) { + break + } } } diff --git a/src/ws-transport.js b/src/ws-transport.js index 7b214c4..d9a6fe0 100644 --- a/src/ws-transport.js +++ b/src/ws-transport.js @@ -1,11 +1,9 @@ import Debug from 'debug' -import { makeDeferred } from './common' +import { generateRandomId, makeDeferred } from './common' const debug = Debug('ws-transport') -const generateRandomId = () => Math.random().toString(36).slice(2, 8) - const KEEP_ALIVE_MESSAGE = '-' const KEEP_ALIVE_TIMED_OUT_ERROR_PAYLOAD = { payload: {