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

Skip to content

Commit 8db2066

Browse files
committed
move to channel over WorkspaceWatcher
1 parent 4acfc7a commit 8db2066

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

coderd/workspaces_test.go

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

628-
ww, err := client.WatchWorkspace(context.Background(), w.ID)
628+
ctx, cancel := context.WithCancel(context.Background())
629+
defer cancel()
630+
wc, err := client.WatchWorkspace(ctx, w.ID)
629631
require.NoError(t, err)
630-
defer ww.Close()
631-
for i := 0; i < 5; i++ {
632-
_, err := ww.Read(context.Background())
633-
require.NoError(t, err)
632+
for i := 0; i < 3; i++ {
633+
_, more := <-wc
634+
require.True(t, more)
634635
}
635-
err = ww.Close()
636-
require.NoError(t, err)
637-
_, err = ww.Read(context.Background())
638-
require.Error(t, err)
636+
cancel()
637+
require.EqualValues(t, codersdk.Workspace{}, <-wc)
639638
}

codersdk/workspaces.go

Lines changed: 24 additions & 28 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
}
125-
126-
return &WorkspaceWatcher{
127-
conn: conn,
128-
}, nil
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+
}()
123+
124+
return wc, nil
129125
}
130126

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

0 commit comments

Comments
 (0)