You've already forked VptPassiveAdapter
优化http连接池
This commit is contained in:
40
api/http_client.go
Normal file
40
api/http_client.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
// 通用HTTP客户端,带短超时时间,适用于API调用
|
||||
apiClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 10,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
DisableKeepAlives: false,
|
||||
},
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
// 文件上传专用HTTP客户端,超时时间较长
|
||||
uploadClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: 50,
|
||||
MaxIdleConnsPerHost: 5,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
DisableKeepAlives: false,
|
||||
},
|
||||
Timeout: 60 * time.Second, // 文件上传需要更长的超时时间
|
||||
}
|
||||
)
|
||||
|
||||
// GetAPIClient 获取API调用客户端
|
||||
func GetAPIClient() *http.Client {
|
||||
return apiClient
|
||||
}
|
||||
|
||||
// GetUploadClient 获取文件上传客户端
|
||||
func GetUploadClient() *http.Client {
|
||||
return uploadClient
|
||||
}
|
@@ -58,8 +58,7 @@ func OssUpload(ctx context.Context, url, filePath string) error {
|
||||
}
|
||||
req.Header.Set("Content-Type", "video/mp4")
|
||||
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(fileBytes)))
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := GetUploadClient().Do(req)
|
||||
if err != nil {
|
||||
span.SetAttributes(attribute.String("error", err.Error()))
|
||||
span.SetStatus(codes.Error, "发送请求失败")
|
||||
|
@@ -9,7 +9,6 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func SyncTask() ([]dto.Task, error) {
|
||||
@@ -29,10 +28,7 @@ func SyncTask() ([]dto.Task, error) {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second, // 设置超时时间为5秒
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := GetAPIClient().Do(req)
|
||||
if err != nil {
|
||||
log.Println("Error sending request:", err)
|
||||
return nil, err
|
||||
|
@@ -26,8 +26,7 @@ func QueryUploadUrlForTask(ctx context.Context, taskId string) (string, error) {
|
||||
log.Println("Error creating request:", err)
|
||||
return "", err
|
||||
}
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := GetAPIClient().Do(req)
|
||||
if err != nil {
|
||||
span.SetAttributes(attribute.String("error", err.Error()))
|
||||
span.SetStatus(codes.Error, "发送请求失败")
|
||||
@@ -62,8 +61,7 @@ func ReportTaskFailure(ctx context.Context, taskId string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := GetAPIClient().Do(req)
|
||||
if err != nil {
|
||||
span.SetAttributes(attribute.String("error", err.Error()))
|
||||
span.SetStatus(codes.Error, "发送请求失败")
|
||||
@@ -107,8 +105,7 @@ func ReportTaskSuccess(ctx context.Context, taskId string, file *dto.FileObject)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
resp, err := GetAPIClient().Do(req)
|
||||
if err != nil {
|
||||
span.SetAttributes(attribute.String("error", err.Error()))
|
||||
span.SetStatus(codes.Error, "发送请求失败")
|
||||
|
Reference in New Issue
Block a user