Files
ziti/controller/webapis/timeout.go
T
Paul Lorenz 54bfb3ca21 Return a readable 503 when an API request times out. Fixes #4509
- extends the per-request write deadline in TimeoutHandler when the server's write timeout would expire before the timeout response can be written, so clients get the 503 instead of a TLS error
- passes the server's write timeout from each API's xweb server config into TimeoutHandler through a shared helper
- carries the request context on REST change contexts and refuses to dispatch a command once that request's deadline has passed
- updates xweb to v3.0.6, whose compression middleware supports http.ResponseController and keeps the response status for Brotli
- tests timeout responses over HTTP/1.1 and h2 for each compression encoding, and the dispatcher deadline check
2026-11-03 23:29:55 -05:00

41 lines
1.5 KiB
Go

/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package webapis
import (
"net/http"
"time"
"github.com/openziti/xweb/v3"
"github.com/openziti/ziti/v2/controller/api"
"github.com/openziti/ziti/v2/controller/apierror"
"github.com/openziti/ziti/v2/controller/response"
)
// apiRequestTimeout is how long a REST API request may run before it is answered with a timeout error.
const apiRequestTimeout = 10 * time.Second
// wrapWithTimeout adds CORS handling and the API request timeout to handler, keeping the connection writable for
// the timeout response regardless of serverConfig's write timeout. serverConfig may be nil.
func wrapWithTimeout(handler http.Handler, serverConfig *xweb.ServerConfig) http.Handler {
var writeTimeout time.Duration
if serverConfig != nil {
writeTimeout = serverConfig.Options.WriteTimeout
}
return api.TimeoutHandler(api.WrapCorsHandler(handler), apiRequestTimeout, writeTimeout, apierror.NewTimeoutError(), response.EdgeResponseMapper{})
}