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

clusterClient leader invoke结果回调了2次 #54

Open
ClarkMiaguo opened this issue Apr 25, 2021 · 0 comments
Open

clusterClient leader invoke结果回调了2次 #54

ClarkMiaguo opened this issue Apr 25, 2021 · 0 comments

Comments

@ClarkMiaguo
Copy link

代码

class MongoDataClient extends Base {
    constructor(args) {
        super(args);
        this.ready(true);
    }

    async find(collectionName, ...rest) {
        rest.pop(); //需要手动去掉callback
        const db = await mongoClient;
        return db.collection(collectionName).find(...rest).toArray();
    }
}

结合leader代码

invoke(methodName, args, callback) {
    let method = this._realClient[methodName];
    // compatible with generatorFunction
    if (is.generatorFunction(method)) {
      method = co.wrap(method);
    }
    args.push(callback);
    const ret = method.apply(this._realClient, args);
    if (callback && is.promise(ret)) {
      ret.then(result => callback(null, result), err => callback(err))
        // to avoid uncaught exception in callback function, then cause unhandledRejection
        .catch(err => { this._errorHandler(err); });
    }
  }

假设在clusterClient方式,且被invoke函数是async函数, Leader在执行follower程序,即调用invoke时候, 会被callback 次,一次在method.apply(this._realClient, args); 另外一次在 ret.then(result => callback(null, result), err => callback(err))

导致的结果是发送了2次,第一次没数据,第二次真实数据

 cluster-client#leader [Leader:MongoDataClient] send method:find result to follower, result: undefined +11ms
  cluster-client#leader [Leader:MongoDataClient] send method:find result to follower, result: [...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant