mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-23 02:05:27 -04:00
33c023b375
* refactor(plugin_manager): enhance HTTP client timeout handling in serverless runtime with DialContext - Updated the HTTP client in the ServerlessPluginRuntime to use a context-aware DialContext for better timeout management. - Removed the write timeout option from the HTTP request builder, streamlining the request handling process. - Improved connection handling by setting write deadlines based on the PluginMaxExecutionTimeout. This change enhances the reliability of network operations within the serverless runtime environment. * refactor(plugin_manager): remove write deadline setting in serverless runtime connection initialization - Eliminated the write deadline setting from the connection initialization in the ServerlessPluginRuntime. - This change simplifies the connection handling process and aligns with the recent enhancements to timeout management. This update contributes to a more streamlined and efficient network operation within the serverless environment. * refactor(plugin_manager): adjust HTTP client timeout settings in serverless runtime - Modified the HTTP client configuration in the ServerlessPluginRuntime to set the TLS handshake timeout based on PluginMaxExecutionTimeout. - Retained the IdleConnTimeout setting to ensure consistent connection management. This update improves the timeout handling for secure connections, enhancing overall network reliability in the serverless environment.
103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package http_requests
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func buildHttpRequest(method string, url string, options ...HttpOptions) (*http.Request, error) {
|
|
req, err := http.NewRequest(method, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, option := range options {
|
|
switch option.Type {
|
|
case "header":
|
|
for k, v := range option.Value.(map[string]string) {
|
|
req.Header.Set(k, v)
|
|
}
|
|
case "params":
|
|
q := req.URL.Query()
|
|
for k, v := range option.Value.(map[string]string) {
|
|
q.Add(k, v)
|
|
}
|
|
req.URL.RawQuery = q.Encode()
|
|
case "payload":
|
|
q := req.URL.Query()
|
|
for k, v := range option.Value.(map[string]string) {
|
|
q.Add(k, v)
|
|
}
|
|
req.Body = io.NopCloser(strings.NewReader(q.Encode()))
|
|
case "payloadMultipart":
|
|
buffer := new(bytes.Buffer)
|
|
writer := multipart.NewWriter(buffer)
|
|
|
|
files := option.Value.(map[string]any)["files"].(map[string]HttpPayloadMultipartFile)
|
|
for filename, file := range files {
|
|
part, err := writer.CreateFormFile(filename, file.Filename)
|
|
if err != nil {
|
|
writer.Close()
|
|
return nil, err
|
|
}
|
|
_, err = io.Copy(part, file.Reader)
|
|
if err != nil {
|
|
writer.Close()
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
payload := option.Value.(map[string]any)["payload"].(map[string]string)
|
|
for k, v := range payload {
|
|
if err := writer.WriteField(k, v); err != nil {
|
|
writer.Close()
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if err := writer.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Body = io.NopCloser(buffer)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
case "payloadText":
|
|
req.Body = io.NopCloser(strings.NewReader(option.Value.(string)))
|
|
req.Header.Set("Content-Type", "text/plain")
|
|
case "payloadReader":
|
|
req.Body = option.Value.(io.ReadCloser)
|
|
case "payloadJson":
|
|
jsonStr, err := json.Marshal(option.Value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body = io.NopCloser(bytes.NewBuffer(jsonStr))
|
|
// set application/json content type
|
|
req.Header.Set("Content-Type", "application/json")
|
|
case "directReferer":
|
|
req.Header.Set("Referer", url)
|
|
}
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func Request(client *http.Client, url string, method string, options ...HttpOptions) (*http.Response, error) {
|
|
req, err := buildHttpRequest(method, url, options...)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return resp, nil
|
|
}
|