Skip to content

Commit 3cae4b0

Browse files
committed
Add latency check to wsproxy
1 parent 4b7a40e commit 3cae4b0

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

coderd/coderd.go

+11
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,17 @@ func New(options *Options) *API {
805805
return []string{}
806806
})
807807
r.NotFound(cspMW(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP))).ServeHTTP)
808+
809+
// This must be before all middleware to improve the response time.
810+
// So make a new router, and mount the old one as the root.
811+
rootRouter := chi.NewRouter()
812+
// This is the only route we add before all the middleware.
813+
// We want to time the latency of the request, so any middleware will
814+
// interfere with that timing.
815+
rootRouter.Get("/latency-check", LatencyCheck(api.AccessURL.String()))
816+
rootRouter.Mount("/", r)
817+
api.RootHandler = rootRouter
818+
808819
return api
809820
}
810821

coderd/coderd_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ func TestDERPLatencyCheck(t *testing.T) {
124124
require.Equal(t, http.StatusOK, res.StatusCode)
125125
}
126126

127+
func TestFastLatencyCheck(t *testing.T) {
128+
t.Parallel()
129+
client := coderdtest.New(t, nil)
130+
res, err := client.Request(context.Background(), http.MethodGet, "/latency-check", nil)
131+
require.NoError(t, err)
132+
defer res.Body.Close()
133+
require.Equal(t, http.StatusOK, res.StatusCode)
134+
}
135+
127136
func TestHealthz(t *testing.T) {
128137
t.Parallel()
129138
client := coderdtest.New(t, nil)

coderd/latencycheck.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
)
7+
8+
func LatencyCheck(allowedOrigins ...string) http.HandlerFunc {
9+
origins := strings.Join(allowedOrigins, ",")
10+
return func(rw http.ResponseWriter, r *http.Request) {
11+
// Allowing timing information to be shared. This allows the browser
12+
// to exclude TLS handshake timing.
13+
rw.Header().Set("Timing-Allow-Origin", origins)
14+
rw.WriteHeader(http.StatusOK)
15+
}
16+
}

enterprise/wsproxy/wsproxy.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/go-chi/cors"
14-
1513
"github.com/go-chi/chi/v5"
1614
"github.com/go-chi/cors"
1715
"github.com/google/uuid"
@@ -21,6 +19,7 @@ import (
2119

2220
"cdr.dev/slog"
2321
"github.com/coder/coder/buildinfo"
22+
"github.com/coder/coder/coderd"
2423
"github.com/coder/coder/coderd/httpapi"
2524
"github.com/coder/coder/coderd/httpmw"
2625
"github.com/coder/coder/coderd/tracing"
@@ -262,6 +261,12 @@ func New(ctx context.Context, opts *Options) (*Server, error) {
262261
})
263262
})
264263

264+
// See coderd/coderd.go for why we need this.
265+
rootRouter := chi.NewRouter()
266+
rootRouter.Get("/latency-check", coderd.LatencyCheck(s.DashboardURL.String(), s.AppServer.AccessURL.String()))
267+
rootRouter.Mount("/", r)
268+
s.Handler = rootRouter
269+
265270
return s, nil
266271
}
267272

0 commit comments

Comments
 (0)