|
8 | 8 | "path"
|
9 | 9 | "slices"
|
10 | 10 | "strings"
|
11 |
| - "sync" |
12 | 11 | "time"
|
13 | 12 |
|
14 | 13 | "github.com/go-chi/chi/v5"
|
@@ -38,11 +37,9 @@ type API struct {
|
38 | 37 |
|
39 | 38 | // lockCh protects the below fields. We use a channel instead of a
|
40 | 39 | // mutex so we can handle cancellation properly.
|
41 |
| - lockCh chan struct{} |
42 |
| - containers codersdk.WorkspaceAgentListContainersResponse |
43 |
| - mtime time.Time |
44 |
| - |
45 |
| - devcontainersMu sync.Mutex // Protects following. |
| 40 | + lockCh chan struct{} |
| 41 | + containers codersdk.WorkspaceAgentListContainersResponse |
| 42 | + mtime time.Time |
46 | 43 | devcontainerNames map[string]struct{} // Track devcontainer names to avoid duplicates.
|
47 | 44 | knownDevcontainers []codersdk.WorkspaceAgentDevcontainer // Track predefined and runtime-detected devcontainers.
|
48 | 45 | }
|
@@ -168,9 +165,6 @@ func (api *API) getContainers(ctx context.Context) (codersdk.WorkspaceAgentListC
|
168 | 165 | api.containers = updated
|
169 | 166 | api.mtime = now
|
170 | 167 |
|
171 |
| - api.devcontainersMu.Lock() |
172 |
| - defer api.devcontainersMu.Unlock() |
173 |
| - |
174 | 168 | // Reset all known devcontainers to not running.
|
175 | 169 | for i := range api.knownDevcontainers {
|
176 | 170 | api.knownDevcontainers[i].Running = false
|
@@ -294,9 +288,13 @@ func (api *API) handleListDevcontainers(w http.ResponseWriter, r *http.Request)
|
294 | 288 | return
|
295 | 289 | }
|
296 | 290 |
|
297 |
| - api.devcontainersMu.Lock() |
| 291 | + select { |
| 292 | + case <-ctx.Done(): |
| 293 | + return |
| 294 | + case api.lockCh <- struct{}{}: |
| 295 | + } |
298 | 296 | devcontainers := slices.Clone(api.knownDevcontainers)
|
299 |
| - api.devcontainersMu.Unlock() |
| 297 | + <-api.lockCh |
300 | 298 |
|
301 | 299 | slices.SortFunc(devcontainers, func(a, b codersdk.WorkspaceAgentDevcontainer) int {
|
302 | 300 | if cmp := strings.Compare(a.WorkspaceFolder, b.WorkspaceFolder); cmp != 0 {
|
|
0 commit comments