♻️ 内核中的 HTTP 客户端拆分项目 https://github.com/siyuan-note/siyuan/issues/5269

This commit is contained in:
Liang Ding 2022-06-23 01:22:28 +08:00
parent 25ddad5957
commit 779e4fc4ae
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
13 changed files with 59 additions and 150 deletions

View file

@ -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
}

View file

@ -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")

View file

@ -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")