# How to get the capacity of one channel?

**URL:** <https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135>\
**Category:** Help & Support\
**Created:** [November 26, 2022, 6:04am UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135 "2022-11-26T06:04:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 26, 2022, 6:04am UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135/1 "2022-11-26T06:04:02Z")

</div>

Following is sample code for golang.

```nohighlight
package main

import "fmt"

func main() {
	ch0 := make(chan string)
	fmt.Println(cap(ch0))
	ch3 := make(chan string, 3)
	fmt.Println(cap(ch3))
}

// =>
// 0
// 3

```

We are not the public API to get the @capacity ivar, right?

---

<div class="post-metadata">

**Author:** ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)\
**Post date:** [November 26, 2022, 12:16pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135/2 "2022-11-26T12:16:54Z")

</div>

Crystal `Channel` doesn’t expose such API. Generally capacity applies to `buffered` channels only, where it represents the number of elements channel can hold, and it shouldn’t be confused with `size`. I don’t know what would be the use case where you would require such information, but you can get this info by **monkey patching** the `Channel` class.

**Note:** Monkey patching stdlib isn’t a good practice and is something which should be applied with extreme caution.

```crystal
class Channel(T)
  def capacity : Int32
    (queue = @queue) ? queue.@capacity : 0
  end
end

```

And now you can get the capacity

```crystal
ch0 = Channel(Nil).new
puts ch0.capacity # => 0

ch3 = Channel(Nil).new(3)
puts ch3.capacity # => 3

```

HIH

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 26, 2022, 4:53pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135/3 "2022-11-26T16:53:49Z")

</div>

> [@naqvis](#):
>
> I don’t know what would be the use case where you would require such information

No really, just because i found golang exposed this API, so, it probably useful for users from go, should we add this feature?

---

<div class="post-metadata">

**Author:** ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)\
**Post date:** [November 26, 2022, 5:13pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135/4 "2022-11-26T17:13:02Z")

</div>

Yeah, sure it will be good to raise a feature request and have it open for discussion, and see if community and/or core team agrees to extend `Channel` class to add some api which `Returns Maximum capacity of the Channel`.

Reference could be GoLang API and [Concurrent Ruby](https://ruby-concurrency.github.io/concurrent-ruby/1.1.5/Concurrent/Promises/Channel.html#capacity-instance_method)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [November 26, 2022, 6:55pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135/5 "2022-11-26T18:55:45Z")

</div>

Just note that without an actual use case the chances of adding this will be very small.

---

<div class="post-metadata">

**Author:** ![drhuffman12](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drhuffman12/32/715_2.png) [@drhuffman12](https://forum.crystal-lang.org/u/drhuffman12)\
**Post date:** [November 27, 2022, 1:46am UTC](https://forum.crystal-lang.org/t/how-to-get-the-capacity-of-one-channel/5135/6 "2022-11-27T01:46:03Z")

</div>

I could imagine 1 use case would be gathering/monitoring channel metrics, and maybe automating channel capacity or spawning more process w/ more channels, depending on demand.
