Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine when a core.async channel is closed from the writer's side?

I have a process that is basically part of a long polling implementation. In a nutshell, the client makes a request to the server, which then creates a channel and returns it to that client. The channel will contain the information that client asked for. Because this is intended to be used for long polling, the information that the client has requested can change. If the information changes, the server will write the update to the channel, and in theory, the client can grab the update and reflect that.

The problem I am facing is that there is a chance that I end up with tons of channels, which can cause unnecessary processing when an update occurs. I could potentially be updating channels where the client no longer cares.

My solution was that once the client stops caring about the information, it closes the channel, which will somehow notify the server, and prevent the server from doing additional processing and updates.

How can the server know if a channel is closed? In this scenario, the server is essentially a writer and does not read anything from the channel. Also, all the writing methods are wrapped in a go block, so it always returns a channel (I don't see any nils). Is there a way to do this, or is my approach completely off?

Thanks

like image 519
user1246334 Avatar asked Jul 23 '14 14:07

user1246334


People also ask

What are the objectives of core async?

The objectives of core.async are: To provide facilities for independent threads of activity, communicating via queue-like channels To support both real threads and shared use of thread pools (in any combination), as well as ClojureScript on JS engines

What is an async multi-consumer channel?

An async multi-producer multi-consumer channel, where each message can be received by only one of all existing consumers. Bounded channel with limited capacity. Unbounded channel with unlimited capacity. A channel has the Sender and Receiver side. Both sides are cloneable and can be shared among multiple threads.

What is the use of Channel<Coordinates>writer in while loop?

Again, the Channel<Coordinates>.Writer is used within a while loop. But this time, the WriteAsync method is called. The method will continue only after the coordinates have been written. When the while loop exits, a call to Complete is made, which signals that no more data will be written to the channel.

Where can I find channels library in NET Core?

This library is available in the System.Threading.Channels NuGet package (or included when targeting .NET Core 2.1+). Channels are an implementation of the producer/consumer conceptual programming model. In this programming model, producers asynchronously produce data, and consumers asynchronously consume that data.


2 Answers

As of earlier this year, there's a new feature called "putret" this change makes it so all put functions return false if the put fails due to the channel being closed. This allows for idioms like this:

(when (>! c val)
  (recur (next itms)))
like image 191
Timothy Baldridge Avatar answered Sep 21 '22 19:09

Timothy Baldridge


You can use clojure.core.async.impl.protocols/closed?. As many thing in clojure core.async is 99% finished:)

IMO "producer side closures only" view is rather misleading. Producer could be dead or unresponsive or you might want to re-start your system, or parts of it, without recurring various alts! obfuscating patterns with "stopping channels" etc.

like image 31
VitoshKa Avatar answered Sep 24 '22 19:09

VitoshKa