Improve marketplace list loading (#16965)

- 直接使用 stage 索引中的 package 数据,不再为每个包单独请求 JSON
- 统一索引获取逻辑,使用 singleflight 合并并发请求
- 优化在线状态检查耗时,改用 https://oss.b3logfile.com/204
- 改进 disallowInstallBazaarPackage 函数性能

Co-authored-by: D <845765@qq.com>
This commit is contained in:
Jeffrey Chen 2026-02-03 10:07:16 +08:00 committed by GitHub
parent 9642f486bd
commit 4eec7b5944
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 385 additions and 473 deletions

View file

@ -21,11 +21,8 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"github.com/88250/go-humanize"
ants "github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -36,102 +33,70 @@ type Theme struct {
Modes []string `json:"modes"`
}
// Themes 返回集市主题列表
func Themes() (ret []*Theme) {
ret = []*Theme{}
result := getStageAndBazaar("themes")
isOnline := isBazzarOnline()
if !isOnline {
if !result.Online {
return
}
if result.StageErr != nil {
return
}
if 1 > len(result.BazaarIndex) {
return
}
stageIndex, err := getStageIndex("themes")
if err != nil {
return
for _, repo := range result.StageIndex.Repos {
if nil == repo.Package {
continue
}
theme := buildThemeFromStageRepo(repo, result.BazaarIndex)
if nil != theme {
ret = append(ret, theme)
}
}
bazaarIndex := getBazaarIndex()
if 1 > len(bazaarIndex) {
return
}
requestFailed := false
waitGroup := &sync.WaitGroup{}
lock := &sync.Mutex{}
p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
defer waitGroup.Done()
repo := arg.(*StageRepo)
repoURL := repo.URL
if pkg, found := packageCache.Get(repoURL); found {
lock.Lock()
ret = append(ret, pkg.(*Theme))
lock.Unlock()
return
}
if requestFailed {
return
}
theme := &Theme{}
innerU := util.BazaarOSSServer + "/package/" + repoURL + "/theme.json"
innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(theme).Get(innerU)
if nil != innerErr {
logging.LogErrorf("get bazaar package [%s] failed: %s", innerU, innerErr)
requestFailed = true
return
}
if 200 != innerResp.StatusCode {
logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
requestFailed = true
return
}
theme.DisallowInstall = disallowInstallBazaarPackage(theme.Package)
theme.DisallowUpdate = disallowInstallBazaarPackage(theme.Package)
theme.UpdateRequiredMinAppVer = theme.MinAppVersion
theme.URL = strings.TrimSuffix(theme.URL, "/")
repoURLHash := strings.Split(repoURL, "@")
theme.RepoURL = "https://github.com/" + repoURLHash[0]
theme.RepoHash = repoURLHash[1]
theme.PreviewURL = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageslim"
theme.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repoURL + "/preview.png?imageView2/2/w/436/h/232"
theme.IconURL = util.BazaarOSSServer + "/package/" + repoURL + "/icon.png"
theme.Funding = repo.Package.Funding
theme.PreferredFunding = getPreferredFunding(theme.Funding)
theme.PreferredName = GetPreferredName(theme.Package)
theme.PreferredDesc = getPreferredDesc(theme.Description)
theme.Updated = repo.Updated
theme.Stars = repo.Stars
theme.OpenIssues = repo.OpenIssues
theme.Size = repo.Size
theme.HSize = humanize.BytesCustomCeil(uint64(theme.Size), 2)
theme.InstallSize = repo.InstallSize
theme.HInstallSize = humanize.BytesCustomCeil(uint64(theme.InstallSize), 2)
packageInstallSizeCache.SetDefault(theme.RepoURL, theme.InstallSize)
theme.HUpdated = formatUpdated(theme.Updated)
pkg := bazaarIndex[strings.Split(repoURL, "@")[0]]
if nil != pkg {
theme.Downloads = pkg.Downloads
}
lock.Lock()
ret = append(ret, theme)
lock.Unlock()
packageCache.SetDefault(repoURL, theme)
})
for _, repo := range stageIndex.Repos {
waitGroup.Add(1)
p.Invoke(repo)
}
waitGroup.Wait()
p.Release()
sort.Slice(ret, func(i, j int) bool { return ret[i].Updated > ret[j].Updated })
return
}
// buildThemeFromStageRepo 使用 stage 内嵌的 package 构建 *Theme不发起 HTTP 请求。
func buildThemeFromStageRepo(repo *StageRepo, bazaarIndex map[string]*bazaarPackage) *Theme {
pkg := *repo.Package
pkg.URL = strings.TrimSuffix(pkg.URL, "/")
repoURLHash := strings.Split(repo.URL, "@")
if 2 != len(repoURLHash) {
return nil
}
pkg.RepoURL = "https://github.com/" + repoURLHash[0]
pkg.RepoHash = repoURLHash[1]
pkg.PreviewURL = util.BazaarOSSServer + "/package/" + repo.URL + "/preview.png?imageslim"
pkg.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repo.URL + "/preview.png?imageView2/2/w/436/h/232"
pkg.IconURL = util.BazaarOSSServer + "/package/" + repo.URL + "/icon.png"
pkg.Updated = repo.Updated
pkg.Stars = repo.Stars
pkg.OpenIssues = repo.OpenIssues
pkg.Size = repo.Size
pkg.HSize = humanize.BytesCustomCeil(uint64(pkg.Size), 2)
pkg.InstallSize = repo.InstallSize
pkg.HInstallSize = humanize.BytesCustomCeil(uint64(pkg.InstallSize), 2)
pkg.HUpdated = formatUpdated(pkg.Updated)
pkg.PreferredFunding = getPreferredFunding(pkg.Funding)
pkg.PreferredName = GetPreferredName(&pkg)
pkg.PreferredDesc = getPreferredDesc(pkg.Description)
pkg.DisallowInstall = disallowInstallBazaarPackage(&pkg)
pkg.DisallowUpdate = disallowInstallBazaarPackage(&pkg)
pkg.UpdateRequiredMinAppVer = pkg.MinAppVersion
if bp := bazaarIndex[repoURLHash[0]]; nil != bp {
pkg.Downloads = bp.Downloads
}
packageInstallSizeCache.SetDefault(pkg.RepoURL, pkg.InstallSize)
theme := &Theme{Package: &pkg, Modes: []string{}}
return theme
}
func InstalledThemes() (ret []*Theme) {
ret = []*Theme{}