Skip to content

Commit 30189aa

Browse files
committed
Merge branch 'main' into workspaceagent
2 parents ee0ee70 + 65de96c commit 30189aa

File tree

5 files changed

+85
-18
lines changed

5 files changed

+85
-18
lines changed

.github/workflows/coder.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
os:
123123
- ubuntu-latest
124124
- macos-latest
125-
- windows-latest
125+
- windows-2022
126126
steps:
127127
- uses: actions/checkout@v2
128128

@@ -138,9 +138,9 @@ jobs:
138138
~/.cache/go-build
139139
~/Library/Caches/go-build
140140
%LocalAppData%\go-build
141-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
141+
key: ${{ matrix.os }}-go-${{ hashFiles('**/go.sum') }}
142142
restore-keys: |
143-
${{ runner.os }}-go-
143+
${{ matrix.os }}-go-
144144
145145
- run: go install gotest.tools/gotestsum@latest
146146

@@ -150,11 +150,13 @@ jobs:
150150
terraform_wrapper: false
151151

152152
- name: Test with Mock Database
153+
shell: bash
153154
env:
155+
GOCOUNT: ${{ runner.os == 'Windows' && 3 || 5 }}
154156
GOMAXPROCS: ${{ runner.os == 'Windows' && 1 || 2 }}
155157
run: gotestsum --junitfile="gotests.xml" --packages="./..." --
156-
-covermode=atomic -coverprofile="gotests.coverage" -failfast
157-
-timeout=3m -count=5 -race -short
158+
-covermode=atomic -coverprofile="gotests.coverage"
159+
-timeout=3m -count=$GOCOUNT -race -short -failfast
158160

159161
- name: Upload DataDog Trace
160162
if: (success() || failure()) && github.actor != 'dependabot[bot]'
@@ -171,7 +173,7 @@ jobs:
171173
-count=1 -race -parallel=2 -failfast
172174

173175
- name: Upload DataDog Trace
174-
if: (success() || failure()) && github.actor != 'dependabot[bot]'
176+
if: (success() || failure()) && github.actor != 'dependabot[bot]' && runner.os == 'Linux'
175177
env:
176178
DATADOG_API_KEY: ${{ secrets.DATADOG_API_KEY }}
177179
DD_DATABASE: postgresql

peer/conn.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ func (c *Conn) init() error {
183183
}
184184
})
185185
c.rtc.OnConnectionStateChange(func(peerConnectionState webrtc.PeerConnectionState) {
186-
if c.isClosed() {
187-
// Make sure we don't log after Close() has been called.
188-
return
189-
}
190-
c.opts.Logger.Debug(context.Background(), "rtc connection updated",
191-
slog.F("state", peerConnectionState))
186+
go func() {
187+
c.closeMutex.Lock()
188+
defer c.closeMutex.Unlock()
189+
if c.isClosed() {
190+
return
191+
}
192+
c.opts.Logger.Debug(context.Background(), "rtc connection updated",
193+
slog.F("state", peerConnectionState))
194+
}()
192195

193196
switch peerConnectionState {
194197
case webrtc.PeerConnectionStateDisconnected:

pty/start_other.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import (
99
"syscall"
1010

1111
"github.com/creack/pty"
12+
"golang.org/x/xerrors"
1213
)
1314

1415
func startPty(cmd *exec.Cmd) (PTY, *os.Process, error) {
1516
ptty, tty, err := pty.Open()
1617
if err != nil {
17-
return nil, nil, err
18+
return nil, nil, xerrors.Errorf("open: %w", err)
1819
}
20+
defer func() {
21+
_ = tty.Close()
22+
}()
1923
cmd.SysProcAttr = &syscall.SysProcAttr{
2024
Setsid: true,
2125
Setctty: true,
@@ -26,7 +30,7 @@ func startPty(cmd *exec.Cmd) (PTY, *os.Process, error) {
2630
err = cmd.Start()
2731
if err != nil {
2832
_ = ptty.Close()
29-
return nil, nil, err
33+
return nil, nil, xerrors.Errorf("start: %w", err)
3034
}
3135
oPty := &otherPty{
3236
pty: ptty,

site/nextrouter/nextrouter.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ func Handler(fileSystem fs.FS, options *Options) (http.Handler, error) {
4949
}
5050

5151
// Fallback to static file server for non-HTML files
52-
// Non-HTML files don't have special routing rules, so we can just leverage
53-
// the built-in http.FileServer for it.
54-
fileHandler := http.FileServer(http.FS(fileSystem))
55-
router.NotFound(fileHandler.ServeHTTP)
52+
router.NotFound(FileHandler(fileSystem))
5653

5754
// Finally, if there is a 404.html available, serve that
5855
err = register404(fileSystem, router, *options)
@@ -64,6 +61,31 @@ func Handler(fileSystem fs.FS, options *Options) (http.Handler, error) {
6461
return router, nil
6562
}
6663

64+
// FileHandler serves static content, additionally adding immutable
65+
// cache-control headers for Next.js content
66+
func FileHandler(fileSystem fs.FS) func(writer http.ResponseWriter, request *http.Request) {
67+
// Non-HTML files don't have special routing rules, so we can just leverage
68+
// the built-in http.FileServer for it.
69+
fileHandler := http.FileServer(http.FS(fileSystem))
70+
71+
return func(writer http.ResponseWriter, request *http.Request) {
72+
// From the Next.js documentation:
73+
//
74+
// "Caching improves response times and reduces the number
75+
// of requests to external services. Next.js automatically
76+
// adds caching headers to immutable assets served from
77+
// /_next/static including JavaScript, CSS, static images,
78+
// and other media."
79+
//
80+
// See: https://nextjs.org/docs/going-to-production
81+
if strings.HasPrefix(request.URL.Path, "/_next/static/") {
82+
writer.Header().Add("Cache-Control", "public, max-age=31536000, immutable")
83+
}
84+
85+
fileHandler.ServeHTTP(writer, request)
86+
}
87+
}
88+
6789
// registerRoutes recursively traverses the file-system, building routes
6890
// as appropriate for respecting NextJS dynamic rules.
6991
func registerRoutes(rtr chi.Router, fileSystem fs.FS, options Options) error {

site/nextrouter/nextrouter_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,42 @@ func TestNextRouter(t *testing.T) {
376376
require.EqualValues(t, "test-create", body)
377377
})
378378

379+
t.Run("Caching headers for _next resources", func(t *testing.T) {
380+
t.Parallel()
381+
382+
rootFS := fstest.MapFS{
383+
"index.html": &fstest.MapFile{
384+
Data: []byte("test-root"),
385+
},
386+
"_next/static/test.js": &fstest.MapFile{
387+
Data: []byte("test.js cached forever"),
388+
},
389+
"_next/static/chunks/app/test.css": &fstest.MapFile{
390+
Data: []byte("test.css cached forever"),
391+
},
392+
}
393+
394+
router, err := nextrouter.Handler(rootFS, nil)
395+
require.NoError(t, err)
396+
397+
server := httptest.NewServer(router)
398+
t.Cleanup(server.Close)
399+
400+
res, err := request(server, "/index.html")
401+
require.NoError(t, err)
402+
require.NoError(t, res.Body.Close())
403+
404+
require.Equal(t, http.StatusOK, res.StatusCode)
405+
require.Empty(t, res.Header.Get("Cache-Control"))
406+
407+
res, err = request(server, "/_next/static/test.js")
408+
require.NoError(t, err)
409+
require.NoError(t, res.Body.Close())
410+
411+
require.Equal(t, http.StatusOK, res.StatusCode)
412+
require.Equal(t, "public, max-age=31536000, immutable", res.Header.Get("Cache-Control"))
413+
})
414+
379415
t.Run("Injects template parameters", func(t *testing.T) {
380416
t.Parallel()
381417

0 commit comments

Comments
 (0)