Querying the number of elements in a buffered core.async/chan

204 views Asked by At

Having a buffered chan, say

(def c (clojure.core.async/chan 100))

is it possible to query how many elements are currently inside the chan?

1

There are 1 answers

0
guilespi On BEST ANSWER

The instantiated channel is from ManyToManyChannel type, you can access the internal buffer for the channel using the buf property.

(.buf c)
 => #<FixedBuffer clojure.core.async.impl.buffers.FixedBuffer@3d67452c>

That buffer implements clojure.lang.Counted so it's countable.

(count (.buf c))
=> 0