Skip to content

Commit abc6d92

Browse files
authored
feat(cloud): disconnect from cloud immediately when cloud URL changes… (#326)
2 parents 8268b20 + 73e7151 commit abc6d92

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

cloud.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"sync"
1011
"time"
1112

1213
"github.com/coder/websocket/wsjson"
@@ -113,6 +114,11 @@ var (
113114
)
114115
)
115116

117+
var (
118+
cloudDisconnectChan chan error
119+
cloudDisconnectLock = &sync.Mutex{}
120+
)
121+
116122
func cloudResetMetrics(established bool) {
117123
metricCloudConnectionLastPingTimestamp.Set(-1)
118124
metricCloudConnectionLastPingDuration.Set(-1)
@@ -213,6 +219,24 @@ func handleCloudRegister(c *gin.Context) {
213219
c.JSON(200, gin.H{"message": "Cloud registration successful"})
214220
}
215221

222+
func disconnectCloud(reason error) {
223+
cloudDisconnectLock.Lock()
224+
defer cloudDisconnectLock.Unlock()
225+
226+
if cloudDisconnectChan == nil {
227+
cloudLogger.Tracef("cloud disconnect channel is not set, no need to disconnect")
228+
return
229+
}
230+
231+
// just in case the channel is closed, we don't want to panic
232+
defer func() {
233+
if r := recover(); r != nil {
234+
cloudLogger.Infof("cloud disconnect channel is closed, no need to disconnect: %v", r)
235+
}
236+
}()
237+
cloudDisconnectChan <- reason
238+
}
239+
216240
func runWebsocketClient() error {
217241
if config.CloudToken == "" {
218242
time.Sleep(5 * time.Second)
@@ -275,6 +299,23 @@ func runWebsocketClient() error {
275299
metricCloudConnectionLastPingTimestamp.SetToCurrentTime()
276300
}
277301
}()
302+
303+
// create a channel to receive the disconnect event, once received, we cancelRun
304+
cloudDisconnectChan = make(chan error)
305+
defer func() {
306+
close(cloudDisconnectChan)
307+
cloudDisconnectChan = nil
308+
}()
309+
go func() {
310+
for err := range cloudDisconnectChan {
311+
if err == nil {
312+
continue
313+
}
314+
cloudLogger.Infof("disconnecting from cloud due to: %v", err)
315+
cancelRun()
316+
}
317+
}()
318+
278319
for {
279320
typ, msg, err := c.Read(runCtx)
280321
if err != nil {
@@ -448,6 +489,9 @@ func rpcDeregisterDevice() error {
448489
return fmt.Errorf("failed to save configuration after deregistering: %w", err)
449490
}
450491

492+
cloudLogger.Infof("device deregistered, disconnecting from cloud")
493+
disconnectCloud(fmt.Errorf("device deregistered"))
494+
451495
return nil
452496
}
453497

jsonrpc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,14 @@ func rpcSetUsbDeviceState(device string, enabled bool) error {
771771
}
772772

773773
func rpcSetCloudUrl(apiUrl string, appUrl string) error {
774+
currentCloudURL := config.CloudURL
774775
config.CloudURL = apiUrl
775776
config.CloudAppURL = appUrl
776777

778+
if currentCloudURL != apiUrl {
779+
disconnectCloud(fmt.Errorf("cloud url changed from %s to %s", currentCloudURL, apiUrl))
780+
}
781+
777782
if err := SaveConfig(); err != nil {
778783
return fmt.Errorf("failed to save config: %w", err)
779784
}

0 commit comments

Comments
 (0)