From b9f8c5b4a219a9b3dd2a07a3967e8c360bcc8a98 Mon Sep 17 00:00:00 2001 From: "xiaochen.gaoxc" Date: Tue, 14 Mar 2017 19:32:18 +0800 Subject: [PATCH] refactor: app.cluster auto bind this --- lib/egg.js | 61 +++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/lib/egg.js b/lib/egg.js index 479ef6e586..eac26569e2 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -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()) { @@ -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) => {