grpc-node icon indicating copy to clipboard operation
grpc-node copied to clipboard

High concurrent environment will lead to slow response?

Open pigLoveRabbit520 opened this issue 4 years ago • 2 comments

Problem description

I add a interceptor for grpc client to log :

const interceptor = function(options, nextCall) {
    const requester = {
    start: function(metadata, listener, next) {
        var newListener = {
        onReceiveMetadata: function(metadata, next) {
            next(metadata);
        },
        onReceiveMessage: function(message, next) {
            const responseTime = (new Date()).getTime() - (options.bxhStartTime as Date).getTime();
            // exceeds 50ms to log
            if (responseTime >= 50) {
            const dayBeginUnixTime = Math.floor(new Date().setHours(0, 0, 0, 0) / 1000);
            redis.lpush(slowKey, JSON.stringify({
                responseTime: responseTime,
                path: options.method_definition.path,
                time: new Date(),
            }));
            }
            next(message);
        },
        onReceiveStatus: function(status, next) {
            next(status);
        }
        };
        next(metadata, newListener);
    },
    sendMessage: function(message, next) {
        options.bxhStartTime = new Date();
        next(message);
    },
    };
    return new grpc.InterceptingCall(nextCall(options), requester);
};

Sometimes response time can reach 1012ms:

{
  "responseTime": 1012,
  "path": "********",
  "time": "2021-08-31T07:20:05.653Z"
}

This problem will result in slow http response and I want to know hot to fix it temporarily😭.

Reproduction steps

Only will happen in high concurrent environment.

Environment

  • OS name [Ubuntu 20.04 amd64]
  • Node version [v14.17.1]
  • Package name and version ["@grpc/grpc-js": "^1.3.7"]

Additional context

I will invoke many rpc service in one method:

const allResult = await Promise.all([
    rpcService.getA(),
    rpcService.getB(),
    rpcService.getC(),
    rpcService.getD(),
    rpcService.getE(),
    rpcService.getF(),
    rpcService.getG(),
    rpcService.getH(),
    rpcService.getI(),
    rpcService.getJ(),
]);

Does it matter?

pigLoveRabbit520 avatar Aug 31 '21 08:08 pigLoveRabbit520

I am sorry for not responding to this earlier. This is expected behavior. Node is single-threaded, so if you try to do more things, it will take more time. Eventually, as the number of concurrent requests increases, the server will take longer to respond to individual requests because it is busy handling other requests.

murgatroid99 avatar Nov 30 '21 14:11 murgatroid99