@@ -10,13 +10,18 @@ import (
1010 "strings"
1111
1212 user_model "code.gitea.io/gitea/models/user"
13+ "code.gitea.io/gitea/modules/log"
1314 "code.gitea.io/gitea/modules/setting"
15+ "code.gitea.io/gitea/modules/templates"
1416 "code.gitea.io/gitea/modules/web/middleware"
17+ giteacontext "code.gitea.io/gitea/services/context"
1518
1619 "github.com/bohde/codel"
1720 "github.com/go-chi/chi/v5"
1821)
1922
23+ const tplStatus503 templates.TplName = "status/503"
24+
2025type Priority int
2126
2227func (p Priority ) String () string {
@@ -70,8 +75,7 @@ func QoS() func(next http.Handler) http.Handler {
7075 // Check if the request can begin processing.
7176 err := c .Acquire (ctx , int (priority ))
7277 if err != nil {
73- // If it failed, the service is over capacity and should error
74- http .Error (w , "Service Unavailable" , http .StatusServiceUnavailable )
78+ renderServiceUnavailable (w , req )
7579 return
7680 }
7781
@@ -110,3 +114,31 @@ func requestPriority(ctx context.Context) Priority {
110114
111115 return DefaultPriority
112116}
117+
118+ // renderServiceUnavailable will render an HTTP 503 Service
119+ // Unavailable page, providing HTML if the client accepts it.
120+ func renderServiceUnavailable (w http.ResponseWriter , req * http.Request ) {
121+ acceptsHTML := false
122+ for _ , part := range req .Header ["Accept" ] {
123+ if strings .Contains (part , "text/html" ) {
124+ acceptsHTML = true
125+ break
126+ }
127+ }
128+
129+ // If the client doesn't accept HTML, then render a plain text response
130+ if ! acceptsHTML {
131+ http .Error (w , "503 Service Unavailable" , http .StatusServiceUnavailable )
132+ return
133+ }
134+
135+ tmplCtx := giteacontext.TemplateContext {}
136+ tmplCtx ["Locale" ] = middleware .Locale (w , req )
137+ ctxData := middleware .GetContextData (req .Context ())
138+ err := templates .HTMLRenderer ().HTML (w , http .StatusServiceUnavailable , tplStatus503 , ctxData , tmplCtx )
139+ if err != nil {
140+ log .Error ("Error occurs again when rendering service unavailable page: %v" , err )
141+ w .WriteHeader (http .StatusInternalServerError )
142+ _ , _ = w .Write ([]byte ("Internal server error, please collect error logs and report to Gitea issue tracker" ))
143+ }
144+ }
0 commit comments