Files
forge/routers/common/qos.go
zeekayandHanzo Dev ee51373566 web: own the route context; chi is only the mux now
Path parameters were read through chi from all over the codebase. They are read
through modules/web.RouteContext now, and chi is confined to five files.

The realisation that made this small: chi was never doing the hard part.
router_path.go has always compiled our own patterns to regexps and captured
groups itself — the 48 regex-constrained routes ({sha:([a-f0-9]{7,64})},
{ball_type:tarball|zipball|bundle}) are ours, not chi features. chi was holding
three fields on the way through: routed method, captured params, matched
patterns. Depending on a router for those made the router look load-bearing.

  modules/web/route_context.go   the type: method, params, patterns
  router_path.go                 matchPath fills it directly; chi-free
  base_path.go                   PathParam/SetPathParam read and write it —
                                 the choke point every handler goes through
  qos, blockexpensive, tracing   read RoutePattern from it
  contexttest                    builds one, so a mock request looks routed

RoutePattern collapses the "/*" each Mount contributes, which is what chi did
and what makes the value the pattern a person would have written — logs and
metrics group by it.

What is still chi, and honestly: the mux in modules/web/router.go, the route
tables that feed it, and one BeforeRouting middleware that sets the path the mux
routes ON. Replacing it means a router for 1333 routes including those 48 regex
patterns — stdlib ServeMux cannot express them, and zip is fiber/fasthttp, so
zip-native means moving the whole web layer off net/http. That is a real
decision, not a rename, and it is now a decision about one file.

models, modules, services, routers green; integration compile gate green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 17:55:28 -07:00

125 lines
3.4 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package common
import (
"context"
"fmt"
"github.com/hanzoai/git/modules/web"
"net/http"
"strings"
user_model "github.com/hanzoai/git/models/user"
"github.com/hanzoai/git/modules/log"
"github.com/hanzoai/git/modules/setting"
"github.com/hanzoai/git/modules/templates"
"github.com/hanzoai/git/modules/web/middleware"
"github.com/hanzoai/git/modules/web/routing"
"github.com/bohde/codel"
)
const tplStatus503 templates.TplName = "status/503"
type Priority int
func (p Priority) String() string {
switch p {
case HighPriority:
return "high"
case DefaultPriority:
return "default"
case LowPriority:
return "low"
default:
return fmt.Sprintf("%d", p)
}
}
const (
LowPriority = Priority(-10)
DefaultPriority = Priority(0)
HighPriority = Priority(10)
)
// QoS implements quality of service for requests, based upon whether
// or not the user is logged in. All traffic may get dropped, and
// anonymous users are deprioritized.
func QoS() func(next http.Handler) http.Handler {
if !setting.Service.QoS.Enabled {
return nil
}
maxOutstanding := setting.Service.QoS.MaxInFlightRequests
if maxOutstanding <= 0 {
maxOutstanding = 10
}
c := codel.NewPriority(codel.Options{
// The maximum number of waiting requests.
MaxPending: setting.Service.QoS.MaxWaitingRequests,
// The maximum number of in-flight requests.
MaxOutstanding: maxOutstanding,
// The target latency that a blocked request should wait
// for. After this, it might be dropped.
TargetLatency: setting.Service.QoS.TargetWaitTime,
})
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
reqRecordInfo := routing.GetRequestRecordInfo(ctx)
priority := requestPriority(ctx)
// Check if the request can begin processing.
err := c.Acquire(ctx, int(priority))
if err != nil {
log.Error("QoS error, dropping request of priority %s: %v", priority, err)
renderServiceUnavailable(w, req)
return
}
// Release long-polling immediately, so they don't always take up an in-flight request
if reqRecordInfo.IsLongPolling {
c.Release()
} else {
defer c.Release()
}
next.ServeHTTP(w, req)
})
}
}
// requestPriority assigns a priority value for a request based upon
// whether the user is logged in and how expensive the endpoint is
func requestPriority(ctx context.Context) Priority {
// If the user is logged in, assign high priority.
data := middleware.GetContextData(ctx)
if _, ok := data[middleware.ContextDataKeySignedUser].(*user_model.User); ok {
return HighPriority
}
rctx := web.GetRouteContext(ctx)
if rctx == nil {
return DefaultPriority
}
// If we're operating in the context of a repo, assign low priority
routePattern := rctx.RoutePattern()
if strings.HasPrefix(routePattern, "/{username}/{reponame}/") {
return LowPriority
}
return DefaultPriority
}
// renderServiceUnavailable will render an HTTP 503 Service
// Unavailable page, providing HTML if the client accepts it.
func renderServiceUnavailable(w http.ResponseWriter, req *http.Request) {
ctxData := middleware.GetContextData(req.Context())
renderServerErrorPage(w, req, http.StatusServiceUnavailable, tplStatus503, ctxData, "Service Unavailable")
}