mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-17 12:38:07 +01:00
♻️ 内核中的 HTTP 客户端拆分项目 https://github.com/siyuan-note/siyuan/issues/5269
This commit is contained in:
parent
25ddad5957
commit
779e4fc4ae
13 changed files with 59 additions and 150 deletions
|
|
@ -17,11 +17,10 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
"github.com/siyuan-note/httpclient"
|
||||
)
|
||||
|
||||
var cachedRhyResult = map[string]interface{}{}
|
||||
|
|
@ -37,7 +36,7 @@ func GetRhyResult(force bool, proxyURL string) (map[string]interface{}, error) {
|
|||
return cachedRhyResult, nil
|
||||
}
|
||||
|
||||
request := NewCloudRequest(proxyURL)
|
||||
request := httpclient.NewCloudRequest(proxyURL)
|
||||
_, err := request.SetResult(&cachedRhyResult).Get(AliyunServer + "/apis/siyuan/version?ver=" + Ver)
|
||||
if nil != err {
|
||||
LogErrorf("get version meta info failed: %s", err)
|
||||
|
|
@ -46,108 +45,3 @@ func GetRhyResult(force bool, proxyURL string) (map[string]interface{}, error) {
|
|||
rhyResultCacheTime = now
|
||||
return cachedRhyResult, nil
|
||||
}
|
||||
|
||||
var (
|
||||
browserClient, browserDownloadClient, cloudAPIClient, cloudFileClientTimeout2Min, cloudFileClientTimeout15s *req.Client
|
||||
|
||||
browserUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
|
||||
)
|
||||
|
||||
func NewBrowserRequest(proxyURL string) (ret *req.Request) {
|
||||
if nil == browserClient {
|
||||
browserClient = req.C().
|
||||
SetUserAgent(browserUserAgent).
|
||||
SetTimeout(7 * time.Second).
|
||||
DisableInsecureSkipVerify()
|
||||
}
|
||||
if "" != proxyURL {
|
||||
browserClient.SetProxyURL(proxyURL)
|
||||
}
|
||||
ret = browserClient.R()
|
||||
ret.SetRetryCount(1).SetRetryFixedInterval(3 * time.Second)
|
||||
return
|
||||
}
|
||||
|
||||
func NewBrowserDownloadRequest(proxyURL string) *req.Request {
|
||||
if nil == browserDownloadClient {
|
||||
browserDownloadClient = req.C().
|
||||
SetUserAgent(browserUserAgent).
|
||||
SetTimeout(2 * time.Minute).
|
||||
SetCommonRetryCount(1).
|
||||
SetCommonRetryFixedInterval(3 * time.Second).
|
||||
SetCommonRetryCondition(retryCondition).
|
||||
DisableInsecureSkipVerify()
|
||||
}
|
||||
if "" != proxyURL {
|
||||
browserDownloadClient.SetProxyURL(proxyURL)
|
||||
}
|
||||
return browserDownloadClient.R()
|
||||
}
|
||||
|
||||
func NewCloudRequest(proxyURL string) *req.Request {
|
||||
if nil == cloudAPIClient {
|
||||
cloudAPIClient = req.C().
|
||||
SetUserAgent(UserAgent).
|
||||
SetTimeout(7 * time.Second).
|
||||
SetCommonRetryCount(1).
|
||||
SetCommonRetryFixedInterval(3 * time.Second).
|
||||
SetCommonRetryCondition(retryCondition).
|
||||
DisableInsecureSkipVerify()
|
||||
}
|
||||
if "" != proxyURL {
|
||||
cloudAPIClient.SetProxyURL(proxyURL)
|
||||
}
|
||||
return cloudAPIClient.R()
|
||||
}
|
||||
|
||||
func NewCloudFileRequest2m(proxyURL string) *req.Request {
|
||||
if nil == cloudFileClientTimeout2Min {
|
||||
cloudFileClientTimeout2Min = req.C().
|
||||
SetUserAgent(UserAgent).
|
||||
SetTimeout(2 * time.Minute).
|
||||
SetCommonRetryCount(1).
|
||||
SetCommonRetryFixedInterval(3 * time.Second).
|
||||
SetCommonRetryCondition(retryCondition).
|
||||
DisableInsecureSkipVerify()
|
||||
setTransport(cloudFileClientTimeout2Min.GetClient())
|
||||
}
|
||||
if "" != proxyURL {
|
||||
cloudFileClientTimeout2Min.SetProxyURL(proxyURL)
|
||||
}
|
||||
return cloudFileClientTimeout2Min.R()
|
||||
}
|
||||
|
||||
func NewCloudFileRequest15s(proxyURL string) *req.Request {
|
||||
if nil == cloudFileClientTimeout15s {
|
||||
cloudFileClientTimeout15s = req.C().
|
||||
SetUserAgent(UserAgent).
|
||||
SetTimeout(15 * time.Second).
|
||||
SetCommonRetryCount(1).
|
||||
SetCommonRetryFixedInterval(3 * time.Second).
|
||||
SetCommonRetryCondition(retryCondition).
|
||||
DisableInsecureSkipVerify()
|
||||
setTransport(cloudFileClientTimeout15s.GetClient())
|
||||
}
|
||||
if "" != proxyURL {
|
||||
cloudFileClientTimeout15s.SetProxyURL(proxyURL)
|
||||
}
|
||||
return cloudFileClientTimeout15s.R()
|
||||
}
|
||||
|
||||
func retryCondition(resp *req.Response, err error) bool {
|
||||
if nil != err {
|
||||
return true
|
||||
}
|
||||
if 503 == resp.StatusCode { // 负载均衡会返回 503,需要重试
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func setTransport(client *http.Client) {
|
||||
// 改进同步下载数据稳定性 https://github.com/siyuan-note/siyuan/issues/4994
|
||||
transport := client.Transport.(*req.Transport)
|
||||
transport.MaxIdleConns = 10
|
||||
transport.MaxIdleConnsPerHost = 2
|
||||
transport.MaxConnsPerHost = 2
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import (
|
|||
"github.com/88250/gulu"
|
||||
figure "github.com/common-nighthawk/go-figure"
|
||||
goPS "github.com/mitchellh/go-ps"
|
||||
"github.com/siyuan-note/httpclient"
|
||||
)
|
||||
|
||||
//var Mode = "dev"
|
||||
|
|
@ -53,6 +54,7 @@ func Boot() {
|
|||
IncBootProgress(3, "Booting...")
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
initMime()
|
||||
httpclient.SetUserAgent(UserAgent)
|
||||
|
||||
workspacePath := flag.String("workspace", "", "dir path of the workspace, default to ~/Documents/SiYuan/")
|
||||
wdPath := flag.String("wd", WorkingDir, "working directory of SiYuan")
|
||||
|
|
@ -323,7 +325,7 @@ func checkPort() {
|
|||
|
||||
LogInfof("port [%s] is opened, try to check version of running kernel", ServerPort)
|
||||
result := NewResult()
|
||||
_, err := NewBrowserRequest("").
|
||||
_, err := httpclient.NewBrowserRequest("").
|
||||
SetResult(result).
|
||||
SetHeader("User-Agent", UserAgent).
|
||||
Get("http://127.0.0.1:" + ServerPort + "/api/system/version")
|
||||
|
|
|
|||
|
|
@ -23,12 +23,14 @@ import (
|
|||
"time"
|
||||
|
||||
figure "github.com/common-nighthawk/go-figure"
|
||||
"github.com/siyuan-note/httpclient"
|
||||
)
|
||||
|
||||
func BootMobile(container, appDir, workspaceDir, nativeLibDir, privateDataDir, lang string) {
|
||||
IncBootProgress(3, "Booting...")
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
initMime()
|
||||
httpclient.SetUserAgent(UserAgent)
|
||||
|
||||
HomeDir = filepath.Join(workspaceDir, "home")
|
||||
WorkingDir = filepath.Join(appDir, "app")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue