英文:
R Plumber API published to Posit Connect timeouts after 60s
问题
我们有一个与管道工API相关的问题,该API部署在Ubuntu 20.04虚拟机上的Posit Connect(版本为2023.03.0)。该API运行的进程可能需要超过60秒才能完成(它从其他API获取数据,可能超过60秒)。
每当响应时间超过60秒时,API会返回"504网关超时 - 服务器未能及时响应"错误。但是,API仍然处于活动状态,即使返回了504错误,诸如将数据写入Connect pin之类的任何进程都将在返回504错误消息后完成。因此,API在60秒超时期后仍保持活动状态。
据我所知,Posit Connect中没有除下文提到的设置以外可能导致超时的设置。我怀疑要么是安装了Posit Connect的虚拟化平台,要么是环境的防火墙导致了超时。
Posit Connect上API的运行时设置如下:
初始超时时间:600秒(从默认的60秒增加,以确保这不是原因)
连接超时时间:3600秒
空闲超时时间:120秒
读取超时时间:3600秒
我在下面附上了一个Plumber API的示例,当API的延迟参数超过60时,它也会返回504超时。
library(plumber)
library(pins)
## 配置API
#* @apiTitle 测试Plumber Posit Connect超时
#* @apiVersion 0.0.1
#* @apiDescription 重现Plumber API 60秒超时的示例
## 配置API端点
#* @get /TimeoutTest
#* @param Delay API在返回响应前应等待的时间(秒)。
#* @response default 以JSON格式返回数据框
TimeoutTest <- function(Delay) {
## 延迟API响应,时间为Delay指定的时间段
StartTime = Sys.time()
Sys.sleep(Delay)
DelayEnd = Sys.time()
## 配置API响应的有效负载
payload <- paste0("Delay: ", Delay, "s, Start Time: ", StartTime, " - End Time: ", DelayEnd)
## 配置并将有效负载写入Posit Connect Pin
board <- board_connect(
server = "https://connect.server.com",
auth = "manual",
key= Sys.getenv("CONNECT_API_KEY")
)
pin_write(board = board,
payload,
name = "API_Timeout_Pin",
type = "json")
## 以API GET请求的响应返回有效负载
return(payload)
}
如果"Delay"小于60,则API将返回JSON有效负载,并且有效负载也将写入Connect的pin。如果"Delay"超过60秒,将返回504错误。然而,响应仍然写入Pin,这意味着API进程即使在返回504后仍然保持活动状态。
英文:
We have a problem with a Plumber API which is deployed to Posit Connect (v. 2023.03.0) on an Ubuntu 20.04 virtual machine. The API runs processes which could take longer than 60s to complete (it fetches data from other APIs would could exceed 60s).
Whenever the response time is more than 60s, the API returns a "504 Gateway Time-out - The server didn't respond in time." error. However, the API remains active and even though the 504 error is returned, any processes such as writing data to a Connect pin will be completed after the 504 error message has been returned. The API therefore remains active after the 60s timeout period.
As far as I can establish, there are no settings within Posit Connect (other than that mentioned below) which could result in the timeout occurring. My suspicion is that it is either the virtualisation platform on which Posit Connect is installed, or the firewall of the environment which cause the timeout.
The Runtime Settings for the API on Posit Connect are as following:
Initial Timeout: 600s (increased from the default of 60s to ensure this is not the cause)
Connection timeout: 3600s
Idle Timeout: 120s
Read timeout: 3600s
I include a reprex below of a Plumber API which also returns a 504 timeout when the delay parameter of the API is more than 60.
library(plumber)
library(pins)
## Configure API
#* @apiTitle Testing Plumber Posit Connect Timeout
#* @apiVersion 0.0.1
#* @apiDescription Reprex to replicate Plumber API 60s timeout
## Configure API endpoint
#* @get /TimeoutTest
#* @param Delay The time in seconds which the API should wait before returning a response.
#* @response default Returns a dataframe in JSON format
TimeoutTest <- function(Delay) {
## Delay API response for the period specified in Delay
StartTime = Sys.time()
Sys.sleep(Delay)
DelayEnd = Sys.time()
## Configure API payload reponse
payload <- paste0("Delay: ", Delay, "s, Start Time: ", StartTime, " - End Time: ", DelayEnd)
## configure and write the payload to a Posit Connect Pin
board <- board_connect(
server = "https://connect.server.com",
auth = "manual",
key= Sys.getenv("CONNECT_API_KEY")
)
pin_write(board = board,
payload,
name = "API_Timeout_Pin",
type = "json")
## return payload as the API GET request response
return(payload)
}
If "Delay" is less than 60, then the API returns a JSON payload and the payloads is also written to the pin on Connect. If "Delay" is more than 60s, a 504 error is returned. However the response is still written to the Pin, implying that the API process remains active even after the 504 is returned.
答案1
得分: 1
问题已通过更正代理服务器超时设置来解决。
英文:
The problem was solved by correcting the proxy server timeout settings.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论