mirror of
https://github.com/whyour/qinglong.git
synced 2026-02-12 14:05:38 +08:00
Merge 855f591992 into d53437d169
This commit is contained in:
commit
d2a32918e4
|
|
@ -206,6 +206,7 @@ export default (app: Router) => {
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
let { filename, content, path } = req.body as {
|
let { filename, content, path } = req.body as {
|
||||||
filename: string;
|
filename: string;
|
||||||
|
|
@ -223,6 +224,7 @@ export default (app: Router) => {
|
||||||
await writeFileWithLock(filePath, content);
|
await writeFileWithLock(filePath, content);
|
||||||
return res.send({ code: 200 });
|
return res.send({ code: 200 });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
logger.error('🔥 error saving script: %o', e);
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,17 @@ export class HttpServerService {
|
||||||
metricsService.record('http_service_start', 1, {
|
metricsService.record('http_service_start', 1, {
|
||||||
port: port.toString(),
|
port: port.toString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Set server timeouts to prevent premature connection drops
|
||||||
|
if (this.server) {
|
||||||
|
// Timeout for receiving the entire request (including body) - 5 minutes
|
||||||
|
this.server.requestTimeout = 300000;
|
||||||
|
// Timeout for headers - 2 minutes
|
||||||
|
this.server.headersTimeout = 120000;
|
||||||
|
// Keep-alive timeout - 65 seconds (slightly more than typical load balancer timeout)
|
||||||
|
this.server.keepAliveTimeout = 65000;
|
||||||
|
}
|
||||||
|
|
||||||
resolve(this.server);
|
resolve(this.server);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import { lock } from 'proper-lockfile';
|
import { lock } from 'proper-lockfile';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { writeFile, open, chmod } from 'fs/promises';
|
import { writeFile, open, chmod, FileHandle } from 'fs/promises';
|
||||||
import { fileExist } from '../config/util';
|
import { fileExist } from '../config/util';
|
||||||
|
import Logger from '../loaders/logger';
|
||||||
|
|
||||||
function getUniqueLockPath(filePath: string) {
|
function getUniqueLockPath(filePath: string) {
|
||||||
const sanitizedPath = filePath
|
const sanitizedPath = filePath
|
||||||
|
|
@ -19,24 +20,61 @@ export async function writeFileWithLock(
|
||||||
if (typeof options === 'string') {
|
if (typeof options === 'string') {
|
||||||
options = { encoding: options };
|
options = { encoding: options };
|
||||||
}
|
}
|
||||||
if (!(await fileExist(filePath))) {
|
|
||||||
const fileHandle = await open(filePath, 'w');
|
|
||||||
fileHandle.close();
|
|
||||||
}
|
|
||||||
const lockfilePath = getUniqueLockPath(filePath);
|
|
||||||
|
|
||||||
const release = await lock(filePath, {
|
// Ensure file exists before locking
|
||||||
retries: {
|
if (!(await fileExist(filePath))) {
|
||||||
retries: 10,
|
let fileHandle: FileHandle | undefined;
|
||||||
factor: 2,
|
try {
|
||||||
minTimeout: 100,
|
fileHandle = await open(filePath, 'w');
|
||||||
maxTimeout: 3000,
|
} catch (error) {
|
||||||
},
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||||
lockfilePath,
|
throw new Error(`Failed to create file ${filePath}: ${errorMessage}`);
|
||||||
});
|
} finally {
|
||||||
await writeFile(filePath, content, { encoding: 'utf8', ...options });
|
if (fileHandle !== undefined) {
|
||||||
if (options?.mode) {
|
try {
|
||||||
await chmod(filePath, options.mode);
|
await fileHandle.close();
|
||||||
|
} catch (closeError) {
|
||||||
|
// Log close error but don't throw to avoid masking the original error
|
||||||
|
Logger.error(`Failed to close file handle for ${filePath}:`, closeError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const lockfilePath = getUniqueLockPath(filePath);
|
||||||
|
let release: (() => Promise<void>) | undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
release = await lock(filePath, {
|
||||||
|
retries: {
|
||||||
|
retries: 10,
|
||||||
|
factor: 2,
|
||||||
|
minTimeout: 100,
|
||||||
|
maxTimeout: 3000,
|
||||||
|
},
|
||||||
|
lockfilePath,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||||
|
throw new Error(`Failed to acquire lock for ${filePath}: ${errorMessage}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await writeFile(filePath, content, { encoding: 'utf8', ...options });
|
||||||
|
if (options?.mode) {
|
||||||
|
await chmod(filePath, options.mode);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||||
|
throw new Error(`Failed to write to file ${filePath}: ${errorMessage}`);
|
||||||
|
} finally {
|
||||||
|
if (release) {
|
||||||
|
try {
|
||||||
|
await release();
|
||||||
|
} catch (error) {
|
||||||
|
// Log but don't throw on release failure
|
||||||
|
Logger.error(`Failed to release lock for ${filePath}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
await release();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user