perf(proxy): optimize hot request paths

- Cache route handlers, simplify NoRoute path normalization, and reduce matcher/header allocations

- Honor configured transport pool limits in auto mode and add hotpath regression benchmarks/tests
This commit is contained in:
wjqserver 2026-04-12 03:22:05 +08:00
parent e2719aa761
commit ba3dcf7624
9 changed files with 437 additions and 51 deletions

View file

@ -3,7 +3,6 @@ package proxy
import (
"fmt"
"ghproxy/config"
"regexp"
"strings"
"github.com/infinite-iroha/touka"
@ -31,29 +30,34 @@ func buildProxyPath(path, matcher string) string {
return sb.String()
}
var re = regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
func normalizeProxyPath(rawPath string) (string, bool) {
path := strings.TrimLeft(rawPath, "/")
switch {
case strings.HasPrefix(path, "https:"):
path = path[len("https:"):]
case strings.HasPrefix(path, "http:"):
path = path[len("http:"):]
}
path = strings.TrimLeft(path, "/")
return path, path != ""
}
func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
return func(c *touka.Context) {
var ctx = c.Request.Context()
var shoudBreak bool
var (
rawPath string
matches []string
)
rawPath = strings.TrimPrefix(c.GetRequestURI(), "/") // 去掉前缀/
matches = re.FindStringSubmatch(rawPath) // 匹配路径
path, ok := normalizeProxyPath(c.GetRequestURI())
// 匹配路径错误处理
if len(matches) < 3 {
if !ok {
c.Warnf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.UserAgent(), c.Request.Proto)
ErrorPage(c, NewErrorWithStatusLookup(400, fmt.Sprintf("Invalid URL Format: %s", c.GetRequestURI())))
return
}
path := matches[2]
var matcherErr *GHProxyErrors
user, repo, matcher, matcherErr := Matcher("https://"+path, cfg)
if matcherErr != nil {
@ -61,7 +65,7 @@ func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
return
}
rawPath = buildProxyPath(path, matcher)
rawPath := buildProxyPath(path, matcher)
shoudBreak = listCheck(cfg, c, user, repo, rawPath)
if shoudBreak {