Sidekick
The Sidekick design pattern enables us to delegate some tasks from our primary worker to a secondary worker.
So far, we’ve talked about using select solely as a receiver. However, it’s also possible to use select to send items to another channel. To illustrate, let’s consider an example.
First, we initialize batman
as an actor coroutine that can process 10 messages every second:
val batman = actor<String> {
for (c in channel) {
println("Batman is dealing with $c")
delay(100)
}
}
Next, we introduce robin
, another actor coroutine, albeit a slower one, capable of processing just four messages per second:
val robin = actor<String> {
for (c in channel) {
println("Robin is dealing with $c")
delay(250)
}
}
Here, we have a superhero and his sidekick represented as two actor coroutines. The superhero, being more adept, usually takes less time to handle villains...