Skip to content

Commit

Permalink
Fixed NATSManager
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Oct 17, 2024
1 parent 523a909 commit cd78ba5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
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

0 comments on commit cd78ba5

Please sign in to comment.