Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: handle request concurrency

DarkPhoenix2704740653d4538e
diff --git a/packages/nc-gui/composables/useInfiniteData.ts b/packages/nc-gui/composables/useInfiniteData.ts
index 604d90423d..aa82be3278 100644
--- a/packages/nc-gui/composables/useInfiniteData.ts
+++ b/packages/nc-gui/composables/useInfiniteData.ts
@@ -103,7 +103,7 @@ export function useInfiniteData(args: {
const { user } = useGlobal()
- const { fetchSharedViewData, fetchCount } = useSharedView()
+ const { fetchSharedViewData, fetchCount, fetchBulkListData } = useSharedView()
const {
nestedFilters,
@@ -264,7 +264,7 @@ export function useInfiniteData(args: {
const getChunkIndex = (rowIndex: number) => Math.floor(rowIndex / CHUNK_SIZE)
- const fetchChunk = async (chunkId: number, path: Array<number> = [], forceFetch = false) => {
+ const _fetchChunk = async (chunkId: number, path: Array<number> = [], forceFetch = false) => {
const dataCache = getDataCache(path)
if (dataCache.chunkStates.value[chunkId] && !forceFetch) return
@@ -289,6 +289,155 @@ export function useInfiniteData(args: {
}
}
+ let pendingChunkRequests: Array<{
+ chunkId: number
+ path: Array<number>
+ forceFetch: boolean
+ resolve: (value: any) => void
+ reject: (error: any) => void
+ }> = []
+ let batchTimer: NodeJS.Timeout | null = null
+ const BATCH_SIZE = 50
+ const BATCH_TIMEOUT = 200
+
+ async function fetchChunkIndividually(chunkId: number, path: Array<number>) {
+ const dataCache = getDataCache(path)
+ dataCache.chunkStates.value[chunkId] = 'loading'
+ const offset = chunkId * CHUNK_SIZE
+
+ try {
+ const newItems = await loadData({ offset, limit: CHUNK_SIZE }, false)
+ if (!newItems) {
+ dataCache.chunkStates.value[chunkId] = undefined
+ return
+ }
+
+ newItems.forEach((item) => {
+ dataCache.cachedRows.value.set(item.rowMeta.rowIndex!, item)
+ })
+ dataCache.chunkStates.value[chunkId] = 'loaded'
+ } catch (error) {
+ console.error('Error fetching chunk:', error)
+ dataCache.chunkStates.value[chunkId] = undefined
+ throw error
+ }
+ }
+
+ async function processBatch() {
+ if (pendingChunkRequests.length === 0) return
+
+ if (batchTimer) {
+ clearTimeout(batchTimer)
+ batchTimer = null
+ }
+
+ const batch = [...pendingChunkRequests]
+ pendingChunkRequests = []
+
+ try {
+ const bulkRequests = []
+
+ for (let i = 0; i < batch.length; i++) {
+ const req = batch[i]
+ const where = await callbacks?.getWhereFilter?.(req.path)
+ bulkRequests.push({
+ where,
+ offset: req.chunkId * CHUNK_SIZE,
+ limit: CHUNK_SIZE,
+ alias: `chunk_${req.chunkId}_${req.path.join('_')}`,
+ ...(isUIAllowed('sortSync') ? {} : { sortArrJson: JSON.stringify(sorts.value) }),
+ ...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(nestedFilters.value) }),
+ })
+ }
+
+ const bulkResponse = !isPublic?.value
+ ? await $api.dbDataTableBulkList.dbDataTableBulkList(meta.value.id!, { viewId: viewMeta.value?.id }, bulkRequests, {})
+ : await fetchBulkListData({}, bulkRequests)
+
+ for (const request of batch) {
+ try {
+ const alias = `chunk_${request.chunkId}_${request.path.join('_')}`
+ const chunkData = bulkResponse[alias]
+ const dataCache = getDataCache(request.path)
+
+ if (chunkData && chunkData.list) {
+ const rows = formatData(chunkData.list, chunkData.pageInfo)
+ rows.forEach((item: any) => {
+ dataCache.cachedRows.value.set(item.rowMeta.rowIndex!, item)
+ })
+ dataCache.chunkStates.value[request.chunkId] = 'loaded'
+ } else {
+ dataCache.chunkStates.value[request.chunkId] = undefined
+ }
+
+ request.resolve(undefined)
+ } catch (error) {
+ console.error(`Error processing chunk ${request.chunkId}:`, error)
+ const dataCache = getDataCache(request.path)
+ dataCache.chunkStates.value[request.chunkId] = undefined
+ request.reject(error)
+ }
+ }
+ } catch (error) {
+ console.error('Bulk chunk request failed, falling back to individual requests:', error)
+
+ const promises = batch.map((request) =>
+ fetchChunkIndividually(request.chunkId, request.path)
+ .then(() => request.resolve(undefined))
+ .catch((err) => request.reject(err)),
+ )
+
+ await Promise.allSettled(promises)
+ }
+ }
+
+ const fetchChunk = async (chunkId: number, path: Array<number> = [], forceFetch = false) => {
+ const dataCache = getDataCache(path)
+
+ if (dataCache.chunkStates.value[chunkId] && !forceFetch) return
+
+ const existingRequest = pendingChunkRequests.find((req) => req.chunkId === chunkId && req.path.join(',') === path.join(','))
+
+ if (existingRequest && !forceFetch) {
+ return new Promise<void>((resolve, reject) => {
+ const originalResolve = existingRequest.resolve
+ const originalReject = existingRequest.reject
+
+ existingRequest.resolve = (value) => {
+ originalResolve(value)
+ resolve(value)
+ }
+
+ existingRequest.reject = (error) => {
+ originalReject(error)
+ reject(error)
+ }
+ })
+ }
+
+ return new Promise<void>((resolve, reject) => {
+ pendingChunkRequests.push({
+ chunkId,
+ path,
+ forceFetch,
+ resolve,
+ reject,
+ })
+
+ dataCache.chunkStates.value[chunkId] = 'loading'
+
+ if (pendingChunkRequests.length >= BATCH_SIZE) {
+ processBatch()
+ } else {
+ if (!batchTimer) {
+ batchTimer = setTimeout(() => {
+ processBatch()
+ }, BATCH_TIMEOUT)
+ }
+ }
+ })
+ }
+
const clearCache = (visibleStartIndex: number, visibleEndIndex: number, path: Array<number> = []) => {
const dataCache = getDataCache(path)
if (visibleEndIndex === Number.POSITIVE_INFINITY && visibleStartIndex === Number.NEGATIVE_INFINITY) {
diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts
index 53111652fe..c376b1115c 100644
--- a/packages/nocodb/src/services/data-table.service.ts
+++ b/packages/nocodb/src/services/data-table.service.ts
@@ -9,8 +9,8 @@ import { validatePayload } from 'src/helpers';
import type { NcApiVersion } from 'nocodb-sdk';
import type { LinkToAnotherRecordColumn } from '~/models';
import type { NcContext } from '~/interface/config';
-import { nocoExecute } from '~/utils';
import { Column, Model, Source, View } from '~/models';
+import { nocoExecute, processConcurrently } from '~/utils';
import { DatasService } from '~/services/datas.service';
import { NcError } from '~/helpers/catchError';
import getAst from '~/helpers/getAst';
@@ -866,23 +866,23 @@ export class DataTableService {
NcError.badRequest('Invalid bulkFilterList');
}
- const dataListResults = await bulkFilterList.reduce(
- async (accPromise, dF: any) => {
- const acc = await accPromise;
- const result = await this.datasService.dataList(context, {
- query: {
- ...dF,
- },
+ const results = await processConcurrently(
+ bulkFilterList,
+ async (dF: any) => {
+ const data = await this.datasService.dataList(context, {
+ query: { ...dF },
model,
view,
});
- acc[dF.alias] = result;
- return acc;
+ return { alias: dF.alias, data };
},
- Promise.resolve({}),
+ 5,
);
- return dataListResults;
+ return results.reduce((acc, { alias, data }) => {
+ acc[alias] = data;
+ return acc;
+ }, {});
}
async bulkGroupBy(