Skip to content

Commit 0160863

Browse files
fix(jetbrains): avoid connection loops
1 parent db6f5ed commit 0160863

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

components/ide/jetbrains/gateway-plugin/src/main/kotlin/io/gitpod/jetbrains/gateway/GitpodConnectionProvider.kt

+26-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import com.jetbrains.gateway.ssh.ClientOverSshTunnelConnector
3131
import com.jetbrains.gateway.ssh.SshHostTunnelConnector
3232
import com.jetbrains.gateway.thinClientLink.ThinClientHandle
3333
import com.jetbrains.rd.util.URI
34+
import com.jetbrains.rd.util.addUnique
35+
import com.jetbrains.rd.util.concurrentMapOf
3436
import com.jetbrains.rd.util.lifetime.Lifetime
3537
import io.gitpod.gitpodprotocol.api.entities.WorkspaceInstance
3638
import io.gitpod.jetbrains.icons.GitpodIcons
@@ -47,9 +49,12 @@ import javax.swing.JComponent
4749
import javax.swing.JLabel
4850
import kotlin.coroutines.coroutineContext
4951

52+
class GitpodConnection(val gitpodHost: String, val workspaceId: String, val backendPort: String?) {}
53+
5054
@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
5155
class GitpodConnectionProvider : GatewayConnectionProvider {
52-
56+
// private val activeConnections = mutableListOf<GitpodConnection>()
57+
private val activeConnectionsC = concurrentMapOf<String, Boolean>()
5358
private val gitpod = service<GitpodConnectionService>()
5459

5560
private val httpClient = HttpClient.newBuilder()
@@ -74,6 +79,16 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
7479
parameters["workspaceId"]!!,
7580
parameters["backendPort"]
7681
)
82+
83+
val connectionKeyId = "${connectParams.gitpodHost}-${connectParams.workspaceId}-${connectParams.backendPort}"
84+
85+
if (activeConnectionsC.containsKey(connectionKeyId)) {
86+
thisLogger().warn("THE CONNECTION ALREADY EXISTS!!")
87+
throw IllegalArgumentException("THE CONNECTION ALREADY EXISTS!!")
88+
} else {
89+
thisLogger().warn("NEW CONNECTION!!!!!")
90+
}
91+
7792
val client = gitpod.obtainClient(connectParams.gitpodHost)
7893
val connectionLifetime = Lifetime.Eternal.createNested()
7994
val updates = client.listenToWorkspace(connectionLifetime, connectParams.workspaceId)
@@ -230,6 +245,15 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
230245
setErrorMessage("failed to fetch JetBrains Gateway Join Link.")
231246
return@launch
232247
}
248+
249+
// TODO: is this the right place?
250+
// val newConnection = GitpodConnection(connectParams.gitpodHost, connectParams.workspaceId, connectParams.backendPort)
251+
// activeConnections.add(newConnection)
252+
val connectionKeyId = "${connectParams.gitpodHost}-${connectParams.workspaceId}-${connectParams.backendPort}"
253+
activeConnectionsC[connectionKeyId] = true
254+
255+
thisLogger().warn("Storing connection for ${connectParams.gitpodHost} - ${connectParams.workspaceId} - ${connectParams.backendPort}")
256+
233257
val connector = ClientOverSshTunnelConnector(
234258
connectionLifetime,
235259
SshHostTunnelConnector(credentials),
@@ -239,6 +263,7 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
239263
clientHandle.clientClosed.advise(connectionLifetime) {
240264
application.invokeLater {
241265
connectionLifetime.terminate()
266+
// todo: remove this connection from activeConnections
242267
}
243268
}
244269
clientHandle.onClientPresenceChanged.advise(connectionLifetime) {

0 commit comments

Comments
 (0)