fix: code completion grpc connections upon client deadline timeouts

This commit is contained in:
Carl-Robert Linnupuu 2025-12-03 22:01:56 +00:00
parent fd57cc0b4d
commit f12656bd99
3 changed files with 27 additions and 43 deletions

View file

@ -1,13 +1,13 @@
package ee.carlrobert.codegpt.codecompletions.edit
import com.intellij.codeInsight.inline.completion.elements.InlineCompletionElement
import com.intellij.openapi.editor.Editor
import ee.carlrobert.codegpt.CodeGPTKeys
import com.intellij.notification.NotificationType
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.editor.Editor
import ee.carlrobert.codegpt.CodeGPTKeys
import ee.carlrobert.codegpt.codecompletions.CodeCompletionEventListener
import ee.carlrobert.llm.client.openai.completion.ErrorDetails
import ee.carlrobert.codegpt.ui.OverlayUtil
import ee.carlrobert.llm.client.openai.completion.ErrorDetails
import ee.carlrobert.service.PartialCodeCompletionResponse
import io.grpc.Status
import io.grpc.StatusRuntimeException
@ -43,26 +43,25 @@ class CodeCompletionStreamObserver(
}
override fun onError(t: Throwable?) {
if (t is StatusRuntimeException && t.status.code == Status.Code.CANCELLED) {
eventListener.onCancelled(messageBuilder)
channel.close()
return
}
logger.error("Error occurred while fetching code completion", t)
if (t is StatusRuntimeException) {
val code = t.status.code
if (code != Status.Code.UNAVAILABLE && code != Status.Code.DEADLINE_EXCEEDED) {
OverlayUtil.showNotification(
t.message ?: "Something went wrong",
NotificationType.ERROR
)
if (code == Status.Code.CANCELLED || code == Status.Code.DEADLINE_EXCEEDED) {
eventListener.onComplete(messageBuilder)
return
}
if (code == Status.Code.UNAVAILABLE) {
eventListener.onError(ErrorDetails("Connection unavailable"), t)
channel.close(t)
return
}
} else {
OverlayUtil.showNotification(
t?.message ?: "Something went wrong",
NotificationType.ERROR
)
}
logger.error("Unexpected error occurred while fetching code completion", t)
OverlayUtil.showNotification(
t?.message ?: "Something went wrong",
NotificationType.ERROR
)
eventListener.onError(ErrorDetails(t?.message ?: "Code completion error"), t ?: Throwable())
channel.close(t)
}

View file

@ -63,7 +63,7 @@ class GrpcClientService(private val project: Project) : Disposable {
val prev = ctx.attach()
try {
codeCompletionStub
?.withDeadlineAfter(10, TimeUnit.SECONDS)
?.withDeadlineAfter(2, TimeUnit.SECONDS)
?.getCodeCompletion(grpcRequest, codeCompletionObserver)
} finally {
ctx.detach(prev)
@ -94,7 +94,7 @@ class GrpcClientService(private val project: Project) : Disposable {
val prev = ctx.attach()
try {
nextEditStub
?.withDeadlineAfter(10, TimeUnit.SECONDS)
?.withDeadlineAfter(2, TimeUnit.SECONDS)
?.nextEdit(request, nextEditStreamObserver)
} finally {
ctx.detach(prev)
@ -225,11 +225,11 @@ class GrpcClientService(private val project: Project) : Disposable {
.trustManager(CertificateManager.getInstance().trustManager)
.build()
)
.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000)
.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5_000)
.keepAliveTime(2, TimeUnit.MINUTES)
.keepAliveTimeout(20, TimeUnit.SECONDS)
.keepAliveWithoutCalls(false)
.idleTimeout(5, TimeUnit.MINUTES)
.keepAliveWithoutCalls(true)
.idleTimeout(30, TimeUnit.MINUTES)
.maxInboundMessageSize(32 * 1024 * 1024)
.build()

View file

@ -11,12 +11,11 @@ import ee.carlrobert.service.NextEditResponse
import io.grpc.Status
import io.grpc.StatusRuntimeException
import io.grpc.stub.StreamObserver
import kotlin.coroutines.cancellation.CancellationException
class NextEditStreamObserver(
private val editor: Editor,
private val addToQueue: Boolean = false,
private val onDispose: () -> Unit
private val onRefreshConnection: () -> Unit
) : StreamObserver<NextEditResponse> {
companion object {
@ -40,25 +39,11 @@ class NextEditStreamObserver(
}
override fun onError(ex: Throwable) {
if (ex is CancellationException ||
(ex is StatusRuntimeException && ex.status.code == Status.Code.CANCELLED)
) {
onCompleted()
return
if (ex is StatusRuntimeException && ex.status.code == Status.Code.UNAVAILABLE) {
onRefreshConnection()
}
try {
if (ex is StatusRuntimeException) {
if (ex.status.code == Status.Code.DEADLINE_EXCEEDED) {
return
}
} else {
logger.error("Something went wrong", ex)
}
} finally {
onCompleted()
onDispose()
}
onCompleted()
}
override fun onCompleted() {