mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 01:39:52 +08:00
96 lines
2.4 KiB
TypeScript
96 lines
2.4 KiB
TypeScript
import express from 'express';
|
|
import Logger from '../loaders/logger';
|
|
import { metricsService } from './metrics';
|
|
import { Service } from 'typedi';
|
|
import { Server } from 'http';
|
|
import config from '../config';
|
|
|
|
@Service()
|
|
export class HttpServerService {
|
|
private server?: Server = undefined;
|
|
|
|
async initialize(expressApp: express.Application, port: number) {
|
|
const hostsToTry = [
|
|
config.bindHost,
|
|
...(config.bindHost === '::' ? ['0.0.0.0'] : []),
|
|
];
|
|
|
|
let lastError: Error | null = null;
|
|
|
|
for (const host of hostsToTry) {
|
|
try {
|
|
const server = await this.tryListen(expressApp, port, host);
|
|
Logger.debug(
|
|
`[boot] HTTP service started successfully on ${host}:${port}`,
|
|
);
|
|
metricsService.record('http_service_start', 1, {
|
|
port: port.toString(),
|
|
host,
|
|
});
|
|
this.server = server;
|
|
return server;
|
|
} catch (err) {
|
|
lastError = err as Error;
|
|
Logger.warn(
|
|
`Failed to bind HTTP on ${host}:${port}, trying next...`,
|
|
err,
|
|
);
|
|
}
|
|
}
|
|
|
|
Logger.error('Failed to start HTTP service on all hosts');
|
|
throw lastError || new Error('Failed to start HTTP service');
|
|
}
|
|
|
|
private async tryListen(
|
|
expressApp: express.Application,
|
|
port: number,
|
|
host: string,
|
|
): Promise<Server> {
|
|
return new Promise((resolve, reject) => {
|
|
// There is one HTTP worker; accepting here avoids primary IPC handoff
|
|
// for every connection. Restore shared listening for custom clusters.
|
|
const server = expressApp.listen(
|
|
{
|
|
port,
|
|
host,
|
|
exclusive: process.env.QL_HTTP_SHARED_LISTEN !== 'true',
|
|
},
|
|
() => {
|
|
resolve(server);
|
|
},
|
|
);
|
|
|
|
server.requestTimeout = 300000;
|
|
server.headersTimeout = 120000;
|
|
server.keepAliveTimeout = 65000;
|
|
|
|
server.on('error', (err: Error) => {
|
|
server.close();
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
async shutdown() {
|
|
try {
|
|
if (this.server) {
|
|
await new Promise((resolve) => {
|
|
this.server?.close(() => {
|
|
Logger.debug('HTTP service stopped');
|
|
metricsService.record('http_service_stop', 1);
|
|
resolve(null);
|
|
});
|
|
});
|
|
}
|
|
} catch (err) {
|
|
Logger.error('Error while shutting down HTTP service:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
getServer() {
|
|
return this.server;
|
|
}
|
|
}
|