Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed NATSManager #167

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion src/nats-manager.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -86,6 +90,11 @@ class NatsManager {
if (this.forcedStop) {
break
}

// prevent multiple connections
if (this.id !== rid) {
break
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/ws-transport.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down