@@ -10,6 +10,7 @@ import com.intellij.openapi.diagnostic.thisLogger
1010import com.intellij.remoteDev.util.onTerminationOrNow
1111import com.intellij.ui.RowIcon
1212import com.intellij.util.application
13+ import com.jetbrains.rd.framework.util.launch
1314import com.jetbrains.rd.platform.codeWithMe.portForwarding.*
1415import com.jetbrains.rd.util.URI
1516import com.jetbrains.rd.util.lifetime.Lifetime
@@ -22,10 +23,13 @@ import io.gitpod.supervisor.api.StatusServiceGrpc
2223import io.grpc.stub.ClientCallStreamObserver
2324import io.grpc.stub.ClientResponseObserver
2425import io.ktor.utils.io.*
26+ import kotlinx.coroutines.cancel
27+ import kotlinx.coroutines.delay
28+ import kotlinx.coroutines.future.asDeferred
29+ import kotlinx.coroutines.isActive
2530import org.apache.http.client.utils.URIBuilder
2631import java.util.*
2732import java.util.concurrent.CompletableFuture
28- import java.util.concurrent.TimeUnit
2933
3034@Suppress(" UnstableApiUsage" )
3135class GitpodPortForwardingServiceImpl : GitpodPortForwardingService {
@@ -46,22 +50,25 @@ class GitpodPortForwardingServiceImpl : GitpodPortForwardingService {
4650 observePortsListWhileProjectIsOpen()
4751 }
4852
49- private fun observePortsListWhileProjectIsOpen () = application.executeOnPooledThread {
50- lifetime.executeIfAlive {
53+ private fun observePortsListWhileProjectIsOpen () = lifetime.launch {
54+ while (isActive) {
5155 try {
52- observePortsList().get ()
56+ observePortsList().asDeferred().await ()
5357 } catch (throwable: Throwable ) {
5458 when (throwable) {
55- is InterruptedException , is CancellationException -> lifetime.terminate()
56- else -> thisLogger().error(
57- " gitpod: Got an error while trying to get ports list from Supervisor. " +
58- " Going to try again in a second." ,
59- throwable
60- )
59+ is InterruptedException , is CancellationException -> {
60+ cancel(" gitpod: Stopped observing ports list due to an expected interruption." )
61+ }
62+ else -> {
63+ thisLogger().warn(
64+ " gitpod: Got an error while trying to get ports list from Supervisor. " +
65+ " Going to try again in a second." ,
66+ throwable
67+ )
68+ delay(1000 )
69+ }
6170 }
6271 }
63-
64- TimeUnit .SECONDS .sleep(1 )
6572 }
6673 }
6774
@@ -75,7 +82,7 @@ class GitpodPortForwardingServiceImpl : GitpodPortForwardingService {
7582 val portsStatusResponseObserver = object :
7683 ClientResponseObserver <Status .PortsStatusRequest , Status .PortsStatusResponse > {
7784 override fun beforeStart (request : ClientCallStreamObserver <Status .PortsStatusRequest >) {
78- lifetime.onTerminationOrNow { request.cancel(" gitpod: Project terminated." , null ) }
85+ lifetime.onTerminationOrNow { request.cancel(" gitpod: Service lifetime terminated." , null ) }
7986 }
8087
8188 override fun onNext (response : Status .PortsStatusResponse ) {
0 commit comments