♻️ 内核中的 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

@ -25,6 +25,7 @@ import (
"github.com/dustin/go-humanize"
ants "github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -63,7 +64,7 @@ func Icons(proxyURL string) (icons []*Icon) {
bazaarIndex := getBazaarIndex(proxyURL)
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := util.NewBrowserRequest(proxyURL)
request := httpclient.NewBrowserRequest(proxyURL)
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/icons.json"
resp, err := request.SetResult(&result).Get(u)
if nil != err {
@ -85,7 +86,7 @@ func Icons(proxyURL string) (icons []*Icon) {
icon := &Icon{}
innerU := util.BazaarOSSServer + "/package/" + repoURL + "/icon.json"
innerResp, innerErr := util.NewBrowserRequest(proxyURL).SetResult(icon).Get(innerU)
innerResp, innerErr := httpclient.NewBrowserRequest(proxyURL).SetResult(icon).Get(innerU)
if nil != innerErr {
util.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
return

View file

@ -30,6 +30,7 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/araddon/dateparse"
"github.com/imroc/req/v3"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/util"
textUnicode "golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
@ -82,14 +83,14 @@ func downloadPackage(repoURLHash, proxyURL string, chinaCDN, pushProgress bool,
u = util.BazaarOSSServer + "/package/" + repoURLHash
}
buf := &bytes.Buffer{}
resp, err := util.NewBrowserDownloadRequest(proxyURL).SetOutput(buf).SetDownloadCallback(func(info req.DownloadInfo) {
resp, err := httpclient.NewBrowserDownloadRequest(proxyURL).SetOutput(buf).SetDownloadCallback(func(info req.DownloadInfo) {
if pushProgress {
util.PushDownloadProgress(pushID, float32(info.DownloadedSize)/float32(info.Response.ContentLength))
}
}).Get(u)
if nil != err {
u = util.BazaarOSSServer + "/package/" + repoURLHash
resp, err = util.NewBrowserDownloadRequest(proxyURL).SetOutput(buf).SetDownloadCallback(func(info req.DownloadInfo) {
resp, err = httpclient.NewBrowserDownloadRequest(proxyURL).SetOutput(buf).SetDownloadCallback(func(info req.DownloadInfo) {
if pushProgress {
util.PushDownloadProgress(pushID, float32(info.DownloadedSize)/float32(info.Response.ContentLength))
}
@ -116,7 +117,7 @@ func incPackageDownloads(repoURLHash, proxyURL, systemID string) {
repo := strings.Split(repoURLHash, "@")[0]
u := util.AliyunServer + "/apis/siyuan/bazaar/addBazaarPackageDownloadCount"
util.NewCloudRequest(proxyURL).SetBody(
httpclient.NewCloudRequest(proxyURL).SetBody(
map[string]interface{}{
"systemID": systemID,
"repo": repo,
@ -190,7 +191,7 @@ func getBazaarIndex(proxyURL string) map[string]*bazaarPackage {
return cachedBazaarIndex
}
request := util.NewBrowserRequest(proxyURL)
request := httpclient.NewBrowserRequest(proxyURL)
u := util.BazaarStatServer + "/bazaar/index.json"
resp, reqErr := request.SetResult(&cachedBazaarIndex).Get(u)
if nil != reqErr {

View file

@ -26,6 +26,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -63,7 +64,7 @@ func Templates(proxyURL string) (templates []*Template) {
bazaarIndex := getBazaarIndex(proxyURL)
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := util.NewBrowserRequest(proxyURL)
request := httpclient.NewBrowserRequest(proxyURL)
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/templates.json"
resp, reqErr := request.SetResult(&result).Get(u)
if nil != reqErr {
@ -86,7 +87,7 @@ func Templates(proxyURL string) (templates []*Template) {
template := &Template{}
innerU := util.BazaarOSSServer + "/package/" + repoURL + "/template.json"
innerResp, innerErr := util.NewBrowserRequest(proxyURL).SetResult(template).Get(innerU)
innerResp, innerErr := httpclient.NewBrowserRequest(proxyURL).SetResult(template).Get(innerU)
if nil != innerErr {
util.LogErrorf("get community template [%s] failed: %s", repoURL, innerErr)
return

View file

@ -25,6 +25,7 @@ import (
"github.com/dustin/go-humanize"
ants "github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -64,7 +65,7 @@ func Themes(proxyURL string) (ret []*Theme) {
bazaarIndex := getBazaarIndex(proxyURL)
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := util.NewBrowserRequest(proxyURL)
request := httpclient.NewBrowserRequest(proxyURL)
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/themes.json"
resp, reqErr := request.SetResult(&result).Get(u)
if nil != reqErr {
@ -87,7 +88,7 @@ func Themes(proxyURL string) (ret []*Theme) {
theme := &Theme{}
innerU := util.BazaarOSSServer + "/package/" + repoURL + "/theme.json"
innerResp, innerErr := util.NewBrowserRequest(proxyURL).SetResult(theme).Get(innerU)
innerResp, innerErr := httpclient.NewBrowserRequest(proxyURL).SetResult(theme).Get(innerU)
if nil != innerErr {
util.LogErrorf("get bazaar package [%s] failed: %s", innerU, innerErr)
return

View file

@ -25,6 +25,7 @@ import (
"github.com/dustin/go-humanize"
ants "github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -63,7 +64,7 @@ func Widgets(proxyURL string) (widgets []*Widget) {
bazaarIndex := getBazaarIndex(proxyURL)
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := util.NewBrowserRequest(proxyURL)
request := httpclient.NewBrowserRequest(proxyURL)
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/widgets.json"
resp, err := request.SetResult(&result).Get(u)
if nil != err {
@ -86,7 +87,7 @@ func Widgets(proxyURL string) (widgets []*Widget) {
widget := &Widget{}
innerU := util.BazaarOSSServer + "/package/" + repoURL + "/widget.json"
innerResp, innerErr := util.NewBrowserRequest(proxyURL).SetResult(widget).Get(innerU)
innerResp, innerErr := httpclient.NewBrowserRequest(proxyURL).SetResult(widget).Get(innerU)
if nil != innerErr {
util.LogErrorf("get bazaar package [%s] failed: %s", repoURL, innerErr)
return

View file

@ -43,6 +43,7 @@ require (
github.com/siyuan-note/dejavu v0.0.0-20220620134708-bcbbf7f6478e
github.com/siyuan-note/encryption v0.0.0-20220612074546-f1dd94fe8676
github.com/siyuan-note/filelock v0.0.0-20220616063212-74cfba0754ee
github.com/siyuan-note/httpclient v0.0.0-20220622170705-004622a8138d
github.com/vmihailenco/msgpack/v5 v5.3.5
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673
golang.org/x/image v0.0.0-20220601225756-64ec528b34cd
@ -98,7 +99,7 @@ require (
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
golang.org/x/net v0.0.0-20220621193019-9d032be2e588 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/tools v0.1.8 // indirect

View file

@ -425,6 +425,8 @@ github.com/siyuan-note/encryption v0.0.0-20220612074546-f1dd94fe8676 h1:QB9TjJQF
github.com/siyuan-note/encryption v0.0.0-20220612074546-f1dd94fe8676/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw=
github.com/siyuan-note/filelock v0.0.0-20220616063212-74cfba0754ee h1:8nBBJKHbXuMF2dWKUKsrnnFblgeNBfPjLgVm0aDas/s=
github.com/siyuan-note/filelock v0.0.0-20220616063212-74cfba0754ee/go.mod h1:c4vwvWRrnfa75OXiO21K/76BFRJ4cFITKNoVs5lFdwc=
github.com/siyuan-note/httpclient v0.0.0-20220622170705-004622a8138d h1:zMkahXrTDPhBUg9W7HkGlYP6WY5AkQoJqj7Cmk++qoU=
github.com/siyuan-note/httpclient v0.0.0-20220622170705-004622a8138d/go.mod h1:W/5hysNTpI8a52hSMzNHvf29tbM+VmD/l+c2wvkqlL8=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
@ -577,8 +579,8 @@ golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220111093109-d55c255bac03/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d h1:4SFsTMi4UahlKoloni7L4eYzhFRifURQLw+yv0QDCx8=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220621193019-9d032be2e588 h1:9ubFuySsnAJYGyJrZ3koiEv8FyqofCBdz3G9Mbf2YFc=
golang.org/x/net v0.0.0-20220621193019-9d032be2e588/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

View file

@ -36,6 +36,7 @@ import (
"github.com/88250/lute/parse"
"github.com/gabriel-vasile/mimetype"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
@ -91,7 +92,7 @@ func NetImg2LocalAssets(rootID string) (err error) {
}
}
util.PushUpdateMsg(msgId, fmt.Sprintf(Conf.Language(119), u), 15000)
request := util.NewBrowserRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewBrowserRequest(Conf.System.NetworkProxy.String())
resp, reqErr := request.Get(u)
if nil != reqErr {
util.LogErrorf("download net img [%s] failed: %s", u, reqErr)
@ -285,7 +286,7 @@ func uploadCloud(sqlAssets []*sql.Asset) (err error) {
}
requestResult := gulu.Ret.NewResult()
request := util.NewCloudFileRequest2m(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudFileRequest2m(Conf.System.NetworkProxy.String())
resp, reqErr := request.
SetResult(requestResult).
SetFile("file[]", absAsset).

View file

@ -25,6 +25,7 @@ import (
"time"
"github.com/88250/gulu"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -37,7 +38,7 @@ func StartFreeTrial() (err error) {
}
requestResult := gulu.Ret.NewResult()
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
_, err = request.
SetResult(requestResult).
SetCookies(&http.Cookie{Name: "symphony", Value: Conf.User.UserToken}).
@ -54,7 +55,7 @@ func StartFreeTrial() (err error) {
func DeactivateUser() (err error) {
requestResult := gulu.Ret.NewResult()
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(requestResult).
SetCookies(&http.Cookie{Name: "symphony", Value: Conf.User.UserToken}).
@ -79,7 +80,7 @@ func DeactivateUser() (err error) {
func SetCloudBlockReminder(id, data string, timed int64) (err error) {
requestResult := gulu.Ret.NewResult()
payload := map[string]interface{}{"dataId": id, "data": data, "timed": timed}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(requestResult).
SetBody(payload).
@ -112,7 +113,7 @@ func LoadUploadToken() (err error) {
}
requestResult := gulu.Ret.NewResult()
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(requestResult).
SetCookies(&http.Cookie{Name: "symphony", Value: Conf.User.UserToken}).
@ -251,7 +252,7 @@ func loadUserFromConf() *conf.User {
func RemoveCloudShorthands(ids []string) (err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
body := map[string]interface{}{
"ids": ids,
}
@ -282,7 +283,7 @@ func RemoveCloudShorthands(ids []string) (err error) {
func GetCloudShorthands(page int) (result map[string]interface{}, err error) {
result = map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetCookies(&http.Cookie{Name: "symphony", Value: Conf.User.UserToken}).
@ -319,7 +320,7 @@ var errInvalidUser = errors.New("invalid user")
func getUser(token string) (*conf.User, error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
_, err := request.
SetResult(&result).
SetBody(map[string]string{"token": token}).
@ -350,7 +351,7 @@ func getUser(token string) (*conf.User, error) {
func UseActivationcode(code string) (err error) {
requestResult := gulu.Ret.NewResult()
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
_, err = request.
SetResult(requestResult).
SetBody(map[string]string{"data": code}).
@ -369,7 +370,7 @@ func UseActivationcode(code string) (err error) {
func CheckActivationcode(code string) (retCode int, msg string) {
retCode = 1
requestResult := gulu.Ret.NewResult()
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
_, err := request.
SetResult(requestResult).
SetBody(map[string]string{"data": code}).
@ -389,7 +390,7 @@ func CheckActivationcode(code string) (retCode int, msg string) {
func Login(userName, password, captcha string) (ret *gulu.Result, err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
_, err = request.
SetResult(&result).
SetBody(map[string]string{"userName": userName, "userPassword": password, "captcha": captcha}).
@ -415,7 +416,7 @@ func Login(userName, password, captcha string) (ret *gulu.Result, err error) {
func Login2fa(token, code string) (map[string]interface{}, error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
_, err := request.
SetResult(&result).
SetBody(map[string]string{"twofactorAuthCode": code}).

View file

@ -34,12 +34,13 @@ import (
"github.com/88250/gulu"
"github.com/panjf2000/ants/v2"
"github.com/qiniu/go-sdk/v7/storage"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/util"
)
func getCloudSpaceOSS() (sync, backup map[string]interface{}, assetSize int64, err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetBody(map[string]string{"token": Conf.User.UserToken}).
@ -69,7 +70,7 @@ func getCloudSpaceOSS() (sync, backup map[string]interface{}, assetSize int64, e
func removeCloudDirPath(dirPath string) (err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetBody(map[string]string{"dirPath": dirPath, "token": Conf.User.UserToken}).
@ -94,7 +95,7 @@ func removeCloudDirPath(dirPath string) (err error) {
func createCloudSyncDirOSS(name string) (err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetBody(map[string]string{"name": name, "token": Conf.User.UserToken}).
@ -125,7 +126,7 @@ func createCloudSyncDirOSS(name string) (err error) {
func listCloudSyncDirOSS() (dirs []map[string]interface{}, size int64, err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetBody(map[string]interface{}{"token": Conf.User.UserToken}).
SetResult(&result).
@ -277,7 +278,7 @@ func ossDownload0(localDirPath, cloudDirPath, fetch string, fetchedFiles *int, t
localFilePath := filepath.Join(localDirPath, fetch)
remoteFileURL := path.Join(cloudDirPath, fetch)
var result map[string]interface{}
resp, err := util.NewCloudRequest(Conf.System.NetworkProxy.String()).
resp, err := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String()).
SetResult(&result).
SetBody(map[string]interface{}{"token": Conf.User.UserToken, "path": remoteFileURL}).
Post(util.AliyunServer + "/apis/siyuan/data/getSiYuanFile?uid=" + Conf.User.UserId)
@ -312,9 +313,9 @@ func ossDownload0(localDirPath, cloudDirPath, fetch string, fetchedFiles *int, t
os.Remove(localFilePath)
if bootORExit {
resp, err = util.NewCloudFileRequest15s(Conf.System.NetworkProxy.String()).Get(downloadURL)
resp, err = httpclient.NewCloudFileRequest15s(Conf.System.NetworkProxy.String()).Get(downloadURL)
} else {
resp, err = util.NewCloudFileRequest2m(Conf.System.NetworkProxy.String()).Get(downloadURL)
resp, err = httpclient.NewCloudFileRequest2m(Conf.System.NetworkProxy.String()).Get(downloadURL)
}
if nil != err {
util.LogErrorf("download request [%s] failed: %s", downloadURL, err)
@ -457,7 +458,7 @@ func ossRemove0(cloudDirPath string, removes []string) (err error) {
return
}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetBody(map[string]interface{}{"token": Conf.User.UserToken, "dirPath": cloudDirPath, "paths": removes}).
Post(util.AliyunServer + "/apis/siyuan/data/removeSiYuanFile?uid=" + Conf.User.UserId)
@ -511,7 +512,7 @@ func getOssUploadToken(filename, cloudDirPath string, length int64) (ret string,
// 因为需要指定 key所以每次上传文件都必须在云端生成 Token否则有安全隐患
var result map[string]interface{}
req := util.NewCloudRequest(Conf.System.NetworkProxy.String()).
req := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String()).
SetResult(&result)
req.SetBody(map[string]interface{}{
"token": Conf.User.UserToken,
@ -551,7 +552,7 @@ func getOssUploadToken(filename, cloudDirPath string, length int64) (ret string,
func getCloudSyncVer(cloudDir string) (cloudSyncVer int64, err error) {
start := time.Now()
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetBody(map[string]string{"syncDir": cloudDir, "token": Conf.User.UserToken}).
@ -591,7 +592,7 @@ func getCloudSyncVer(cloudDir string) (cloudSyncVer int64, err error) {
func getCloudSync(cloudDir string) (assetSize, backupSize int64, device string, err error) {
start := time.Now()
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetBody(map[string]string{"syncDir": cloudDir, "token": Conf.User.UserToken}).
@ -656,7 +657,7 @@ func getLocalFileListOSS(isBackup bool) (ret map[string]*CloudIndex, err error)
func getCloudFileListOSS(cloudDirPath string) (ret map[string]*CloudIndex, err error) {
result := map[string]interface{}{}
request := util.NewCloudRequest(Conf.System.NetworkProxy.String())
request := httpclient.NewCloudRequest(Conf.System.NetworkProxy.String())
resp, err := request.
SetResult(&result).
SetBody(map[string]string{"dirPath": cloudDirPath, "token": Conf.User.UserToken}).
@ -681,7 +682,7 @@ func getCloudFileListOSS(cloudDirPath string) (ret map[string]*CloudIndex, err e
retData := result["data"].(map[string]interface{})
downloadURL := retData["url"].(string)
resp, err = util.NewCloudFileRequest15s(Conf.System.NetworkProxy.String()).Get(downloadURL)
resp, err = httpclient.NewCloudFileRequest15s(Conf.System.NetworkProxy.String()).Get(downloadURL)
if nil != err {
util.LogErrorf("download request [%s] failed: %s", downloadURL, err)
return

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