Skip to content

Commit bfb42a5

Browse files
committed
fix: a case where multiple QuantelGateway clients where spawned by mistake
1 parent 4d2153a commit bfb42a5

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

  • shared/packages/worker/src/worker/accessorHandlers

shared/packages/worker/src/worker/accessorHandlers/quantel.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export class QuantelAccessorHandle<Metadata> extends GenericAccessorHandle<Metad
384384
}
385385
private async getQuantelGateway(): Promise<QuantelGateway> {
386386
/** Persistant store for Quantel gatews */
387-
const cacheGateways = this.ensureCache<Record<string, QuantelGateway>>('gateways', {})
387+
const cacheGateways = this.ensureCache<Record<string, Promise<QuantelGateway>>>('gateways', {})
388388

389389
// These errors are just for types. User-facing checks are done in this.checkAccessor()
390390
if (!this.accessor.quantelGatewayUrl) throw new Error('accessor.quantelGatewayUrl is not set')
@@ -399,18 +399,32 @@ export class QuantelAccessorHandle<Metadata> extends GenericAccessorHandle<Metad
399399
ISAUrls = (ISAUrls as string).split(',')
400400
}
401401

402-
let gateway: QuantelGateway = cacheGateways[id]
403-
404-
if (!gateway) {
405-
gateway = new QuantelGateway()
406-
this.worker.logger.debug(`Quantel.QuantelGateway: Created new Quantel Gateway client "${id}"`)
407-
await gateway.init(this.accessor.quantelGatewayUrl, ISAUrls, this.accessor.zoneId, this.accessor.serverId)
408-
409-
gateway.on('error', (e) => this.worker.logger.error(`Quantel.QuantelGateway: ${JSON.stringify(e)}`))
402+
let pGateway: Promise<QuantelGateway> | undefined = cacheGateways[id]
403+
404+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
405+
if (!pGateway) {
406+
const quantelGatewayUrl = this.accessor.quantelGatewayUrl
407+
408+
// Note: We need to store a Promise<QuantelGateway> in the cache because otherwise many QuantelGateways
409+
// can be created if multiple calls to this are done synchronously.
410+
pGateway = new Promise((resolve, reject) => {
411+
const gateway = new QuantelGateway()
412+
this.worker.logger.debug(`Quantel.QuantelGateway: Created new Quantel Gateway client "${id}"`)
413+
gateway.on('error', (e) => this.worker.logger.error(`Quantel.QuantelGateway: ${JSON.stringify(e)}`))
414+
415+
gateway
416+
.init(quantelGatewayUrl, ISAUrls, this.accessor.zoneId, this.accessor.serverId)
417+
.then(() => {
418+
resolve(gateway)
419+
})
420+
.catch(reject)
421+
})
410422

411-
cacheGateways[id] = gateway
423+
cacheGateways[id] = pGateway
412424
}
413425

426+
const gateway: QuantelGateway = await pGateway
427+
414428
// Verify that the cached gateway matches what we want:
415429
// The reason for this is that a Quantel gateway is pointed at an ISA-setup on startup,
416430
// and shouldn't be changed without restarting aftewards.

0 commit comments

Comments
 (0)