Add a limit for the NSO APIs

This commit is contained in:
Matt Isenhower
2024-11-21 22:08:51 -08:00
parent a134856713
commit 664f7546b8

View File

@@ -1,9 +1,13 @@
// eslint-disable-next-line import/no-unresolved
import CoralApi from 'nxapi/coral';
import { addUserAgent } from 'nxapi';
import pLimit from 'p-limit';
import ValueCache from '../common/ValueCache.mjs';
import prefixedConsole from '../common/prefixedConsole.mjs';
const coralLimit = pLimit(1);
const webServiceLimit = pLimit(1);
let _nxapiInitialized = false;
function initializeNxapi() {
@@ -72,15 +76,17 @@ export default class NsoClient
}
async getCoralApi(useCache = true) {
let data = useCache
? await this._getCoralCache().getData()
: null;
return coralLimit(async () => {
let data = useCache
? await this._getCoralCache().getData()
: null;
if (!data) {
data = await this._createCoralSession();
}
if (!data) {
data = await this._createCoralSession();
}
return CoralApi.createWithSavedToken(data);
return CoralApi.createWithSavedToken(data);
});
}
async _createCoralSession() {
@@ -101,16 +107,18 @@ export default class NsoClient
}
async getWebServiceToken(id, useCache = true) {
let tokenCache = this._getWebServiceTokenCache(id);
let token = useCache
? await tokenCache.getData()
: null;
return webServiceLimit(async () => {
let tokenCache = this._getWebServiceTokenCache(id);
let token = useCache
? await tokenCache.getData()
: null;
if (!token) {
token = await this._createWebServiceToken(id, tokenCache);
}
if (!token) {
token = await this._createWebServiceToken(id, tokenCache);
}
return token.accessToken;
return token.accessToken;
});
}
async _createWebServiceToken(id, tokenCache) {