mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
feat: Workspace Proxy picker show latency to each proxy (#7486)
* chore: Add cors to workspace proxies to allow for latency checks * Add latency check to wsproxy Use performance API timings. - Fix cors and timing headers - Accept custom headers
This commit is contained in:
@ -806,6 +806,17 @@ func New(options *Options) *API {
|
||||
return []string{}
|
||||
})
|
||||
r.NotFound(cspMW(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP))).ServeHTTP)
|
||||
|
||||
// This must be before all middleware to improve the response time.
|
||||
// So make a new router, and mount the old one as the root.
|
||||
rootRouter := chi.NewRouter()
|
||||
// This is the only route we add before all the middleware.
|
||||
// We want to time the latency of the request, so any middleware will
|
||||
// interfere with that timing.
|
||||
rootRouter.Get("/latency-check", LatencyCheck(api.AccessURL))
|
||||
rootRouter.Mount("/", r)
|
||||
api.RootHandler = rootRouter
|
||||
|
||||
return api
|
||||
}
|
||||
|
||||
|
@ -124,6 +124,15 @@ func TestDERPLatencyCheck(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
func TestFastLatencyCheck(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
res, err := client.Request(context.Background(), http.MethodGet, "/latency-check", nil)
|
||||
require.NoError(t, err)
|
||||
defer res.Body.Close()
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
func TestHealthz(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
|
24
coderd/latencycheck.go
Normal file
24
coderd/latencycheck.go
Normal file
@ -0,0 +1,24 @@
|
||||
package coderd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func LatencyCheck(allowedOrigins ...*url.URL) http.HandlerFunc {
|
||||
allowed := make([]string, 0, len(allowedOrigins))
|
||||
for _, origin := range allowedOrigins {
|
||||
// Allow the origin without a path
|
||||
tmp := *origin
|
||||
tmp.Path = ""
|
||||
allowed = append(allowed, strings.TrimSuffix(origin.String(), "/"))
|
||||
}
|
||||
origins := strings.Join(allowed, ",")
|
||||
return func(rw http.ResponseWriter, r *http.Request) {
|
||||
// Allowing timing information to be shared. This allows the browser
|
||||
// to exclude TLS handshake timing.
|
||||
rw.Header().Set("Timing-Allow-Origin", origins)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user