Browse Source

refactor: move queue logic to utils

pull/7369/head
Pranav C 8 months ago
parent
commit
6e5e5ba923
  1. 41
      packages/nocodb/src/utils/RequestQueue.ts
  2. 44
      packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

41
packages/nocodb/src/utils/RequestQueue.ts

@ -0,0 +1,41 @@
export default class RequestQueue {
private runningCount: number;
private queue: any[];
private maxParallelRequests: number;
constructor(maxParallelRequests = 10) {
this.maxParallelRequests = maxParallelRequests;
this.queue = [];
this.runningCount = 0;
}
async enqueue(requestFunction) {
return new Promise((resolve, reject) => {
const execute = async () => {
this.runningCount++;
try {
const result = await requestFunction();
resolve(result);
} catch (error) {
reject(error);
} finally {
this.runningCount--;
this.processQueue();
}
};
if (this.runningCount < this.maxParallelRequests) {
execute();
} else {
this.queue.push(execute);
}
});
}
processQueue() {
if (this.runningCount < this.maxParallelRequests && this.queue.length > 0) {
const nextRequest = this.queue.shift();
nextRequest();
}
}
}

44
packages/nocodb/src/version-upgrader/ncXcdbCreatedAndUpdatedTimeUpgrader.ts

@ -13,49 +13,7 @@ import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2';
import { Altered } from '~/services/columns.service';
import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2';
import getColumnUiType from '~/helpers/getColumnUiType';
// todo: move to utils
class RequestQueue {
private runningCount: number;
private queue: any[];
private maxParallelRequests: number;
constructor(maxParallelRequests = 10) {
this.maxParallelRequests = maxParallelRequests;
this.queue = [];
this.runningCount = 0;
}
async enqueue(requestFunction) {
return new Promise((resolve, reject) => {
const execute = async () => {
this.runningCount++;
try {
const result = await requestFunction();
resolve(result);
} catch (error) {
reject(error);
} finally {
this.runningCount--;
this.processQueue();
}
};
if (this.runningCount < this.maxParallelRequests) {
execute();
} else {
this.queue.push(execute);
}
});
}
processQueue() {
if (this.runningCount < this.maxParallelRequests && this.queue.length > 0) {
const nextRequest = this.queue.shift();
nextRequest();
}
}
}
import RequestQueue from '~/utils/RequestQueue';
// Example Usage:

Loading…
Cancel
Save