feat: separate db reads and writes to separate dispatchers

This commit is contained in:
Harsh Shandilya 2025-03-11 17:32:56 +05:30
parent f818da6f64
commit 976c9dd064
5 changed files with 38 additions and 20 deletions

View file

@ -21,8 +21,11 @@ interface DispatcherProvider {
fun io(): CoroutineDispatcher = Dispatchers.IO
fun database(): CoroutineDispatcher =
Dispatchers.IO.limitedParallelism(1, name = "DatabaseDispatcher")
fun databaseRead(): CoroutineDispatcher =
Dispatchers.IO.limitedParallelism(4, name = "DatabaseRead")
fun databaseWrite(): CoroutineDispatcher =
Dispatchers.IO.limitedParallelism(1, name = "DatabaseWrite")
}
/** Concrete type for [DispatcherProvider] with all the defaults from the class. */

View file

@ -16,7 +16,9 @@ import dev.msfjarvis.claw.core.coroutines.DispatcherProvider
import javax.inject.Qualifier
import kotlinx.coroutines.CoroutineDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseReadDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseWriteDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class MainDispatcher
@ -36,9 +38,16 @@ interface CoroutineDispatcherModule {
return dispatcherProvider.io()
}
@[Provides DatabaseDispatcher]
fun provideDatabaseDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
return dispatcherProvider.database()
@[Provides DatabaseReadDispatcher]
fun provideDatabaseReadDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
return dispatcherProvider.databaseRead()
}
@[Provides DatabaseWriteDispatcher]
fun provideDatabaseWriteDispatcher(
dispatcherProvider: DispatcherProvider
): CoroutineDispatcher {
return dispatcherProvider.databaseWrite()
}
@[Provides MainDispatcher]