Skip to content

Commit

Permalink
refactor: app.cluster auto bind this (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored and fengmk2 committed Mar 18, 2017
1 parent 4687f0f commit 984d732
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
61 changes: 30 additions & 31 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ class EggApplication extends EggCore {

this[CLUSTER_CLIENTS] = [];

/**
* Wrap the Client with Leader/Follower Pattern
*
* @description almost the same as Agent.cluster API, the only different is that this method create Follower.
*
* @see https://github.com/node-modules/cluster-client
* @param {Function} clientClass - client class function
* @param {Object} [options]
* - {Boolean} [autoGenerate] - whether generate delegate rule automatically, default is true
* - {Function} [formatKey] - a method to tranform the subscription info into a string,default is JSON.stringify
* - {Object} [transcode|JSON.stringify/parse]
* - {Function} encode - custom serialize method
* - {Function} decode - custom deserialize method
* - {Boolean} [isBroadcast] - whether broadcast subscrption result to all followers or just one, default is true
* - {Number} [responseTimeout] - response timeout, default is 3 seconds
* - {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
* @return {ClientWrapper} wrapper
*/
this.cluster = (clientClass, options) => {
options = options || {};
// cluster need a port that can't conflict on the environment
options.port = this._options.clusterPort;
// agent worker is leader, app workers are follower
options.isLeader = this.type === 'agent';
options.logger = this.coreLogger;
const client = cluster(clientClass, options);
this._patchClusterClient(client);
return client;
};

// register close function
this.beforeClose(() => {
for (const logger of this.loggers.values()) {
Expand Down Expand Up @@ -317,37 +347,6 @@ class EggApplication extends EggCore {
singleton.init();
}


/**
* Wrap the Client with Leader/Follower Pattern
*
* @description almost the same as Agent.cluster API, the only different is that this method create Follower.
*
* @see https://github.com/node-modules/cluster-client
* @param {Function} clientClass - client class function
* @param {Object} [options]
* - {Boolean} [autoGenerate] - whether generate delegate rule automatically, default is true
* - {Function} [formatKey] - a method to tranform the subscription info into a string,default is JSON.stringify
* - {Object} [transcode|JSON.stringify/parse]
* - {Function} encode - custom serialize method
* - {Function} decode - custom deserialize method
* - {Boolean} [isBroadcast] - whether broadcast subscrption result to all followers or just one, default is true
* - {Number} [responseTimeout] - response timeout, default is 3 seconds
* - {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
* @return {ClientWrapper} wrapper
*/
cluster(clientClass, options) {
options = options || {};
// cluster need a port that can't conflict on the environment
options.port = this._options.clusterPort;
// agent worker is leader, app workers are follower
options.isLeader = this.type === 'agent';
options.logger = this.coreLogger;
const client = cluster(clientClass, options);
this._patchClusterClient(client);
return client;
}

_patchClusterClient(client) {
const create = client.create;
client.create = (...args) => {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/apps/cluster_mod_app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = function(app) {
const done = app.readyCallback('register_client', {
isWeakDep: app.config.runMode === 0,
});
app.registryClient = app.cluster(RegistryClient).create();
const cluster = app.cluster;
app.registryClient = cluster(RegistryClient).create();
app.registryClient.ready(done);

app.registryClient.subscribe({
Expand Down

0 comments on commit 984d732

Please sign in to comment.