From d70c9ce6455c4e307ac3c56e12ff008d2015813d Mon Sep 17 00:00:00 2001 From: Janet Cobb Date: Wed, 3 Jan 2024 00:22:41 -0500 Subject: [PATCH] Use I/O dispatcher for global data I/O in DefaultArchiver --- .../randomcat/agorabot/util/DefaultArchiver.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/org/randomcat/agorabot/util/DefaultArchiver.kt b/src/main/kotlin/org/randomcat/agorabot/util/DefaultArchiver.kt index 121f21b8..876fd2f7 100644 --- a/src/main/kotlin/org/randomcat/agorabot/util/DefaultArchiver.kt +++ b/src/main/kotlin/org/randomcat/agorabot/util/DefaultArchiver.kt @@ -901,12 +901,16 @@ private suspend fun receiveGlobalData( metadataPath: Path, globalDataDir: Path, ) { - coroutineScope { - launch { - usePathGenerator(metadataPath) { metadataGenerator -> - usePathGenerator(globalDataDir.resolve("users.json")) { usersGenerator -> - usePathGenerator(globalDataDir.resolve("roles.json")) { rolesGenerator -> - usePathGenerator(globalDataDir.resolve("channels.json")) { channelsGenerator -> + // Here, do all the opening and closing on the I/O dispatcher, so blocking reads/writes don't block on the default + // dispatcher, but then return to the default dispatcher for the actual work, which should not be doing much + // blocking I/O (and, in any case, receiveGlobalDataWithGenerators can switch dispatchers itself if it needs to). + + withContext(Dispatchers.IO) { + usePathGenerator(metadataPath) { metadataGenerator -> + usePathGenerator(globalDataDir.resolve("users.json")) { usersGenerator -> + usePathGenerator(globalDataDir.resolve("roles.json")) { rolesGenerator -> + usePathGenerator(globalDataDir.resolve("channels.json")) { channelsGenerator -> + withContext(Dispatchers.Default) { receiveGlobalDataWithGenerators( dataChannel = dataChannel, guild = guild,