fix: reserve web ui port for gateways (#746)
This commit is contained in:
@@ -49,6 +49,7 @@ const execFileAsync = promisify(execFile)
|
|||||||
|
|
||||||
const HERMES_BASE = detectHermesHome()
|
const HERMES_BASE = detectHermesHome()
|
||||||
const HERMES_BIN = getHermesBin()
|
const HERMES_BIN = getHermesBin()
|
||||||
|
const DEFAULT_WEB_UI_PORT = 8648
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检测系统的 init 系统(服务管理器)
|
* 检测系统的 init 系统(服务管理器)
|
||||||
@@ -174,6 +175,11 @@ export function buildGatewayProcessEnv(profileName: string, hermesHome: string):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getWebUiPort(): number | null {
|
||||||
|
const port = parseInt(process.env.PORT || String(DEFAULT_WEB_UI_PORT), 10)
|
||||||
|
return port > 0 && port <= 65535 ? port : null
|
||||||
|
}
|
||||||
|
|
||||||
// ============================
|
// ============================
|
||||||
// 类型定义
|
// 类型定义
|
||||||
// ============================
|
// ============================
|
||||||
@@ -472,6 +478,8 @@ export class GatewayManager {
|
|||||||
usedPorts.add(gw.port)
|
usedPorts.add(gw.port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const webUiPort = getWebUiPort()
|
||||||
|
if (webUiPort !== null) usedPorts.add(webUiPort)
|
||||||
|
|
||||||
const port = await this.findFreePort(8642, host, usedPorts)
|
const port = await this.findFreePort(8642, host, usedPorts)
|
||||||
if (configuredPort !== port) {
|
if (configuredPort !== port) {
|
||||||
|
|||||||
@@ -170,3 +170,20 @@ describe('GatewayManager gateway process env', () => {
|
|||||||
expect(env.CUSTOM_GATEWAY_SETTING).toBe('from-parent')
|
expect(env.CUSTOM_GATEWAY_SETTING).toBe('from-parent')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('GatewayManager gateway port allocation', () => {
|
||||||
|
it('skips the Web UI listen port when assigning gateway ports', async () => {
|
||||||
|
const home = createHermesHome()
|
||||||
|
mkdirSync(join(home, 'profiles', 'work'), { recursive: true })
|
||||||
|
process.env.HERMES_HOME = home
|
||||||
|
process.env.PORT = '8648'
|
||||||
|
vi.resetModules()
|
||||||
|
const { GatewayManager } = await import('../../packages/server/src/services/hermes/gateway-manager')
|
||||||
|
const manager = new GatewayManager('default') as any
|
||||||
|
manager.allocatedPorts = new Set([8642, 8643, 8644, 8645, 8646, 8647])
|
||||||
|
|
||||||
|
const endpoint = await manager.resolvePort('work')
|
||||||
|
|
||||||
|
expect(endpoint.port).toBe(8649)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user