Skip to content

Commit 36c24b4

Browse files
authored
Ban use of Dispatchers (#2688)
1 parent fb9ad04 commit 36c24b4

File tree

12 files changed

+59
-31
lines changed

12 files changed

+59
-31
lines changed

core/src/software/aws/toolkits/core/credentials/sso/SsoCredentialProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
@file:Suppress("BannedImports")
5+
46
package software.aws.toolkits.core.credentials.sso
57

68
import kotlinx.coroutines.Dispatchers

detekt-rules/src/main/kotlin/software/aws/toolkits/gradle/detekt/rules/BannedImportsRule.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class BannedImportsRule : Rule() {
4848
)
4949
)
5050
}
51+
52+
if (element.importedFqName?.asString()?.contains("kotlinx.coroutines.Dispatchers") == true) {
53+
report(
54+
CodeSmell(
55+
issue,
56+
Entity.from(element),
57+
message = "Use contexts from CoroutineUtils.kt instead of Dispatchers"
58+
)
59+
)
60+
}
5161
}
5262
}
5363
}

detekt-rules/src/main/kotlin/software/aws/toolkits/gradle/detekt/rules/BannedPatternRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class BannedPatternRule(private val patterns: List<BannedPattern>) : Rule() {
1616

1717
override fun visitKtFile(file: KtFile) {
1818
var offset = 0
19-
file.text.split("\n").forEachIndexed { line, text ->
19+
file.text.split("\n").forEachIndexed { _, text ->
2020
patterns.forEach { pattern ->
2121
val match = pattern.regex.find(text) ?: return@forEach
2222
report(

detekt-rules/src/test/kotlin/BannedImportsRuleTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,18 @@ class BannedImportsRuleTest {
4242
fun `Importing Assert assertThat succeeds`() {
4343
assertThat(rule.lint("import org.assertj.core.api.Assertions.assertThat")).isEmpty()
4444
}
45+
46+
@Test
47+
fun `Importing Dispatchers fails`() {
48+
assertThat(rule.lint("import kotlinx.coroutines.Dispatchers"))
49+
.singleElement()
50+
.matches { it.id == "BannedImports" && it.message == "Use contexts from CoroutineUtils.kt instead of Dispatchers" }
51+
}
52+
53+
@Test
54+
fun `Importing Dispatchers statically fails`() {
55+
assertThat(rule.lint("import kotlinx.coroutines.Dispatchers.IO"))
56+
.singleElement()
57+
.matches { it.id == "BannedImports" && it.message == "Use contexts from CoroutineUtils.kt instead of Dispatchers" }
58+
}
4559
}

jetbrains-core/src/software/aws/toolkits/jetbrains/services/cloudwatch/logs/CloudWatchActor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.intellij.ui.TableUtil
99
import com.intellij.ui.table.TableView
1010
import com.intellij.util.ExceptionUtil
1111
import kotlinx.coroutines.CoroutineExceptionHandler
12-
import kotlinx.coroutines.Dispatchers
1312
import kotlinx.coroutines.Job
1413
import kotlinx.coroutines.channels.Channel
1514
import kotlinx.coroutines.launch
@@ -30,6 +29,7 @@ import software.aws.toolkits.jetbrains.services.cloudwatch.logs.insights.LogResu
3029
import software.aws.toolkits.jetbrains.services.cloudwatch.logs.insights.identifier
3130
import software.aws.toolkits.jetbrains.services.cloudwatch.logs.insights.toLogResult
3231
import software.aws.toolkits.jetbrains.utils.ApplicationThreadPoolScope
32+
import software.aws.toolkits.jetbrains.utils.getCoroutineBgContext
3333
import software.aws.toolkits.jetbrains.utils.getCoroutineUiContext
3434
import software.aws.toolkits.jetbrains.utils.notifyError
3535
import software.aws.toolkits.resources.message
@@ -467,7 +467,7 @@ class InsightsQueryResultsActor(
467467
}
468468

469469
// run on a separate context so we don't lock up the message listener
470-
private fun startLoading() = coroutineScope.launch(Dispatchers.Default) {
470+
private fun startLoading() = coroutineScope.launch(getCoroutineBgContext()) {
471471
tableLoading()
472472
val loadedQueryResults = mutableSetOf<String>()
473473
var response: GetQueryResultsResponse

jetbrains-core/src/software/aws/toolkits/jetbrains/services/cloudwatch/logs/insights/SaveQueryDialog.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.intellij.openapi.project.Project
77
import com.intellij.openapi.ui.DialogWrapper
88
import com.intellij.openapi.ui.ValidationInfo
99
import kotlinx.coroutines.CoroutineScope
10-
import kotlinx.coroutines.Dispatchers
1110
import kotlinx.coroutines.launch
1211
import kotlinx.coroutines.withContext
1312
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient
@@ -18,6 +17,7 @@ import software.aws.toolkits.jetbrains.core.AwsResourceCache
1817
import software.aws.toolkits.jetbrains.core.credentials.ConnectionSettings
1918
import software.aws.toolkits.jetbrains.services.cloudwatch.logs.resources.CloudWatchResources
2019
import software.aws.toolkits.jetbrains.utils.ApplicationThreadPoolScope
20+
import software.aws.toolkits.jetbrains.utils.getCoroutineBgContext
2121
import software.aws.toolkits.jetbrains.utils.notifyError
2222
import software.aws.toolkits.jetbrains.utils.notifyInfo
2323
import software.aws.toolkits.resources.message
@@ -63,12 +63,12 @@ class SaveQueryDialog(
6363
super.doCancelAction()
6464
}
6565

66-
override fun createCenterPanel(): JComponent? = view.saveQueryPanel
66+
override fun createCenterPanel(): JComponent = view.saveQueryPanel
6767
override fun doValidate(): ValidationInfo? = validateQueryName(view)
6868
override fun getOKAction(): Action = action
6969

7070
private suspend fun getExistingQueryId(queryName: String): String? {
71-
val definitions = withContext(Dispatchers.Default) {
71+
val definitions = withContext(getCoroutineBgContext()) {
7272
resourceCache.getResourceNow(
7373
CloudWatchResources.DESCRIBE_QUERY_DEFINITIONS,
7474
region = connectionSettings.region,

jetbrains-core/src/software/aws/toolkits/jetbrains/services/ecr/actions/PullFromRepositoryAction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.intellij.openapi.project.Project
1212
import com.intellij.openapi.ui.DialogWrapper
1313
import com.intellij.ui.SimpleListCellRenderer
1414
import com.intellij.ui.layout.panel
15-
import kotlinx.coroutines.Dispatchers
1615
import kotlinx.coroutines.launch
1716
import kotlinx.coroutines.withContext
1817
import software.amazon.awssdk.services.ecr.EcrClient
@@ -26,6 +25,7 @@ import software.aws.toolkits.jetbrains.services.ecr.resources.EcrResources
2625
import software.aws.toolkits.jetbrains.services.ecr.resources.Repository
2726
import software.aws.toolkits.jetbrains.ui.ResourceSelector
2827
import software.aws.toolkits.jetbrains.utils.ApplicationThreadPoolScope
28+
import software.aws.toolkits.jetbrains.utils.getCoroutineBgContext
2929
import software.aws.toolkits.resources.message
3030

3131
class PullFromRepositoryAction : EcrDockerAction() {
@@ -43,7 +43,7 @@ class PullFromRepositoryAction : EcrDockerAction() {
4343
val client: EcrClient = project.awsClient()
4444
coroutineScope.launch {
4545
val runtime = dockerServerRuntime.await()
46-
val authData = withContext(Dispatchers.IO) {
46+
val authData = withContext(getCoroutineBgContext()) {
4747
client.authorizationToken.authorizationData().first()
4848
}
4949
PullFromEcrTask(project, authData.getDockerLogin(), repo, image, runtime).queue()

jetbrains-core/src/software/aws/toolkits/jetbrains/services/ecr/actions/PushToRepositoryAction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import com.intellij.ui.layout.panel
3535
import com.intellij.ui.layout.selected
3636
import com.intellij.util.text.nullize
3737
import kotlinx.coroutines.Deferred
38-
import kotlinx.coroutines.Dispatchers
3938
import kotlinx.coroutines.launch
4039
import kotlinx.coroutines.withContext
4140
import software.amazon.awssdk.core.exception.SdkException
@@ -55,6 +54,7 @@ import software.aws.toolkits.jetbrains.services.ecr.resources.Repository
5554
import software.aws.toolkits.jetbrains.services.ecr.toLocalImageList
5655
import software.aws.toolkits.jetbrains.ui.ResourceSelector
5756
import software.aws.toolkits.jetbrains.utils.ApplicationThreadPoolScope
57+
import software.aws.toolkits.jetbrains.utils.getCoroutineBgContext
5858
import software.aws.toolkits.jetbrains.utils.notifyError
5959
import software.aws.toolkits.jetbrains.utils.ui.installOnParent
6060
import software.aws.toolkits.jetbrains.utils.ui.selected
@@ -84,7 +84,7 @@ class PushToRepositoryAction : EcrDockerAction() {
8484
val pushRequest = dialog.getPushRequest()
8585
var result = Result.Failed
8686
try {
87-
val authData = withContext(Dispatchers.IO) {
87+
val authData = withContext(getCoroutineBgContext()) {
8888
client.authorizationToken.authorizationData().first()
8989
}
9090

jetbrains-core/src/software/aws/toolkits/jetbrains/services/ecs/resources/SessionManagerPluginInstallationVerification.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import com.intellij.execution.configurations.GeneralCommandLine
77
import com.intellij.execution.process.CapturingProcessHandler
88
import com.intellij.openapi.application.runInEdt
99
import com.intellij.openapi.project.Project
10-
import kotlinx.coroutines.Dispatchers
1110
import kotlinx.coroutines.runBlocking
1211
import software.aws.toolkits.jetbrains.services.ecs.exec.SessionManagerPluginWarning
12+
import software.aws.toolkits.jetbrains.utils.getCoroutineBgContext
1313

1414
object SessionManagerPluginInstallationVerification {
15-
private fun checkInstallation(): Boolean = runBlocking(Dispatchers.IO) {
15+
private fun checkInstallation(): Boolean = runBlocking(getCoroutineBgContext()) {
1616
try {
1717
val process = CapturingProcessHandler(GeneralCommandLine("session-manager-plugin")).runProcess()
1818
process.exitCode == 0

jetbrains-core/src/software/aws/toolkits/jetbrains/services/s3/editor/S3VirtualBucket.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.intellij.openapi.vfs.VirtualFile
99
import com.intellij.openapi.vfs.VirtualFileManager
1010
import com.intellij.testFramework.LightVirtualFile
1111
import kotlinx.coroutines.CoroutineScope
12-
import kotlinx.coroutines.Dispatchers
1312
import kotlinx.coroutines.future.await
1413
import kotlinx.coroutines.runBlocking
1514
import kotlinx.coroutines.withContext
@@ -24,6 +23,7 @@ import software.aws.toolkits.jetbrains.services.s3.download
2423
import software.aws.toolkits.jetbrains.services.s3.resources.S3Resources
2524
import software.aws.toolkits.jetbrains.services.s3.upload
2625
import software.aws.toolkits.jetbrains.utils.ApplicationThreadPoolScope
26+
import software.aws.toolkits.jetbrains.utils.getCoroutineBgContext
2727
import software.aws.toolkits.jetbrains.utils.getCoroutineUiContext
2828
import software.aws.toolkits.jetbrains.utils.notifyError
2929
import software.aws.toolkits.resources.message
@@ -60,45 +60,47 @@ class S3VirtualBucket(val s3Bucket: Bucket, prefix: String, val client: S3Client
6060
override fun hashCode(): Int = s3Bucket.name().hashCode() + prefix.hashCode()
6161

6262
suspend fun newFolder(name: String) {
63-
withContext(Dispatchers.IO) {
63+
withContext(getCoroutineBgContext()) {
6464
client.putObject({ it.bucket(s3Bucket.name()).key(name.trimEnd('/') + "/") }, RequestBody.empty())
6565
}
6666
}
6767

68-
suspend fun listObjects(prefix: String, continuationToken: String?): ListObjectsV2Response = withContext(Dispatchers.IO) {
69-
client.listObjectsV2 {
70-
it.bucket(s3Bucket.name()).delimiter("/").prefix(prefix).maxKeys(MAX_ITEMS_TO_LOAD).continuationToken(continuationToken)
68+
suspend fun listObjects(prefix: String, continuationToken: String?): ListObjectsV2Response =
69+
withContext(getCoroutineBgContext()) {
70+
client.listObjectsV2 {
71+
it.bucket(s3Bucket.name()).delimiter("/").prefix(prefix).maxKeys(MAX_ITEMS_TO_LOAD).continuationToken(continuationToken)
72+
}
7173
}
72-
}
7374

74-
suspend fun listObjectVersions(key: String, keyMarker: String?, versionIdMarker: String?): ListObjectVersionsResponse? = withContext(Dispatchers.IO) {
75-
client.listObjectVersions {
76-
it.bucket(s3Bucket.name()).prefix(key).delimiter("/").maxKeys(MAX_ITEMS_TO_LOAD).keyMarker(keyMarker).versionIdMarker(versionIdMarker)
75+
suspend fun listObjectVersions(key: String, keyMarker: String?, versionIdMarker: String?): ListObjectVersionsResponse? =
76+
withContext(getCoroutineBgContext()) {
77+
client.listObjectVersions {
78+
it.bucket(s3Bucket.name()).prefix(key).delimiter("/").maxKeys(MAX_ITEMS_TO_LOAD).keyMarker(keyMarker).versionIdMarker(versionIdMarker)
79+
}
7780
}
78-
}
7981

8082
suspend fun deleteObjects(keys: List<String>) {
81-
withContext(Dispatchers.IO) {
83+
withContext(getCoroutineBgContext()) {
8284
val keysToDelete = keys.map { ObjectIdentifier.builder().key(it).build() }
8385
client.deleteObjects { it.bucket(s3Bucket.name()).delete { del -> del.objects(keysToDelete) } }
8486
}
8587
}
8688

8789
suspend fun renameObject(fromKey: String, toKey: String) {
88-
withContext(Dispatchers.IO) {
90+
withContext(getCoroutineBgContext()) {
8991
client.copyObject { it.copySource("${s3Bucket.name()}/$fromKey").destinationBucket(s3Bucket.name()).destinationKey(toKey) }
9092
client.deleteObject { it.bucket(s3Bucket.name()).key(fromKey) }
9193
}
9294
}
9395

9496
suspend fun upload(project: Project, source: InputStream, length: Long, key: String) {
95-
withContext(Dispatchers.IO) {
97+
withContext(getCoroutineBgContext()) {
9698
client.upload(project, source, length, s3Bucket.name(), key).await()
9799
}
98100
}
99101

100102
suspend fun download(project: Project, key: String, versionId: String? = null, output: OutputStream) {
101-
withContext(Dispatchers.IO) {
103+
withContext(getCoroutineBgContext()) {
102104
client.download(project, s3Bucket.name(), key, versionId, output).await()
103105
}
104106
}

0 commit comments

Comments
 (0)