refactor: optimize batchqueue
This commit is contained in:
@@ -247,7 +247,15 @@ export const apiTranslate = async ({
|
|||||||
let srLang = "";
|
let srLang = "";
|
||||||
if (apiSetting.isBatchFetch && OPT_TRANS_BATCH.has(translator)) {
|
if (apiSetting.isBatchFetch && OPT_TRANS_BATCH.has(translator)) {
|
||||||
const queue = getBatchQueue(
|
const queue = getBatchQueue(
|
||||||
{ translator, from, to, docInfo, apiSetting, usePool },
|
{
|
||||||
|
translator,
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
docInfo,
|
||||||
|
apiSetting,
|
||||||
|
usePool,
|
||||||
|
taskFn: fetchTranslate,
|
||||||
|
},
|
||||||
apiSetting
|
apiSetting
|
||||||
);
|
);
|
||||||
const tranlation = await queue.addTask({ text });
|
const tranlation = await queue.addTask({ text });
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
import { fetchTranslate } from "../apis/trans";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批处理队列
|
* 批处理队列
|
||||||
* @param {*} translator
|
* @param {*} args
|
||||||
* @param {*} param1
|
* @param {*} param1
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const batchQueue = (
|
const batchQueue = (
|
||||||
{ translator, from, to, docInfo, apiSetting, usePool },
|
{ taskFn, ...args },
|
||||||
{ batchInterval = 1000, batchSize = 10, batchLength = 10000 } = {}
|
{ batchInterval = 1000, batchSize = 10, batchLength = 10000 } = {}
|
||||||
) => {
|
) => {
|
||||||
const queue = [];
|
const queue = [];
|
||||||
@@ -16,14 +14,9 @@ const batchQueue = (
|
|||||||
|
|
||||||
const sendBatchRequest = async (payloads) => {
|
const sendBatchRequest = async (payloads) => {
|
||||||
const texts = payloads.map((item) => item.text);
|
const texts = payloads.map((item) => item.text);
|
||||||
return fetchTranslate({
|
return taskFn({
|
||||||
translator,
|
...args,
|
||||||
texts,
|
texts,
|
||||||
from,
|
|
||||||
to,
|
|
||||||
docInfo,
|
|
||||||
apiSetting,
|
|
||||||
usePool,
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user