Channel Buffering in Crystal

# Channel Buffering

By default channels are *unbuffered*, meaning that they
will only accept sends (`channel.send`) if there is a
corresponding receive (`channel.receive`) ready to receive the
sent value. *Buffered channels* accept a limited
number of values without a corresponding receiver for
those values.

```crystal
require "fiber"

def main
  # Here we create a channel of strings buffering up to
  # 2 values.
  messages = Channel(String).new(2)

  # Because this channel is buffered, we can send these
  # values into the channel without a corresponding
  # concurrent receive.
  messages.send "buffered"
  messages.send "channel"

  # Later we can receive these two values as usual.
  puts messages.receive
  puts messages.receive
end

main

To run the program:

$ crystal channel-buffering.cr
buffered
channel

In Crystal, channels are implemented using the Channel class. We create a buffered channel by specifying the buffer size in the constructor. The send method is used to send values to the channel, and receive is used to receive values from the channel.

Crystal’s channels work similarly to those in other concurrent languages, allowing for synchronization and communication between fibers (Crystal’s lightweight threads). The buffered channel allows sending a limited number of values without blocking, which can be useful in certain concurrent programming scenarios.