-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow worker listen different port to support nginx sticky #94
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ const terminate = require('./utils/terminate'); | |
const PROTOCOL = Symbol('Master#protocol'); | ||
const REAL_PORT = Symbol('Master#real_port'); | ||
const APP_ADDRESS = Symbol('Master#appAddress'); | ||
const WORKER_ADDRESSES = Symbol('Master#workerAddresses'); | ||
|
||
class Master extends EventEmitter { | ||
|
||
|
@@ -38,6 +39,7 @@ class Master extends EventEmitter { | |
* - {Object} [https] https options, { key, cert, ca }, full path | ||
* - {Array|String} [require] will inject into worker/agent process | ||
* - {String} [pidFile] will save master pid to this file | ||
* - {boolean} [nginxSticky] - use nginx sticky mode, default false | ||
*/ | ||
constructor(options) { | ||
super(); | ||
|
@@ -60,6 +62,7 @@ class Master extends EventEmitter { | |
if (process.env.EGG_SERVER_ENV === 'local' || process.env.NODE_ENV === 'development') { | ||
this.logMethod = 'debug'; | ||
} | ||
this[WORKER_ADDRESSES] = []; | ||
|
||
// get the real framework info | ||
const frameworkPath = this.options.framework; | ||
|
@@ -84,9 +87,14 @@ class Master extends EventEmitter { | |
|
||
this.ready(() => { | ||
this.isStarted = true; | ||
const stickyMsg = this.options.sticky ? ' with STICKY MODE!' : ''; | ||
this.logger.info('[master] %s started on %s (%sms)%s', | ||
frameworkPkg.name, this[APP_ADDRESS], Date.now() - startTime, stickyMsg); | ||
if (this.options.nginxSticky) { | ||
this.logger.info('[master] %s started on %j (%sms) with NGINX STICKY MODE!', | ||
frameworkPkg.name, this[WORKER_ADDRESSES].map(getAddress), Date.now() - startTime); | ||
} else { | ||
const stickyMsg = this.options.sticky ? ' with STICKY MODE!' : ''; | ||
this.logger.info('[master] %s started on %s (%sms)%s', | ||
frameworkPkg.name, this[APP_ADDRESS], Date.now() - startTime, stickyMsg); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the different between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const action = 'egg-ready'; | ||
this.messenger.send({ | ||
|
@@ -361,17 +369,6 @@ class Master extends EventEmitter { | |
from: 'app', | ||
}); | ||
}); | ||
cluster.on('listening', (worker, address) => { | ||
this.messenger.send({ | ||
action: 'app-start', | ||
data: { | ||
workerPid: worker.process.pid, | ||
address, | ||
}, | ||
to: 'master', | ||
from: 'app', | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. send port to master after egg ready and before server listen. |
||
} | ||
|
||
/** | ||
|
@@ -547,16 +544,12 @@ class Master extends EventEmitter { | |
onAppStart(data) { | ||
const worker = this.workerManager.getWorker(data.workerPid); | ||
const address = data.address; | ||
|
||
// worker should listen stickyWorkerPort when sticky mode | ||
if (this.options.sticky) { | ||
if (String(address.port) !== String(this.options.stickyWorkerPort)) { | ||
return; | ||
} | ||
// worker should listen REALPORT when not sticky mode | ||
} else if (!isUnixSock(address) | ||
&& (String(address.port) !== String(this[REAL_PORT]))) { | ||
return; | ||
address.protocol = this[PROTOCOL]; | ||
if (this.options.nginxSticky) { | ||
this[WORKER_ADDRESSES].push(address); | ||
} else { | ||
address.port = this.options.sticky ? this[REAL_PORT] : address.port; | ||
this[APP_ADDRESS] = getAddress(address); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why delete |
||
|
||
// send message to agent with alive workers | ||
|
@@ -598,10 +591,6 @@ class Master extends EventEmitter { | |
worker.disableRefork = false; | ||
} | ||
|
||
address.protocol = this[PROTOCOL]; | ||
address.port = this.options.sticky ? this[REAL_PORT] : address.port; | ||
this[APP_ADDRESS] = getAddress(address); | ||
|
||
if (this.options.sticky) { | ||
this.startMasterSocketServer(err => { | ||
if (err) return this.ready(err); | ||
|
@@ -720,7 +709,3 @@ function getAddress({ | |
const hostname = address || '127.0.0.1'; | ||
return `${protocol}://${hostname}:${port}`; | ||
} | ||
|
||
function isUnixSock(address) { | ||
return address.addressType === -1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
// don't use the port that egg-mock defined | ||
app.options.port = undefined; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
exports.index = function* () { | ||
this.body = 'hi cluster'; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
// GET / 302 to /portal/i.htm | ||
app.redirect('/', '/portal/i.htm', 302); | ||
|
||
// GET /portal/i.htm => controllers/home.js | ||
app.get('/portal/i.htm', app.controller.home.index); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const { worker } = require('cluster'); | ||
|
||
module.exports = { | ||
keys: '123', | ||
cluster: { | ||
listen: { | ||
port: worker ? 17010 + worker.id : 17010, | ||
}, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "cluster_mod_nginx_sticky" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why delete this one?