Skip to content
Permalink
Browse files
refactor: dubbo-service port
  • Loading branch information
hufeng committed Aug 6, 2021
1 parent d3b044e commit db7564a27e4d8a24094825aa5baf618f380a781f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
@@ -22,6 +22,7 @@
"@types/node-zookeeper-client": "^0.2.6",
"@types/prettier": "^2.2.3",
"@types/uuid": "3.4.3",
"@types/proper-lockfile": "^4.1.2",
"chalk": "^4.1.1",
"glob": "^7.1.7",
"husky": "^6.0.0",
@@ -33,17 +33,13 @@ export class PortManager {
if (this.isMasterProcess) {
// create dubbo lock file
fs.ensureFileSync(LOCK_FILE)
} else {
this.clearPidPort()
}
// listen process exit event
// and clean port/pid file content
this.clearPidPort()
}

async getReusedPort(): Promise<number> {
if (this.isMasterProcess) {
this.port = await this.getFreePort()
return this.port
}

try {
// set file lock
const release = await lockfile.lock(LOCK_FILE, {
@@ -58,9 +54,9 @@ export class PortManager {
for (let portPid of portPidFiles) {
const file = fs.readFileSync(path.join(ROOT, portPid)).toString()
if (file === '') {
release()
fs.writeFileSync(path.join(ROOT, portPid), String(process.pid))
this.port = Number(portPid)
await release()
return this.port
} else {
excludes.push(Number(portPid))
@@ -69,7 +65,7 @@ export class PortManager {

this.port = await this.getFreePort(excludes)
fs.writeFileSync(path.join(ROOT, String(this.port)), String(process.pid))
release()
await release()
return this.port
} catch (err) {
throw err
@@ -85,7 +81,11 @@ export class PortManager {
}

const availablePort = ports.filter((port) => !exclude.includes(port))[0]
dlog('get random port %d in master mode', availablePort)
dlog(
'get random port %d in %s mode',
availablePort,
this.isMasterProcess ? 'master' : 'worker'
)
return availablePort
}

@@ -100,9 +100,11 @@ export class PortManager {
'SIGINT',
'SIGUSR2',
'SIGUSR1',
'SIGKILL',
'SIGTERM',
'uncaughtException'
].forEach((event) => {
dlog('bind %s event', event)
process.on(event, cleanup)
})
}

0 comments on commit db7564a

Please sign in to comment.