How to window the fixed-size buffered items in Channel?
See original GitHub issueWhen using Channel with capacity > 0, like Channel(3) I want to keep the buffered item inside the channel as fresh as possible, The use-case is like following:
fun bufferChannel() {
val channel = Channel<Int>(3)
scope.launch {
launch {
(1..10).forEach {
println("send $it")
channel.send(it)
}
channel.close()
println("done send")
}
launch {
delay(2000)
println("recv start consume")
channel.consumeEach {
println("recv $it")
}
println("done recv")
}
}
}
I’m looking for a way to let the receiver receives the latest items, e.g.
recv 8
recv 9
recv 10
Is there a way to drop the oldest item in the channel when offer new items to the channel?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Buffered Channels and Worker Pools in Go - golangbot.com
Buffered channels can be created by passing an additional capacity parameter to the make function which specifies the size of the buffer.
Read more >Buffered Channel in Golang - GeeksforGeeks
Buffered channels can be created by passing an additional capacity parameter to the make( ) function which specifies the size of the buffer....
Read more >go - What is channel buffer size? - Stack Overflow
The buffer size is the number of elements that can be sent to the channel without the send blocking. By default, a channel...
Read more >Understanding Buffer Misses and Failures - Cisco
This document discusses buffer misses and failures on the Routing ... Note: The interface processor must ask for a buffer of a certain...
Read more >Buffered Channels - A Tour of Go
Buffered Channels. Channels can be buffered. Provide the buffer length as the second argument to make to initialize a buffered channel: ch :=...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@elizarov It’s designed for the chat room of live streaming. Sometimes there’s a large number of messages sent from websocket, and we’d like the viewers to see the new messages as fresh as possible. Due to the back pressure, we need to control the message buffer size and drop some messages and here we choose to drop the oldest ones.
Thanks. I know that you can do it in Rx and we are also looking at whether we need to add this kind of a feature to Kotlin flow. For that, we need to know the details. My question if where do you find the need to use it. What is the domain you are working with and what kind of events are you working with?