Skip to content

Commit 6e666be

Browse files
committed
move to channel over WorkspaceWatcher
1 parent 84a5231 commit 6e666be

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

coderd/workspaces_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -638,15 +638,14 @@ func TestWorkspaceWatcher(t *testing.T) {
638638
w, err := client.Workspace(context.Background(), workspace.ID)
639639
require.NoError(t, err)
640640

641-
ww, err := client.WatchWorkspace(context.Background(), w.ID)
641+
ctx, cancel := context.WithCancel(context.Background())
642+
defer cancel()
643+
wc, err := client.WatchWorkspace(ctx, w.ID)
642644
require.NoError(t, err)
643-
defer ww.Close()
644-
for i := 0; i < 5; i++ {
645-
_, err := ww.Read(context.Background())
646-
require.NoError(t, err)
645+
for i := 0; i < 3; i++ {
646+
_, more := <-wc
647+
require.True(t, more)
647648
}
648-
err = ww.Close()
649-
require.NoError(t, err)
650-
_, err = ww.Read(context.Background())
651-
require.Error(t, err)
649+
cancel()
650+
require.EqualValues(t, codersdk.Workspace{}, <-wc)
652651
}

codersdk/workspaces.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,38 +94,34 @@ func (c *Client) WorkspaceBuildByName(ctx context.Context, workspace uuid.UUID,
9494
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
9595
}
9696

97-
type WorkspaceWatcher struct {
98-
conn *websocket.Conn
99-
}
100-
101-
func (w *WorkspaceWatcher) Read(ctx context.Context) (Workspace, error) {
102-
var ws Workspace
103-
err := wsjson.Read(ctx, w.conn, &ws)
104-
if err != nil {
105-
return ws, xerrors.Errorf("read workspace: %w", err)
106-
}
107-
108-
return ws, nil
109-
}
110-
111-
func (w *WorkspaceWatcher) Close() error {
112-
err := w.conn.Close(websocket.StatusNormalClosure, "")
113-
if err != nil {
114-
return xerrors.Errorf("closing workspace watcher: %w", err)
115-
}
116-
117-
return nil
118-
}
119-
120-
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
97+
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (<-chan Workspace, error) {
12198
conn, err := c.dialWebsocket(ctx, fmt.Sprintf("/api/v2/workspaces/%s/watch", id))
12299
if err != nil {
123100
return nil, err
124101
}
102+
wc := make(chan Workspace, 256)
103+
104+
go func() {
105+
defer close(wc)
106+
defer conn.Close(websocket.StatusNormalClosure, "")
107+
108+
for {
109+
select {
110+
case <-ctx.Done():
111+
return
112+
default:
113+
var ws Workspace
114+
err := wsjson.Read(ctx, conn, &ws)
115+
if err != nil {
116+
conn.Close(websocket.StatusInternalError, "failed to read workspace")
117+
return
118+
}
119+
wc <- ws
120+
}
121+
}
122+
}()
125123

126-
return &WorkspaceWatcher{
127-
conn: conn,
128-
}, nil
124+
return wc, nil
129125
}
130126

131127
// UpdateWorkspaceAutostartRequest is a request to update a workspace's autostart schedule.

0 commit comments

Comments
 (0)