feat: add support for request timeout settings in the HTTP request node. (#3854)

Co-authored-by: Yeuoly <admin@srmxy.cn>
This commit is contained in:
majian
2024-04-29 13:59:07 +08:00
committed by GitHub
parent 2f84d00300
commit 8f2ae51fe5
17 changed files with 286 additions and 10 deletions

View File

@@ -35,9 +35,15 @@ class HttpRequestNodeData(BaseNodeData):
type: Literal['none', 'form-data', 'x-www-form-urlencoded', 'raw-text', 'json']
data: Union[None, str]
class Timeout(BaseModel):
connect: int
read: int
write: int
method: Literal['get', 'post', 'put', 'patch', 'delete', 'head']
url: str
authorization: Authorization
headers: str
params: str
body: Optional[Body]
body: Optional[Body]
timeout: Optional[Timeout]

View File

@@ -13,7 +13,6 @@ from core.workflow.entities.variable_pool import ValueType, VariablePool
from core.workflow.nodes.http_request.entities import HttpRequestNodeData
from core.workflow.utils.variable_template_parser import VariableTemplateParser
HTTP_REQUEST_DEFAULT_TIMEOUT = (10, 60)
MAX_BINARY_SIZE = 1024 * 1024 * 10 # 10MB
READABLE_MAX_BINARY_SIZE = '10MB'
MAX_TEXT_SIZE = 1024 * 1024 // 10 # 0.1MB
@@ -137,14 +136,16 @@ class HttpExecutor:
files: Union[None, dict[str, Any]]
boundary: str
variable_selectors: list[VariableSelector]
timeout: HttpRequestNodeData.Timeout
def __init__(self, node_data: HttpRequestNodeData, variable_pool: Optional[VariablePool] = None):
def __init__(self, node_data: HttpRequestNodeData, timeout: HttpRequestNodeData.Timeout, variable_pool: Optional[VariablePool] = None):
"""
init
"""
self.server_url = node_data.url
self.method = node_data.method
self.authorization = node_data.authorization
self.timeout = timeout
self.params = {}
self.headers = {}
self.body = None
@@ -307,7 +308,7 @@ class HttpExecutor:
'url': self.server_url,
'headers': headers,
'params': self.params,
'timeout': HTTP_REQUEST_DEFAULT_TIMEOUT,
'timeout': (self.timeout.connect, self.timeout.read, self.timeout.write),
'follow_redirects': True
}

View File

@@ -1,4 +1,5 @@
import logging
import os
from mimetypes import guess_extension
from os import path
from typing import cast
@@ -12,18 +13,49 @@ from core.workflow.nodes.http_request.entities import HttpRequestNodeData
from core.workflow.nodes.http_request.http_executor import HttpExecutor, HttpExecutorResponse
from models.workflow import WorkflowNodeExecutionStatus
MAX_CONNECT_TIMEOUT = int(os.environ.get('HTTP_REQUEST_MAX_CONNECT_TIMEOUT', '300'))
MAX_READ_TIMEOUT = int(os.environ.get('HTTP_REQUEST_MAX_READ_TIMEOUT', '600'))
MAX_WRITE_TIMEOUT = int(os.environ.get('HTTP_REQUEST_MAX_WRITE_TIMEOUT', '600'))
HTTP_REQUEST_DEFAULT_TIMEOUT = HttpRequestNodeData.Timeout(connect=min(10, MAX_CONNECT_TIMEOUT),
read=min(60, MAX_READ_TIMEOUT),
write=min(20, MAX_WRITE_TIMEOUT))
class HttpRequestNode(BaseNode):
_node_data_cls = HttpRequestNodeData
node_type = NodeType.HTTP_REQUEST
@classmethod
def get_default_config(cls) -> dict:
return {
"type": "http-request",
"config": {
"method": "get",
"authorization": {
"type": "no-auth",
},
"body": {
"type": "none"
},
"timeout": {
**HTTP_REQUEST_DEFAULT_TIMEOUT.dict(),
"max_connect_timeout": MAX_CONNECT_TIMEOUT,
"max_read_timeout": MAX_READ_TIMEOUT,
"max_write_timeout": MAX_WRITE_TIMEOUT,
}
},
}
def _run(self, variable_pool: VariablePool) -> NodeRunResult:
node_data: HttpRequestNodeData = cast(self._node_data_cls, self.node_data)
# init http executor
http_executor = None
try:
http_executor = HttpExecutor(node_data=node_data, variable_pool=variable_pool)
http_executor = HttpExecutor(node_data=node_data,
timeout=self._get_request_timeout(node_data),
variable_pool=variable_pool)
# invoke http executor
response = http_executor.invoke()
@@ -38,7 +70,7 @@ class HttpRequestNode(BaseNode):
error=str(e),
process_data=process_data
)
files = self.extract_files(http_executor.server_url, response)
return NodeRunResult(
@@ -54,6 +86,16 @@ class HttpRequestNode(BaseNode):
}
)
def _get_request_timeout(self, node_data: HttpRequestNodeData) -> HttpRequestNodeData.Timeout:
timeout = node_data.timeout
if timeout is None:
return HTTP_REQUEST_DEFAULT_TIMEOUT
timeout.connect = min(timeout.connect, MAX_CONNECT_TIMEOUT)
timeout.read = min(timeout.read, MAX_READ_TIMEOUT)
timeout.write = min(timeout.write, MAX_WRITE_TIMEOUT)
return timeout
@classmethod
def _extract_variable_selector_to_variable_mapping(cls, node_data: HttpRequestNodeData) -> dict[str, list[str]]:
"""
@@ -62,7 +104,7 @@ class HttpRequestNode(BaseNode):
:return:
"""
try:
http_executor = HttpExecutor(node_data=node_data)
http_executor = HttpExecutor(node_data=node_data, timeout=HTTP_REQUEST_DEFAULT_TIMEOUT)
variable_selectors = http_executor.variable_selectors
@@ -84,7 +126,7 @@ class HttpRequestNode(BaseNode):
# if not image, return directly
if 'image' not in mimetype:
return files
if mimetype:
# extract filename from url
filename = path.basename(url)