SenecaJS 服务和 Express 之间的客户端请求超时 API
Client Request Timeout between SenecaJS Service and Express API
我正在使用 SenecaJS 构建基于微服务的应用程序。到目前为止,我已经概念化了一个只包含一个动作的微服务。此操作在调用时将执行耗时的 shell 命令(大约耗时 3 分钟)和 return 作为响应 - shell 命令的输出。我的代码文件可以在这里找到:https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb
所以,我一直在尝试以两种不同的方式向该服务发出请求:首先,我使用 cURL 向服务(运行插件,osfp_tool
)发送直接请求,如此处所示:http://senecajs.org/getting-started/#writing-microservices。
其次,通过参考本教程:http://senecajs.org/getting-started/#web-server-integration,我编写了一个与我的服务(osfp_service
)通信的 Express API。因此,我将 HTTP 请求(使用 POSTMAN)发送到 Express API.
我曾经在这两种情况下都收到客户端请求超时错误。经过一些研究,我开始了解 Seneca 实例中的超时配置。因此,我在 2 个地方添加了时间配置 - 在 Seneca 服务中 (osfp_service
) 以及在 Express API (app.js
) 中。请注意,我已将超时设置为 300000 毫秒或 5 分钟。我已经检查过 shell 命令大约需要 3 分钟,所以超时设置的时间比这更长。但是,我仍然面临客户端请求超时错误,如下所示。我知道 shell 命令执行没有错误,就像我的服务日志一样,即使在我收到客户端超时请求错误之后,该操作也成功完成了执行,这可以使用 console.log 消息看到。
希望有人能帮我解决这个问题,困扰了很长时间。
编辑
所以,我一直在玩超时配置。通过在最顶层 (https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb#file-osfp_service-js-L8) 的 seneca 实例中设置超时,我能够解决 osfp_service.js
脚本的超时错误。
如果我以同样的方式在app.js
中设置超时配置(https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb#file-app2-js-L26), then I still get Error 504: Client request timeout/Gateway timeout (https://drive.google.com/open?id=1El2JCy047dnm6PHlvU33d_mKPuIWUlfX)。
如果我在 seneca 实例 (https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb#file-app1-js-L26), then I get Error 503: Response timeout/Service Unavailable (https://drive.google.com/open?id=1u6w7XyK9-vAJVhna_JnIQ4imRzOm_51T) 的传输对象内的 app.js
中设置超时配置。我不明白为什么它说服务不可用,因为操作确实执行了,甚至成功完成了。
我似乎无法理解不同的行为。
我还处理过 Seneca 的超时问题。
对于 my 应用程序,解决方案是:
在require('seneca')中设置超时:
let seneca = require('seneca')(
{
timeout: config.request_timeout,
tag: ...
}
)
在每个 act() 调用中设置超时:
seneca.act({timeout$: config.request_timeout, role: ...});
希望这对您有所帮助。
编辑:
正如在 this post 中发现的那样,还可以配置传输超时:
let seneca = require('seneca')(
{
timeout: config.request_timeout,
tag: ...,
transport: {
'web': { timeout: config.request_timeout },
'tcp': { timeout: config.request_timeout }
}
}
);
我正在使用 SenecaJS 构建基于微服务的应用程序。到目前为止,我已经概念化了一个只包含一个动作的微服务。此操作在调用时将执行耗时的 shell 命令(大约耗时 3 分钟)和 return 作为响应 - shell 命令的输出。我的代码文件可以在这里找到:https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb
所以,我一直在尝试以两种不同的方式向该服务发出请求:首先,我使用 cURL 向服务(运行插件,osfp_tool
)发送直接请求,如此处所示:http://senecajs.org/getting-started/#writing-microservices。
其次,通过参考本教程:http://senecajs.org/getting-started/#web-server-integration,我编写了一个与我的服务(osfp_service
)通信的 Express API。因此,我将 HTTP 请求(使用 POSTMAN)发送到 Express API.
我曾经在这两种情况下都收到客户端请求超时错误。经过一些研究,我开始了解 Seneca 实例中的超时配置。因此,我在 2 个地方添加了时间配置 - 在 Seneca 服务中 (osfp_service
) 以及在 Express API (app.js
) 中。请注意,我已将超时设置为 300000 毫秒或 5 分钟。我已经检查过 shell 命令大约需要 3 分钟,所以超时设置的时间比这更长。但是,我仍然面临客户端请求超时错误,如下所示。我知道 shell 命令执行没有错误,就像我的服务日志一样,即使在我收到客户端超时请求错误之后,该操作也成功完成了执行,这可以使用 console.log 消息看到。
希望有人能帮我解决这个问题,困扰了很长时间。
编辑
所以,我一直在玩超时配置。通过在最顶层 (https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb#file-osfp_service-js-L8) 的 seneca 实例中设置超时,我能够解决 osfp_service.js
脚本的超时错误。
如果我以同样的方式在app.js
中设置超时配置(https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb#file-app2-js-L26), then I still get Error 504: Client request timeout/Gateway timeout (https://drive.google.com/open?id=1El2JCy047dnm6PHlvU33d_mKPuIWUlfX)。
如果我在 seneca 实例 (https://gist.github.com/ohmtrivedi/5a94841d25714f3cfd6aee260add97bb#file-app1-js-L26), then I get Error 503: Response timeout/Service Unavailable (https://drive.google.com/open?id=1u6w7XyK9-vAJVhna_JnIQ4imRzOm_51T) 的传输对象内的 app.js
中设置超时配置。我不明白为什么它说服务不可用,因为操作确实执行了,甚至成功完成了。
我似乎无法理解不同的行为。
我还处理过 Seneca 的超时问题。 对于 my 应用程序,解决方案是:
在require('seneca')中设置超时:
let seneca = require('seneca')( { timeout: config.request_timeout, tag: ... } )
在每个 act() 调用中设置超时:
seneca.act({timeout$: config.request_timeout, role: ...});
希望这对您有所帮助。
编辑:
正如在 this post 中发现的那样,还可以配置传输超时:
let seneca = require('seneca')(
{
timeout: config.request_timeout,
tag: ...,
transport: {
'web': { timeout: config.request_timeout },
'tcp': { timeout: config.request_timeout }
}
}
);