diff --git a/cloud.go b/cloud.go index e22b775..dc50428 100644 --- a/cloud.go +++ b/cloud.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "net/url" + "sync" "time" "github.com/coder/websocket/wsjson" @@ -121,6 +122,11 @@ var ( ) ) +var ( + cloudDisconnectChan chan error + cloudDisconnectLock = &sync.Mutex{} +) + func wsResetMetrics(established bool, sourceType string, source string) { metricConnectionLastPingTimestamp.WithLabelValues(sourceType, source).Set(-1) metricConnectionLastPingDuration.WithLabelValues(sourceType, source).Set(-1) diff --git a/web.go b/web.go index 7d7724b..97e002d 100644 --- a/web.go +++ b/web.go @@ -207,6 +207,24 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool, } }() + if isCloudConnection { + // create a channel to receive the disconnect event, once received, we cancelRun + cloudDisconnectChan = make(chan error) + defer func() { + close(cloudDisconnectChan) + cloudDisconnectChan = nil + }() + go func() { + for err := range cloudDisconnectChan { + if err == nil { + continue + } + cloudLogger.Infof("disconnecting from cloud due to: %v", err) + cancelRun() + } + }() + } + for { typ, msg, err := wsCon.Read(runCtx) if err != nil {