Skip to content

Commit b53552d

Browse files
committed
init lockCh in New(), remove sync.Once
1 parent 51e5976 commit b53552d

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

agent/agentcontainers/containers.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"net/http"
77
"slices"
8-
"sync"
98
"time"
109

1110
"golang.org/x/xerrors"
@@ -26,7 +25,6 @@ type devcontainersHandler struct {
2625
cl Lister
2726
clock quartz.Clock
2827

29-
initLockOnce sync.Once // ensures we don't get a race when initializing lockCh
3028
// lockCh protects the below fields. We use a channel instead of a mutex so we
3129
// can handle cancellation properly.
3230
lockCh chan struct{}
@@ -47,7 +45,9 @@ func WithLister(cl Lister) Option {
4745

4846
// New returns a new devcontainersHandler with the given options applied.
4947
func New(options ...Option) http.Handler {
50-
ch := &devcontainersHandler{}
48+
ch := &devcontainersHandler{
49+
lockCh: make(chan struct{}, 1),
50+
}
5151
for _, opt := range options {
5252
opt(ch)
5353
}
@@ -81,11 +81,6 @@ func (ch *devcontainersHandler) ServeHTTP(rw http.ResponseWriter, r *http.Reques
8181
}
8282

8383
func (ch *devcontainersHandler) getContainers(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) {
84-
ch.initLockOnce.Do(func() {
85-
if ch.lockCh == nil {
86-
ch.lockCh = make(chan struct{}, 1)
87-
}
88-
})
8984
select {
9085
case <-ctx.Done():
9186
return codersdk.WorkspaceAgentListContainersResponse{}, ctx.Err()

agent/agentcontainers/containers_internal_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func TestContainersHandler(t *testing.T) {
163163
cl: mockLister,
164164
clock: clk,
165165
containers: &tc.cacheData,
166+
lockCh: make(chan struct{}, 1),
166167
}
167168
)
168169
if tc.cacheAge != 0 {

0 commit comments

Comments
 (0)