Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 5d7dd80

Browse files
add retry for luis rate limiting issue (#830)
* add once retry for luis rate limiting issue * optimize style * make parameters of retry configrable and add corresponding tests Co-authored-by: Vishwac Sena Kannan <[email protected]>
1 parent 6b714c2 commit 5d7dd80

File tree

3 files changed

+425
-42
lines changed

3 files changed

+425
-42
lines changed

packages/lu/src/parser/lubuild/builder.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,25 @@ export class Builder {
140140
fallbackLocale: string,
141141
deleteOldVersion: boolean,
142142
multiRecognizers?: Map<string, MultiLanguageRecognizer>,
143-
settings?: Map<string, Settings>) {
143+
settings?: Map<string, Settings>,
144+
luisAPITPS?: number,
145+
timeBucketOfRequests?: number,
146+
retryCount?: number,
147+
retryDuration?: number) {
144148
// luis api TPS which means 5 concurrent transactions to luis api in 1 second
145149
// can set to other value if switched to a higher TPS(transaction per second) key
146-
let luisApiTps = 5
150+
let luisApiTps = luisAPITPS || 5
147151

148152
// set luis call delay duration to 1100 millisecond because 1000 can hit corner case of rate limit
149-
let delayDuration = 1100
153+
let timeBucket = timeBucketOfRequests || 1100
150154

151-
const luBuildCore = new LuBuildCore(authoringKey, endpoint)
155+
// set retry count for rate limit luis API failure
156+
let countForRetry = retryCount || 1
157+
158+
// set retry duration for rate limit luis API failure
159+
let durationForRetry = retryDuration || 1000
160+
161+
const luBuildCore = new LuBuildCore(authoringKey, endpoint, countForRetry, durationForRetry)
152162
const apps = await luBuildCore.getApplicationList()
153163

154164
// here we do a while loop to make full use of luis tps capacity
@@ -180,15 +190,15 @@ export class Builder {
180190
// otherwise create a new application
181191
if (recognizer.getAppId() && recognizer.getAppId() !== '') {
182192
// To see if need update the model
183-
needTrainAndPublish = await this.updateApplication(currentApp, luBuildCore, recognizer, delayDuration, deleteOldVersion)
193+
needTrainAndPublish = await this.updateApplication(currentApp, luBuildCore, recognizer, timeBucket, deleteOldVersion)
184194
} else {
185195
// create a new application
186-
needTrainAndPublish = await this.createApplication(currentApp, luBuildCore, recognizer, delayDuration)
196+
needTrainAndPublish = await this.createApplication(currentApp, luBuildCore, recognizer, timeBucket)
187197
}
188198

189199
if (needTrainAndPublish) {
190200
// train and publish application
191-
await this.trainAndPublishApplication(luBuildCore, recognizer, delayDuration)
201+
await this.trainAndPublishApplication(luBuildCore, recognizer, timeBucket)
192202
}
193203

194204
// update multiLanguageRecognizer asset
@@ -277,8 +287,8 @@ export class Builder {
277287
return writeDone
278288
}
279289

280-
async getActiveVersionIds(appNames: string[], authoringKey: string, region: string) {
281-
const luBuildCore = new LuBuildCore(authoringKey, `https://${region}.api.cognitive.microsoft.com`)
290+
async getActiveVersionIds(appNames: string[], authoringKey: string, region: string, retryCount?: number, retryDuration?: number) {
291+
const luBuildCore = new LuBuildCore(authoringKey, `https://${region}.api.cognitive.microsoft.com`, retryCount || 1, retryDuration || 1000)
282292
const apps = await luBuildCore.getApplicationList()
283293
let appNameVersionMap = new Map<string, string>()
284294
for (const appName of appNames) {
@@ -311,12 +321,12 @@ export class Builder {
311321
return currentApp
312322
}
313323

314-
async updateApplication(currentApp: any, luBuildCore: LuBuildCore, recognizer: Recognizer, delayDuration: number, deleteOldVersion: boolean) {
315-
await delay(delayDuration)
324+
async updateApplication(currentApp: any, luBuildCore: LuBuildCore, recognizer: Recognizer, timeBucket: number, deleteOldVersion: boolean) {
325+
await delay(timeBucket)
316326
const appInfo = await luBuildCore.getApplicationInfo(recognizer.getAppId())
317327
recognizer.versionId = appInfo.activeVersion || appInfo.endpoints.PRODUCTION.versionId
318328

319-
await delay(delayDuration)
329+
await delay(timeBucket)
320330
const existingApp = await luBuildCore.exportApplication(recognizer.getAppId(), recognizer.versionId)
321331

322332
// compare models
@@ -329,16 +339,16 @@ export class Builder {
329339
}
330340

331341
this.handler(`${recognizer.getLuPath()} creating version=${newVersionId}\n`)
332-
await delay(delayDuration)
342+
await delay(timeBucket)
333343
await luBuildCore.importNewVersion(recognizer.getAppId(), currentApp, options)
334344

335345
if (deleteOldVersion) {
336-
await delay(delayDuration)
346+
await delay(timeBucket)
337347
const versionObjs = await luBuildCore.listApplicationVersions(recognizer.getAppId())
338348
for (const versionObj of versionObjs) {
339349
if (versionObj.version !== newVersionId) {
340350
this.handler(`${recognizer.getLuPath()} deleting old version=${versionObj.version}`)
341-
await delay(delayDuration)
351+
await delay(timeBucket)
342352
await luBuildCore.deleteVersion(recognizer.getAppId(), versionObj.version)
343353
}
344354
}
@@ -351,25 +361,25 @@ export class Builder {
351361
}
352362
}
353363

354-
async createApplication(currentApp: any, luBuildCore: LuBuildCore, recognizer: Recognizer, delayDuration: number) {
364+
async createApplication(currentApp: any, luBuildCore: LuBuildCore, recognizer: Recognizer, timeBucket: number) {
355365
currentApp.versionId = currentApp.versionId && currentApp.versionId !== '' ? currentApp.versionId : '0.1'
356366
recognizer.versionId = currentApp.versionId
357367
this.handler(`Creating LUIS.ai application: ${currentApp.name} version:${currentApp.versionId}\n`)
358-
await delay(delayDuration)
368+
await delay(timeBucket)
359369
const response = await luBuildCore.importApplication(currentApp)
360370
recognizer.setAppId(typeof response === 'string' ? response : response[Object.keys(response)[0]])
361371
return true
362372
}
363373

364-
async trainAndPublishApplication(luBuildCore: LuBuildCore, recognizer: Recognizer, delayDuration: number) {
374+
async trainAndPublishApplication(luBuildCore: LuBuildCore, recognizer: Recognizer, timeBucket: number) {
365375
// send train application request
366376
this.handler(`${recognizer.getLuPath()} training version=${recognizer.versionId}\n`)
367-
await delay(delayDuration)
377+
await delay(timeBucket)
368378
await luBuildCore.trainApplication(recognizer.getAppId(), recognizer.versionId)
369379
this.handler(`${recognizer.getLuPath()} waiting for training for version=${recognizer.versionId}...\n`)
370380
let done = true
371381
do {
372-
await delay(delayDuration)
382+
await delay(timeBucket)
373383

374384
// get training status to see if training completed
375385
let trainingStatus = await luBuildCore.getTrainingStatus(recognizer.getAppId(), recognizer.versionId)
@@ -387,7 +397,7 @@ export class Builder {
387397

388398
// publish applications
389399
this.handler(`${recognizer.getLuPath()} publishing version=${recognizer.versionId}\n`)
390-
await delay(delayDuration)
400+
await delay(timeBucket)
391401
await luBuildCore.publishApplication(recognizer.getAppId(), recognizer.versionId)
392402
this.handler(`${recognizer.getLuPath()} publishing finished\n`)
393403
}

0 commit comments

Comments
 (0)