perf(proxy): reduce nest rewrite allocations

- Dispatch shell link rewriting between streaming and buffered paths based on response size

- Reuse buffers and reduce URL construction allocations in proxy handlers

- Add nest benchmarks and align extractParts compatibility expectations with the current contract
This commit is contained in:
wjqserver 2026-04-12 00:02:54 +08:00
parent 4c555ed50c
commit e2719aa761
6 changed files with 248 additions and 88 deletions

View file

@ -9,6 +9,28 @@ import (
"github.com/infinite-iroha/touka"
)
func buildProxyPath(path, matcher string) string {
var sb strings.Builder
sb.Grow(len(path) + 50)
if matcher == "blob" && strings.HasPrefix(path, "github.com") {
sb.WriteString("https://raw.githubusercontent.com")
pathSegment := path[len("github.com"):]
if i := strings.Index(pathSegment, "/blob/"); i != -1 {
sb.WriteString(pathSegment[:i])
sb.WriteByte('/')
sb.WriteString(pathSegment[i+len("/blob/"):])
} else {
sb.WriteString(pathSegment)
}
return sb.String()
}
sb.WriteString("https://")
sb.WriteString(path)
return sb.String()
}
var re = regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
@ -31,22 +53,16 @@ func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
return
}
// 制作url
rawPath = "https://" + matches[2]
var (
user string
repo string
matcher string
)
path := matches[2]
var matcherErr *GHProxyErrors
user, repo, matcher, matcherErr = Matcher(rawPath, cfg)
user, repo, matcher, matcherErr := Matcher("https://"+path, cfg)
if matcherErr != nil {
ErrorPage(c, matcherErr)
return
}
rawPath = buildProxyPath(path, matcher)
shoudBreak = listCheck(cfg, c, user, repo, rawPath)
if shoudBreak {
return
@ -59,9 +75,6 @@ func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
// 处理blob/raw路径
if matcher == "blob" {
rawPath = rawPath[18:]
rawPath = "https://raw.githubusercontent.com" + rawPath
rawPath = strings.Replace(rawPath, "/blob/", "/", 1)
matcher = "raw"
}