fix: batch queue

This commit is contained in:
Gabe
2025-10-10 23:07:18 +08:00
parent 001f04a9ee
commit 951ce985b5
3 changed files with 15 additions and 13 deletions

View File

@@ -451,6 +451,11 @@ export const apiTranslate = async ({
const { apiSlug, batchInterval, batchSize, batchLength } = apiSetting; const { apiSlug, batchInterval, batchSize, batchLength } = apiSetting;
const key = `${apiSlug}_${fromLang}_${toLang}`; const key = `${apiSlug}_${fromLang}_${toLang}`;
const queue = getBatchQueue(key, handleTranslate, { const queue = getBatchQueue(key, handleTranslate, {
batchInterval,
batchSize,
batchLength,
});
const tranlation = await queue.addTask(text, {
from, from,
to, to,
fromLang, fromLang,
@@ -460,11 +465,7 @@ export const apiTranslate = async ({
glossary, glossary,
apiSetting, apiSetting,
usePool, usePool,
batchInterval,
batchSize,
batchLength,
}); });
const tranlation = await queue.addTask(text);
if (Array.isArray(tranlation)) { if (Array.isArray(tranlation)) {
[trText, srLang = ""] = tranlation; [trText, srLang = ""] = tranlation;
} else if (typeof tranlation === "string") { } else if (typeof tranlation === "string") {

View File

@@ -16,15 +16,14 @@ const BatchQueue = (
batchInterval = DEFAULT_BATCH_INTERVAL, batchInterval = DEFAULT_BATCH_INTERVAL,
batchSize = DEFAULT_BATCH_SIZE, batchSize = DEFAULT_BATCH_SIZE,
batchLength = DEFAULT_BATCH_LENGTH, batchLength = DEFAULT_BATCH_LENGTH,
...args
} = {} } = {}
) => { ) => {
const queue = []; const queue = [];
let isProcessing = false; let isProcessing = false;
let timer = null; let timer = null;
const sendBatchRequest = async (payloads) => { const sendBatchRequest = async (payloads, batchArgs) => {
return taskFn(payloads, args); return taskFn(payloads, batchArgs);
}; };
const processQueue = async () => { const processQueue = async () => {
@@ -66,7 +65,8 @@ const BatchQueue = (
try { try {
const payloads = tasksToProcess.map((item) => item.payload); const payloads = tasksToProcess.map((item) => item.payload);
const responses = await sendBatchRequest(payloads); const batchArgs = tasksToProcess[0].args;
const responses = await sendBatchRequest(payloads, batchArgs);
if (!Array.isArray(responses)) { if (!Array.isArray(responses)) {
throw new Error("responses format error"); throw new Error("responses format error");
} }
@@ -99,10 +99,10 @@ const BatchQueue = (
} }
}; };
const addTask = (data) => { const addTask = (data, args) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const payload = data; const payload = data;
queue.push({ payload, resolve, reject }); queue.push({ payload, resolve, reject, args });
if (queue.length >= batchSize) { if (queue.length >= batchSize) {
processQueue(); processQueue();
@@ -132,12 +132,12 @@ const queueMap = new Map();
/** /**
* 获取批处理实例 * 获取批处理实例
*/ */
export const getBatchQueue = (key, taskFn, args) => { export const getBatchQueue = (key, taskFn, options) => {
if (queueMap.has(key)) { if (queueMap.has(key)) {
return queueMap.get(key); return queueMap.get(key);
} }
const queue = BatchQueue(taskFn, args); const queue = BatchQueue(taskFn, options);
queueMap.set(key, queue); queueMap.set(key, queue);
return queue; return queue;
}; };

View File

@@ -58,6 +58,7 @@ function TestButton({ api }) {
toLang: "zh-CN", toLang: "zh-CN",
apiSetting: { ...api }, apiSetting: { ...api },
useCache: false, useCache: false,
usePool: false,
}); });
if (!text) { if (!text) {
throw new Error("empty result"); throw new Error("empty result");
@@ -701,7 +702,7 @@ function ApiFields({ apiSlug, isUserApi, deleteApi }) {
> >
{i18n("save")} {i18n("save")}
</Button> </Button>
<TestButton api={api} /> <TestButton api={formData} />
<Button size="small" variant="outlined" onClick={handleReset}> <Button size="small" variant="outlined" onClick={handleReset}>
{i18n("restore_default")} {i18n("restore_default")}
</Button> </Button>