Compare commits

..

2 commits

Author SHA1 Message Date
wjqserver
5731418822 3.5.1 2025-06-09 23:31:37 +08:00
wjqserver
8d5b764ec7 optimize matcher 2025-06-09 23:30:39 +08:00
4 changed files with 122 additions and 91 deletions

View file

@ -1,5 +1,9 @@
# 更新日志 # 更新日志
3.5.1 - 2025-06-09
---
- CHANGE: 大幅优化`Matcher`的性能, 实现零分配, 大幅提升性能; 单次操作时间: `254.3 ns/op` => `29.59 ns/op`
25w45a - 2025-06-09 25w45a - 2025-06-09
--- ---
- PRE-RELEASE: 此版本是v3.5.1预发布版本,请勿在生产环境中使用; - PRE-RELEASE: 此版本是v3.5.1预发布版本,请勿在生产环境中使用;

View file

@ -1 +1 @@
3.5.0 3.5.1

View file

@ -9,6 +9,25 @@ import (
"sync" "sync"
) )
var (
githubPrefix = "https://github.com/"
rawPrefix = "https://raw.githubusercontent.com/"
gistPrefix = "https://gist.github.com/"
apiPrefix = "https://api.github.com/"
githubPrefixLen int
rawPrefixLen int
gistPrefixLen int
apiPrefixLen int
)
func init() {
githubPrefixLen = len(githubPrefix)
rawPrefixLen = len(rawPrefix)
gistPrefixLen = len(gistPrefix)
apiPrefixLen = len(apiPrefix)
//log.Printf("githubPrefixLen: %d, rawPrefixLen: %d, gistPrefixLen: %d, apiPrefixLen: %d", githubPrefixLen, rawPrefixLen, gistPrefixLen, apiPrefixLen)
}
// Matcher 从原始URL路径中高效地解析并匹配代理规则. // Matcher 从原始URL路径中高效地解析并匹配代理规则.
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) { func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
if len(rawPath) < 18 { if len(rawPath) < 18 {
@ -16,8 +35,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://github.com/" // 匹配 "https://github.com/"
if strings.HasPrefix(rawPath, "https://github.com/") { if strings.HasPrefix(rawPath, githubPrefix) {
remaining := rawPath[19:] remaining := rawPath[githubPrefixLen:]
i := strings.IndexByte(remaining, '/') i := strings.IndexByte(remaining, '/')
if i <= 0 { if i <= 0 {
return "", "", "", NewErrorWithStatusLookup(400, "malformed github path: missing user") return "", "", "", NewErrorWithStatusLookup(400, "malformed github path: missing user")
@ -55,8 +74,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://raw.githubusercontent.com/" // 匹配 "https://raw.githubusercontent.com/"
if strings.HasPrefix(rawPath, "https://raw.githubusercontent.com/") { if strings.HasPrefix(rawPath, rawPrefix) {
remaining := rawPath[34:] remaining := rawPath[rawPrefixLen:]
// 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file... // 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file...
// 我们只需要 user 和 repo // 我们只需要 user 和 repo
i := strings.IndexByte(remaining, '/') i := strings.IndexByte(remaining, '/')
@ -79,8 +98,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://gist.github.com/" // 匹配 "https://gist.github.com/"
if strings.HasPrefix(rawPath, "https://gist.github.com/") { if strings.HasPrefix(rawPath, gistPrefix) {
remaining := rawPath[24:] remaining := rawPath[gistPrefixLen:]
i := strings.IndexByte(remaining, '/') i := strings.IndexByte(remaining, '/')
if i <= 0 { if i <= 0 {
// case: https://gist.github.com/user // case: https://gist.github.com/user
@ -96,11 +115,11 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://api.github.com/" // 匹配 "https://api.github.com/"
if strings.HasPrefix(rawPath, "https://api.github.com/") { if strings.HasPrefix(rawPath, apiPrefix) {
if !cfg.Auth.ForceAllowApi && (cfg.Auth.Method != "header" || !cfg.Auth.Enabled) { if !cfg.Auth.ForceAllowApi && (cfg.Auth.Method != "header" || !cfg.Auth.Enabled) {
return "", "", "", NewErrorWithStatusLookup(403, "API proxy requires header authentication") return "", "", "", NewErrorWithStatusLookup(403, "API proxy requires header authentication")
} }
remaining := rawPath[23:] remaining := rawPath[apiPrefixLen:]
var user, repo string var user, repo string
if strings.HasPrefix(remaining, "repos/") { if strings.HasPrefix(remaining, "repos/") {
parts := strings.SplitN(remaining[6:], "/", 3) parts := strings.SplitN(remaining[6:], "/", 3)
@ -120,8 +139,9 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
return "", "", "", NewErrorWithStatusLookup(404, "no matcher found for the given path") return "", "", "", NewErrorWithStatusLookup(404, "no matcher found for the given path")
} }
// 原实现
/* /*
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) { func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
var ( var (
user string user string
repo string repo string
@ -215,8 +235,9 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
//return "", "", "", ErrNotFound //return "", "", "", ErrNotFound
errMsg := "Didn't match any matcher" errMsg := "Didn't match any matcher"
return "", "", "", NewErrorWithStatusLookup(404, errMsg) return "", "", "", NewErrorWithStatusLookup(404, errMsg)
} }
*/ */
var ( var (
proxyableMatchersMap map[string]struct{} proxyableMatchersMap map[string]struct{}
initMatchersOnce sync.Once initMatchersOnce sync.Once

View file

@ -68,6 +68,12 @@ func TestMatcher_Compatibility(t *testing.T) {
config: cfgWithAuth, config: cfgWithAuth,
expectedUser: "owner", expectedRepo: "repo", expectedMatcher: "clone", expectedUser: "owner", expectedRepo: "repo", expectedMatcher: "clone",
}, },
{
name: "Girhub Broken Path",
rawPath: "https://github.com/owner",
config: cfgWithAuth,
expectError: true, expectedErrCode: 400,
},
{ {
name: "RawGHUserContent Path", name: "RawGHUserContent Path",