Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f029650

Browse files
committed
move to channel over WorkspaceWatcher
1 parent be27df5 commit f029650

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
@@ -634,15 +634,14 @@ func TestWorkspaceWatcher(t *testing.T) {
634634
w, err := client.Workspace(context.Background(), workspace.ID)
635635
require.NoError(t, err)
636636

637-
ww, err := client.WatchWorkspace(context.Background(), w.ID)
637+
ctx, cancel := context.WithCancel(context.Background())
638+
defer cancel()
639+
wc, err := client.WatchWorkspace(ctx, w.ID)
638640
require.NoError(t, err)
639-
defer ww.Close()
640-
for i := 0; i < 5; i++ {
641-
_, err := ww.Read(context.Background())
642-
require.NoError(t, err)
641+
for i := 0; i < 3; i++ {
642+
_, more := <-wc
643+
require.True(t, more)
643644
}
644-
err = ww.Close()
645-
require.NoError(t, err)
646-
_, err = ww.Read(context.Background())
647-
require.Error(t, err)
645+
cancel()
646+
require.EqualValues(t, codersdk.Workspace{}, <-wc)
648647
}

codersdk/workspaces.go

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

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

132-
return &WorkspaceWatcher{
133-
conn: conn,
134-
}, nil
130+
return wc, nil
135131
}
136132

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

0 commit comments

Comments
 (0)