question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to window the fixed-size buffered items in Channel?

See original GitHub issue

When 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:open
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
diouskcommented, Mar 30, 2020

@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.

0reactions
elizarovcommented, Mar 26, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found