Skip to content

Commit 34eead6

Browse files
author
Hiram
committed
[optimize] sync req use timeout global
1 parent 76b0d0e commit 34eead6

File tree

10 files changed

+46
-22
lines changed

10 files changed

+46
-22
lines changed

src/main/core/global/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const setup = async () => {
1212
debug: (await setting.get('debug')) || false,
1313
};
1414

15-
global.variable = variable;
15+
globalThis.variable = variable;
1616
logger.info(`[global][variable]`, variable);
1717
};
1818

src/main/core/ipc/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const ipcListen = () => {
7777
});
7878

7979
ipcMain.handle('ffmpeg-thumbnail', async (_, url, key) => {
80-
const ua = global.variable.ua;
80+
const ua = globalThis.variable.ua;
8181
const formatPath = is.dev
8282
? join(process.cwd(), 'thumbnail', `${key}.jpg`)
8383
: join(app.getPath('userData'), 'thumbnail', `${key}.jpg`);
@@ -184,7 +184,7 @@ const ipcListen = () => {
184184
globalShortcut.register(shortcut, () => {
185185
toggleWindowVisibility();
186186
});
187-
global.variable.recordShortcut = shortcut;
187+
globalThis.variable.recordShortcut = shortcut;
188188
});
189189

190190
ipcMain.on('manage-win', (_, winName, action) => {
@@ -252,8 +252,8 @@ const ipcListen = () => {
252252

253253
ipcMain.on('update-global', (_, key, value) => {
254254
logger.info(`[ipcMain] update-global: key: ${key} - value: ${value}`);
255-
if (Object.keys(global.variable).includes(key)) {
256-
global.variable[key] = value;
255+
if (Object.keys(globalThis.variable).includes(key)) {
256+
globalThis.variable[key] = value;
257257
}
258258
});
259259

src/main/core/server/routes/v1/lab/ai/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function createOpenAI(config: CreateOpenAIConfig = {}): OpenAIApp {
9292

9393
const spinner = ora(logStart(`AI is answering your question, please wait a moment`)).start();
9494
try {
95-
const timeout = global.variable.timeout || 5000;
95+
const timeout = globalThis.variable.timeout || 5000;
9696
const completion = await openai.chat.completions.create(
9797
{
9898
model,

src/main/core/server/routes/v1/site/cms/adapter/drpy/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import treeKill from 'tree-kill';
66
import logger from '@main/core/logger';
77

88
let child: ChildProcess | null = null;
9-
const restart = (): void => {
9+
const restart = async () => {
1010
logger.warn(`[t3][worker][restart] worker id:${uuidv4()}`);
1111
if (child) {
1212
child.removeAllListeners();
1313
treeKill(child.pid!, 'SIGTERM');
1414
}
1515
child = null;
16-
child = fork(resolve(__dirname, 'worker.js'), [`T3DrpyWorker-${uuidv4()}`]);
16+
const TIMEOUT = globalThis.variable.timeout;
17+
child = fork(resolve(__dirname, 'worker.js'), [`T3DrpyWorker-${uuidv4()}`, TIMEOUT]);
1718
};
1819

1920
const doWork = (data: { [key: string]: string | object | null }): Promise<{ [key: string]: any }> => {
@@ -61,7 +62,7 @@ class T3Adapter {
6162

6263
private async getInstance() {
6364
if (!this.instance) {
64-
restart();
65+
await restart();
6566
this.instance = await doWork({ type: 'init', data: this.ext });
6667
}
6768
return this.instance;

src/main/core/server/routes/v1/site/cms/adapter/drpy/worker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ const drpyWork = (parms) => {
6464
};
6565

6666
process.on('message', (message: { [key: string]: any }) => {
67+
const variable = {
68+
timeout: process.argv?.[3] || 3000,
69+
};
70+
globalThis.variable = variable;
6771
let res;
6872
try {
6973
res = drpyWork(message);

src/main/core/shortcut.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const customShortcutResgin = (shortcut, func) => {
99
};
1010

1111
const bossShortcutResgin = () => {
12-
if (global.variable.recordShortcut) {
13-
globalShortcut.register(global.variable.recordShortcut, () => {
12+
if (globalThis.variable.recordShortcut) {
13+
globalShortcut.register(globalThis.variable.recordShortcut, () => {
1414
toggleWindowVisibility();
1515
});
1616
}

src/main/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { initialize as remoteInit } from '@electron/remote/main';
2-
import { electronApp, is, optimizer } from '@electron-toolkit/utils';
2+
import { electronApp, platform, optimizer } from '@electron-toolkit/utils';
33
import { registerContextMenuListener } from '@electron-uikit/contextmenu';
44
import { registerTitleBarListener } from '@electron-uikit/titlebar';
55
import { app, BrowserWindow, globalShortcut, nativeTheme, session } from 'electron';
@@ -41,6 +41,10 @@ const setup = async () => {
4141
app.commandLine.appendSwitch('disable-web-security'); // 禁用安全
4242
app.commandLine.appendSwitch('gpu-memory-buffer-compositor-resources'); // GPU内存缓冲
4343

44+
if (platform.isLinux) {
45+
app.disableHardwareAcceleration();
46+
}
47+
4448
remoteInit(); // 主进程初始化
4549
await dbInit(); // 初始化数据库
4650
await globalVariable(); // 全局变量
@@ -55,11 +59,11 @@ const ready = () => {
5559
// initialization and is ready to create browser windows.
5660
// Some APIs can only be used after this event occurs.
5761
app.whenReady().then(() => {
58-
if (global.variable.dns) {
59-
logger.info(`[dns] doh: ${global.variable.dns}`);
62+
if (globalThis.variable.dns) {
63+
logger.info(`[dns] doh: ${globalThis.variable.dns}`);
6064
app.configureHostResolver({
6165
secureDnsMode: 'secure',
62-
secureDnsServers: [global.variable.dns],
66+
secureDnsServers: [globalThis.variable.dns],
6367
});
6468
}
6569

@@ -111,7 +115,8 @@ const ready = () => {
111115
}
112116

113117
// 处理 User-Agent
114-
requestHeaders['User-Agent'] = headers?.['User-Agent'] || requestHeaders?.['User-Agent'] || global.variable.ua;
118+
requestHeaders['User-Agent'] =
119+
headers?.['User-Agent'] || requestHeaders?.['User-Agent'] || globalThis.variable.ua;
115120
// 处理 Host
116121
requestHeaders['Host'] = headers?.['Host'] || requestHeaders?.['Host'] || new URL(url).host;
117122
// 处理 Cookie

src/main/utils/hiker/request.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import { getHome, keysToLowerCase, parseQueryString } from './base';
77

88
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
99

10+
const getTimeout = (timeout: number | undefined | null) => {
11+
const baseTimeout = 3000;
12+
13+
if (timeout !== null && timeout !== undefined) {
14+
return Math.max(baseTimeout, timeout);
15+
}
16+
17+
if (globalThis.variable?.timeout) {
18+
return Math.max(baseTimeout, globalThis.variable.timeout);
19+
}
20+
21+
return baseTimeout;
22+
};
23+
1024
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
1125

1226
interface RequestOptions {
@@ -41,7 +55,7 @@ const fetch = (url: string, options: RequestOptions = {}) => {
4155

4256
const config = {
4357
headers: headersInTitleCase,
44-
timeout: options?.timeout || 5000,
58+
timeout: getTimeout(options?.timeout),
4559
followRedirects: options?.redirect === false ? false : true,
4660
};
4761

src/main/utils/request/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
22
import PQueue from 'p-queue';
33

4-
const getTimeout = (timeout: number | undefined) => {
4+
const getTimeout = (timeout: number | undefined | null) => {
55
const baseTimeout = 3000;
66

7-
if (timeout != null) {
7+
if (timeout !== null && timeout !== undefined) {
88
return Math.max(baseTimeout, timeout);
99
}
1010

11-
if (global.variable?.timeout) {
12-
return Math.max(baseTimeout, global.variable.timeout);
11+
if (globalThis.variable?.timeout) {
12+
return Math.max(baseTimeout, globalThis.variable.timeout);
1313
}
1414

1515
return baseTimeout;

src/main/utils/sniffer/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const puppeteerInElectron = async (
5858

5959
try {
6060
const browser = await pie.connect(app, puppeteer as any); // 连接puppeteer
61-
const debugStatus = global.variable.debug || false;
61+
const debugStatus = globalThis.variable.debug || false;
6262
snifferWindow = createWin(`sniffer-${pageId}`, { title: 'zy-sniifer', show: debugStatus }); // 创建无界面窗口
6363
snifferWindow.webContents.setAudioMuted(true); // 设置窗口静音
6464
snifferWindow.webContents.setWindowOpenHandler(() => {

0 commit comments

Comments
 (0)