Skip to content
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

refactor: app.cluster auto bind this #570

Merged
merged 2 commits into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/zh-cn/basics/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ app.verb('router-name', 'path-match', middleware1, ..., middlewareN, 'controller
- router-name 给路由设定一个别名,可以通过 Helper 提供的辅助函数 `pathFor` 和 `urlFor` 来生成 URL。(可选)
- path-match - 路由 URL 路径。
- middleware1 - 在 Router 里面可以配置多个 Middleware。(可选)
- controller.action - 注意是字符串,框架会自动从 `app/controller` 目录中区查找同名 Controller,
- controller.action - 注意是字符串,框架会自动从 `app/controller` 目录中去查找同名 Controller,
并且把处理指定到配置的 action 方法。如果 Controller 文件直接 export 一个方法,可以省略 action。

### 注意事项
Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是这样的吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等下 加个用例

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放这里有用么? 下面的 this 还是取不到吧,感觉需要将方法定义在下面,然后 this.cluster = this.cluster.bind(this) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他是 arrow function

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@fengmk2 fengmk2 Mar 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options.cluster = app.cluster;
app.registryClient = new RegistryClient(options);

这样才对的

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦,这里的 RegistryClient 是 DataClient

app.registryClient.ready(done);

app.registryClient.subscribe({
Expand Down