Compare commits

..

No commits in common. "5731418822fd37e3e6c81da6992797a2868b790b" and "5dde21a403320031b43cd4b38189542e66ac11f0" have entirely different histories.

4 changed files with 98 additions and 129 deletions

View file

@ -1,9 +1,5 @@
# 更新日志 # 更新日志
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.1 3.5.0

View file

@ -9,25 +9,6 @@ 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 {
@ -35,8 +16,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://github.com/" // 匹配 "https://github.com/"
if strings.HasPrefix(rawPath, githubPrefix) { if strings.HasPrefix(rawPath, "https://github.com/") {
remaining := rawPath[githubPrefixLen:] remaining := rawPath[19:]
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")
@ -74,8 +55,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://raw.githubusercontent.com/" // 匹配 "https://raw.githubusercontent.com/"
if strings.HasPrefix(rawPath, rawPrefix) { if strings.HasPrefix(rawPath, "https://raw.githubusercontent.com/") {
remaining := rawPath[rawPrefixLen:] remaining := rawPath[34:]
// 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file... // 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file...
// 我们只需要 user 和 repo // 我们只需要 user 和 repo
i := strings.IndexByte(remaining, '/') i := strings.IndexByte(remaining, '/')
@ -98,8 +79,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://gist.github.com/" // 匹配 "https://gist.github.com/"
if strings.HasPrefix(rawPath, gistPrefix) { if strings.HasPrefix(rawPath, "https://gist.github.com/") {
remaining := rawPath[gistPrefixLen:] remaining := rawPath[24:]
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
@ -115,11 +96,11 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
} }
// 匹配 "https://api.github.com/" // 匹配 "https://api.github.com/"
if strings.HasPrefix(rawPath, apiPrefix) { if strings.HasPrefix(rawPath, "https://api.github.com/") {
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[apiPrefixLen:] remaining := rawPath[23:]
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)
@ -139,7 +120,6 @@ 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 (
@ -237,7 +217,6 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
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,12 +68,6 @@ 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",