mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat(healthcheck): add websocket report (#7689)
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/coder/coder/coderd/healthcheck"
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/httpmw"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
@ -29,11 +30,28 @@ func (api *API) debugCoordinator(rw http.ResponseWriter, r *http.Request) {
|
||||
// @Success 200 {object} healthcheck.Report
|
||||
// @Router /debug/health [get]
|
||||
func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
|
||||
apiKey := httpmw.APITokenFromRequest(r)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), api.HealthcheckTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Get cached report if it exists.
|
||||
if report := api.healthCheckCache.Load(); report != nil {
|
||||
if time.Since(report.Time) < api.HealthcheckRefresh {
|
||||
httpapi.Write(ctx, rw, http.StatusOK, report)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
resChan := api.healthCheckGroup.DoChan("", func() (*healthcheck.Report, error) {
|
||||
return api.HealthcheckFunc(ctx)
|
||||
// Create a new context not tied to the request.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), api.HealthcheckTimeout)
|
||||
defer cancel()
|
||||
|
||||
report, err := api.HealthcheckFunc(ctx, apiKey)
|
||||
if err == nil {
|
||||
api.healthCheckCache.Store(report)
|
||||
}
|
||||
return report, err
|
||||
})
|
||||
|
||||
select {
|
||||
@ -43,13 +61,19 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
return
|
||||
case res := <-resChan:
|
||||
if time.Since(res.Val.Time) > api.HealthcheckRefresh {
|
||||
api.healthCheckGroup.Forget("")
|
||||
api.debugDeploymentHealth(rw, r)
|
||||
return
|
||||
}
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusOK, res.Val)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// For some reason the swagger docs need to be attached to a function.
|
||||
//
|
||||
// @Summary Debug Info Websocket Test
|
||||
// @ID debug-info-websocket-test
|
||||
// @Security CoderSessionToken
|
||||
// @Produce json
|
||||
// @Tags Debug
|
||||
// @Success 201 {object} codersdk.Response
|
||||
// @Router /debug/ws [get]
|
||||
// @x-apidocgen {"skip": true}
|
||||
func _debugws(http.ResponseWriter, *http.Request) {} //nolint:unused
|
||||
|
Reference in New Issue
Block a user