gpt4 book ai didi

c++ - 一些进展。发送调用没有到达 nodejs 土地

转载 作者:搜寻专家 更新时间:2023-11-01 00:37:37 27 4
gpt4 key购买 nike

我已经使用 AsyncProgressWorker 线程制作了一个 Node 插件来处理我的套接字消息。这是我的代码:

class ProgressWorker : public AsyncProgressWorker {
public:
ProgressWorker(
Callback *callback
, Callback *progress)
: AsyncProgressWorker(callback), progress(progress) {}
~ProgressWorker() {}

void Execute (const AsyncProgressWorker::ExecutionProgress& progress) {
char response[4096];
int result;
int connected = 1;
int timeout = 0;
int pending = 0;

while(connected) {
result = sctp_recvmsg(sock, (void *)&response, (size_t)sizeof(response), NULL, 0, 0, 0);
if (result > 0 && result < 4095) {
if (debug) {
printf("Server replied (size %d)\n", result);
}
pending = 0;
progress.Send((const char *)response, size_t(result));
result = 0;
}
else {
// Don't mind my timeout mechanism. :))
if ((result == -1 && errno != EWOULDBLOCK) || pending) {
if (timeout == 0) {
printf("Can't receive from other end. Waiting for 3 seconds. Error code: %d\n", errno);
pending = 1;
}
if (timeout >= 3000) {
connected = 0;
close(sock);
}
else {
timeout += 5;
usleep(5000);
}
}
else {
usleep(5000);
}
}
}

}

void HandleProgressCallback(const char *data, size_t count) {
HandleScope scope;

v8::Local<v8::Value> argv[] = {
CopyBuffer(const_cast<char*>(data), count).ToLocalChecked()
};
progress->Call(1, argv); // This is the callback to nodejs
}

private:
Callback *progress;
};

直到今晚我才对此进行压力测试,然后我注意到有些消息不会返回到 Node 。它将打印我的“服务器回复”调试日志,但不会记录我放在进度回调中的调试日志。我在这里错过了什么吗?提前致谢。

最佳答案

AsyncProgressWorker基于uv_async_t ,它允许任何线程唤醒主线程。但是,如 documentation 中所述:

libuv will coalesce calls to uv_async_send(), that is, not every call to it will yield an execution of the callback. For example: if uv_async_send() is called 5 times in a row before the callback is called, the callback will only be called once. If uv_async_send() is called again after the callback was called, it will be called again.

^^ 这就是当您的应用程序处于压力下时,您有时可能无法收到某些事件的原因。这条线上方是问题的答案。以下是我处理您的问题的“超越”可能的解决方案:

碰巧我正在为 AsyncProgressWorker 添加一个新的替代品 promise 传递每个事件,就像AsyncProgressWorker一样确实如此,但使用队列。此功能最近已合并到 NAN 中。如果您想测试它,请尝试位于 https://github.com/nodejs/nan 的 git 存储库。 , 然后替换你的 AsyncProgressWorkerAsyncProgressQueueWorker<char>重新运行您的测试,所有事件都将被传送。

添加此新功能的拉取请求在这里:https://github.com/nodejs/nan/pull/692 - 2017 年 10 月 6 日合并。

此新功能已在 NAN 2.8.0 版本中发布

您可以通过更改 package.json 来使用这个新的类模板使用 nan 2.8.0 或更高版本:

   "dependencies": {
"nan": "^2.8.0"
},

关于c++ - 一些进展。发送调用没有到达 nodejs 土地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46031901/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com