Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amazonq): use Dispatchers.IO for @workspace requests #5374

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Amazon Q: Attempt to reduce thread pool contention caused by `@workspace` makinng large number of requests"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
Expand All @@ -28,7 +30,7 @@ class ProjectContextController(private val project: Project, private val cs: Cor
// TODO: Ideally we should inject dependencies via constructor for easier testing, refer to how [TelemetryService] inject publisher and batcher
private val encoderServer: EncoderServer = EncoderServer(project)
private val projectContextProvider: ProjectContextProvider = ProjectContextProvider(project, encoderServer, cs)
val initJob: Job = cs.launch {
val initJob: Job = cs.launch(Dispatchers.IO) {
encoderServer.downloadArtifactsAndStartServer()
}

Expand All @@ -51,16 +53,16 @@ class ProjectContextController(private val project: Project, private val cs: Cor

fun getProjectContextIndexComplete() = projectContextProvider.isIndexComplete.get()

suspend fun queryChat(prompt: String, timeout: Long?): List<RelevantDocument> {
suspend fun queryChat(prompt: String, timeout: Long?): List<RelevantDocument> = withContext(Dispatchers.IO) {
try {
return projectContextProvider.query(prompt, timeout)
projectContextProvider.query(prompt, timeout)
} catch (e: Exception) {
logger.warn { "error while querying for project context $e.message" }
return emptyList()
emptyList()
}
}

suspend fun queryInline(query: String, filePath: String): List<InlineBm25Chunk> =
suspend fun queryInline(query: String, filePath: String): List<InlineBm25Chunk> = withContext(Dispatchers.IO) {
try {
projectContextProvider.queryInline(query, filePath, InlineContextTarget.CODEMAP)
} catch (e: Exception) {
Expand All @@ -71,6 +73,7 @@ class ProjectContextController(private val project: Project, private val cs: Cor
logger.warn { logStr }
emptyList()
}
}

@RequiresBackgroundThread
fun updateIndex(filePaths: List<String>, mode: IndexUpdateMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileVisitor
import com.intellij.openapi.vfs.isFile
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -44,7 +45,7 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
private val mapper = jacksonObjectMapper()

init {
cs.launch {
cs.launch(Dispatchers.IO) {
if (ApplicationManager.getApplication().isUnitTestMode) {
return@launch
}
Expand Down
Loading