I need a suggestion regarding the architecture of the telegram bot API wrapper. So basically I need a way to handle updates efficiently. I have checked how kotlin-telgram-bot does it. And it has a Courotine dispatcher on a single thread Executors.newSingleThreadExecutor().asCoroutineDispatcher()
. Handling its update as follows:
private val scope: CoroutineScope = CoroutineScope(coroutineDispatcher)
@Volatile private var job: Job? = null
internal fun startCheckingUpdates() {
job?.cancel()
job = scope.launch { checkQueueUpdates() }
}
private suspend fun checkQueueUpdates() {
while (true) {
when (val item = updatesChannel.receive()) {
is Update -> handleUpdate(item)
is TelegramError -> handleError(item)
else -> Unit
}
yield()
}
}
- Is it an effective way to handle updates?
- Can I use a coroutine dispatcher that works on several threads?
So if you have a recommendation for the best design of update handling I would appreciate it.