Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8db2066

Browse files
committed
move to channel over WorkspaceWatcher
1 parent4acfc7a commit8db2066

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+
defercancel()
630+
wc,err:=client.WatchWorkspace(ctx,w.ID)
629631
require.NoError(t,err)
630-
deferww.Close()
631-
fori:=0;i<5;i++ {
632-
_,err:=ww.Read(context.Background())
633-
require.NoError(t,err)
632+
fori:=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
returnworkspaceBuild,json.NewDecoder(res.Body).Decode(&workspaceBuild)
9595
}
9696

97-
typeWorkspaceWatcherstruct {
98-
conn*websocket.Conn
99-
}
100-
101-
func (w*WorkspaceWatcher)Read(ctx context.Context) (Workspace,error) {
102-
varwsWorkspace
103-
err:=wsjson.Read(ctx,w.conn,&ws)
104-
iferr!=nil {
105-
returnws,xerrors.Errorf("read workspace: %w",err)
106-
}
107-
108-
returnws,nil
109-
}
110-
111-
func (w*WorkspaceWatcher)Close()error {
112-
err:=w.conn.Close(websocket.StatusNormalClosure,"")
113-
iferr!=nil {
114-
returnxerrors.Errorf("closing workspace watcher: %w",err)
115-
}
116-
117-
returnnil
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) (<-chanWorkspace,error) {
12198
conn,err:=c.dialWebsocket(ctx,fmt.Sprintf("/api/v2/workspaces/%s/watch",id))
12299
iferr!=nil {
123100
returnnil,err
124101
}
125-
126-
return&WorkspaceWatcher{
127-
conn:conn,
128-
},nil
102+
wc:=make(chanWorkspace,256)
103+
104+
gofunc() {
105+
deferclose(wc)
106+
deferconn.Close(websocket.StatusNormalClosure,"")
107+
108+
for {
109+
select {
110+
case<-ctx.Done():
111+
return
112+
default:
113+
varwsWorkspace
114+
err:=wsjson.Read(ctx,conn,&ws)
115+
iferr!=nil {
116+
conn.Close(websocket.StatusInternalError,"failed to read workspace")
117+
return
118+
}
119+
wc<-ws
120+
}
121+
}
122+
}()
123+
124+
returnwc,nil
129125
}
130126

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp