-
Notifications
You must be signed in to change notification settings - Fork 22
End to end communication types in channels
Devrath edited this page Jan 18, 2024
·
10 revisions
Contents |
---|
1-N --> Communicaton - Fan-Out approach |
N-1 --> Communicaton - Fan-In approach |
N-N --> Communication - Load balancers |
- Here basically there is one producer and multiple consumers.
- Note in the output when the consumer is consuming an emission or if it's suspended, another consumer receives the emission.
- Also you can observe all the consumers share the emissions meaning an emission received by one consumer is not received in another emission.
- We call it
Fan-Out
approach
![Screenshot 2024-01-18 at 3 19 58β―PM](https://private-user-images.githubusercontent.com/1456191/297696757-ef52dc67-c1c7-4a05-87ad-c8c1944531f9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ2MTY1NDcsIm5iZiI6MTcyNDYxNjI0NywicGF0aCI6Ii8xNDU2MTkxLzI5NzY5Njc1Ny1lZjUyZGM2Ny1jMWM3LTRhMDUtODdhZC1jOGMxOTQ0NTMxZjkucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMjAwNDA3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZTI5NjhjZTYxMDgyYjA3MmQ3ZDZjZjNmYWRkYWU5NzM1OTYyZGNmOWFlYzNkYmIxMzkzZmViNjU3ODQyMDE0MCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.0f0LwzFjte_yiFM_ZRtUcq1bxpdCipa9Uk0CeJnRug4)
output
Consumer-1 <-> (1-n) : ----> 1
Consumer-2 <-> (1-n) : ----> 3
Consumer-3 <-> (1-n) : ----> 5
Consumer-2 <-> (1-n) : ----> 7
Consumer-3 <-> (1-n) : ----> 9
Code
private var one_to_n_channel : ReceiveChannel<Int> = Channel()
fun usingOneToMany() {
viewModelScope.launch {
// Producer -1
one_to_n_channel = produce(capacity = UNLIMITED){
repeat(10){ currentValue -> send(currentValue) }
}
}
viewModelScope.launch {
// Coroutine - 1
viewModelScope.launch {
// Consumer - 1
one_to_n_channel.consumeEach {
println("Consumer-1 <-> (1-n) : ----> $it")
delay(300)
}
}
// Coroutine - 2
viewModelScope.launch {
// Consumer - 2
one_to_n_channel.consumeEach {
println("Consumer-2 <-> (1-n) : ----> $it")
delay(100)
}
}
// Coroutine - 3
viewModelScope.launch {
// Consumer - 3
one_to_n_channel.consumeEach {
println("Consumer-3 <-> (1-n) : ----> $it")
delay(150)
}
}
}
}
- Here there are multiple producers and just one consumer.
- They use one channel for their mode of communication.
- We call it
Fan-In
approach
![Screenshot 2024-01-18 at 3 17 27β―PM](https://private-user-images.githubusercontent.com/1456191/297696430-72bbc275-c8f4-44bb-879d-65b9827bf884.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ2MTY1NDcsIm5iZiI6MTcyNDYxNjI0NywicGF0aCI6Ii8xNDU2MTkxLzI5NzY5NjQzMC03MmJiYzI3NS1jOGY0LTQ0YmItODc5ZC02NWI5ODI3YmY4ODQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMjAwNDA3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MzhjZGFjMjFhYjNiZjkxNzllNGUwZmQ4MDk1MGViZjRkYzBhZjk2ZmU3OGMxODNjNzI3ZTc1NWYzZWE1MTVhNiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.4qfDFvmgOk-6lmqDOMe514bBVb_O09T5ZQC66iMDjVo)
Output
Consumer <-> (N-1) : ----> FromProducer-1 0
Consumer <-> (N-1) : ----> FromProducer-2 0
Consumer <-> (N-1) : ----> FromProducer-2 1
Consumer <-> (N-1) : ----> FromProducer-1 1
Consumer <-> (N-1) : ----> FromProducer-2 2
Consumer <-> (N-1) : ----> FromProducer-1 2
Code
private var n_to_one_channel : Channel<String> = Channel()
fun usingManyToOne() {
viewModelScope.launch {
// Producer -1
val firstProducer = viewModelScope.async {
repeat(3){ currentValue ->
n_to_one_channel.send("FromProducer-1 $currentValue")
delay(150)
}
}
// Producer -2
val secondProducer = viewModelScope.async {
repeat(3){ currentValue ->
n_to_one_channel.send("FromProducer-2 $currentValue")
delay(100)
}
}
awaitAll(secondProducer,firstProducer)
}
// Consumer-1
viewModelScope.launch {
n_to_one_channel.consumeEach {
println("Consumer <-> (N-1) : ----> $it")
}
}
}
![Screenshot 2024-01-18 at 3 26 12β―PM](https://private-user-images.githubusercontent.com/1456191/297698586-1825e9fd-fa1d-4b12-9a70-4c43300cf958.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ2MTY1NDcsIm5iZiI6MTcyNDYxNjI0NywicGF0aCI6Ii8xNDU2MTkxLzI5NzY5ODU4Ni0xODI1ZTlmZC1mYTFkLTRiMTItOWE3MC00YzQzMzAwY2Y5NTgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMjAwNDA3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9Y2M2NTljMDUzM2UxYTJmNWM3Yzg0YWQ3YTA5ODhhNTBlMzUwMmRmYWU3YjU4YTg4YmU2Mzc3MTM1ODMzNTU5OSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.m9_QVddKWgHhxK6GAuu5VAT8D90KP7j_tIZGiQdCc7w)