Skip to content

Commit dad42fe

Browse files
authored
feat: gzip static http server assets (coder#2272)
1 parent d057e8c commit dad42fe

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

coderd/coderd.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import (
44
"context"
55
"crypto/x509"
66
"fmt"
7+
"io"
78
"net/http"
89
"net/url"
910
"sync"
1011
"time"
1112

13+
"github.com/andybalholm/brotli"
1214
"github.com/go-chi/chi/v5"
1315
"github.com/go-chi/chi/v5/middleware"
16+
"github.com/klauspost/compress/zstd"
1417
"github.com/pion/webrtc/v3"
18+
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1519
"golang.org/x/xerrors"
1620
"google.golang.org/api/idtoken"
1721

18-
sdktrace "go.opentelemetry.io/otel/sdk/trace"
19-
2022
"cdr.dev/slog"
2123
"github.com/coder/coder/buildinfo"
2224
"github.com/coder/coder/coderd/awsidentity"
@@ -350,7 +352,8 @@ func New(options *Options) *API {
350352
r.Get("/state", api.workspaceBuildState)
351353
})
352354
})
353-
r.NotFound(api.siteHandler.ServeHTTP)
355+
356+
r.NotFound(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP)).ServeHTTP)
354357
return api
355358
}
356359

@@ -381,3 +384,23 @@ func debugLogRequest(log slog.Logger) func(http.Handler) http.Handler {
381384
})
382385
}
383386
}
387+
388+
func compressHandler(h http.Handler) http.Handler {
389+
cmp := middleware.NewCompressor(5,
390+
"text/*",
391+
"application/*",
392+
"image/*",
393+
)
394+
cmp.SetEncoder("br", func(w io.Writer, level int) io.Writer {
395+
return brotli.NewWriterLevel(w, level)
396+
})
397+
cmp.SetEncoder("zstd", func(w io.Writer, level int) io.Writer {
398+
zw, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
399+
if err != nil {
400+
panic("invalid zstd compressor: " + err.Error())
401+
}
402+
return zw
403+
})
404+
405+
return cmp.Handler(h)
406+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
cdr.dev/slog v1.4.2-0.20220525200111-18dce5c2cd5f
4444
cloud.google.com/go/compute v1.6.1
4545
github.com/AlecAivazis/survey/v2 v2.3.4
46+
github.com/andybalholm/brotli v1.0.4
4647
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2
4748
github.com/awalterschulze/gographviz v2.0.3+incompatible
4849
github.com/bgentry/speakeasy v0.1.0
@@ -180,7 +181,7 @@ require (
180181
github.com/imdario/mergo v0.3.12 // indirect
181182
github.com/inconshreveable/mousetrap v1.0.0 // indirect
182183
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
183-
github.com/klauspost/compress v1.15.0 // indirect
184+
github.com/klauspost/compress v1.15.6
184185
github.com/kr/fs v0.1.0 // indirect
185186
github.com/leodido/go-urn v1.2.1 // indirect
186187
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:C
165165
github.com/alexflint/go-filemutex v1.1.0/go.mod h1:7P4iRhttt/nUvUOrYIhcpMzv2G6CY9UnI16Z+UJqRyk=
166166
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
167167
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
168+
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
169+
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
168170
github.com/andybalholm/crlf v0.0.0-20171020200849-670099aa064f/go.mod h1:k8feO4+kXDxro6ErPXBRTJ/ro2mf0SsFG8s7doP9kJE=
169171
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
170172
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
@@ -1050,8 +1052,8 @@ github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8
10501052
github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
10511053
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
10521054
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
1053-
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
1054-
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
1055+
github.com/klauspost/compress v1.15.6 h1:6D9PcO8QWu0JyaQ2zUMmu16T1T+zjjEpP91guRsvDfY=
1056+
github.com/klauspost/compress v1.15.6/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
10551057
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
10561058
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
10571059
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

provisionersdk/agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ set -eux pipefail
2121
BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder
2222
BINARY_URL=${ACCESS_URL}bin/coder-linux-${ARCH}
2323
if which curl >/dev/null 2>&1; then
24-
curl -fsSL "${BINARY_URL}" -o "${BINARY_LOCATION}"
24+
curl -fsSL --compressed "${BINARY_URL}" -o "${BINARY_LOCATION}"
2525
elif which wget >/dev/null 2>&1; then
2626
wget -q "${BINARY_URL}" -O "${BINARY_LOCATION}"
2727
elif which busybox >/dev/null 2>&1; then
@@ -38,7 +38,7 @@ exec $BINARY_LOCATION agent`
3838
darwinScript = `#!/usr/bin/env sh
3939
set -eux pipefail
4040
BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder
41-
curl -fsSL "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_LOCATION}"
41+
curl -fsSL --compressed "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_LOCATION}"
4242
chmod +x $BINARY_LOCATION
4343
export CODER_AGENT_AUTH="${AUTH_TYPE}"
4444
export CODER_AGENT_URL="${ACCESS_URL}"

0 commit comments

Comments
 (0)