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
|
||||
}
|
Reference in New Issue
Block a user