Back to Gotraining

README

topics/go/concurrency/channels/README.md

latest5.1 KB
Original Source

Channels

Channels allow goroutines to communicate with each other through the use of signaling semantics. Channels accomplish this signaling through the use of sending/receiving data or by identifying state changes on individual channels. Don't architect software with the idea of channels being a queue, focus on signaling and the semantics that simplify the orchestration required.

Design Guidelines

Diagrams

Guarantee Of Delivery

The Guarantee Of Delivery is based on one question: “Do I need a guarantee that the signal sent by a particular goroutine has been received?”

Signaling With Or Without Data

When you are going to signal with data, there are three channel configuration options you can choose depending on the type of guarantee you need.

Signaling without data serves the main purpose of cancellation. It allows one goroutine to signal another goroutine to cancel what they are doing and move on. Cancellation can be implemented using both unbuffered and buffered channels.

State

The behavior of a channel is directly influenced by its current State. The state of a channel can be nil, open or closed.

The Behavior Of Channels - William Kennedy
Channel Communication
Share Memory By Communicating - Andrew Gerrand
The Nature Of Channels In Go - William Kennedy
A Retrospective on SEDA - Matt Welsh
Understanding Channels - Kavya Joshi

Buffer Bloat - 2011

  • Large buffers prevent timely notification of back pressure.
  • They defeat your ability to reduce back pressure in a timely matter.
  • They can increase latency not reduce it.
  • Use buffered channels to provide a way of maintaining continuity.
    • Don't use them just for performance.
    • Use them to handle well defined bursts of data.
    • Use them to deal with speed of light issues between handoffs.

Bufferbloat: Dark Buffers in the Internet
Buffer Bloat Videos

Code Review

Basic mechanics (Go Playground)
Tennis game (Go Playground)
Relay race (Go Playground)
Fan out pattern (Go Playground)
Monitor running time (Go Playground)

Advanced Code Review

Channel communication ordering (Go Playground)

Exercises

Exercise 1

Write a program where two goroutines pass an integer back and forth ten times. Display when each goroutine receives the integer. Increment the integer with each pass. Once the integer equals ten, terminate the program cleanly.

Template (Go Playground) | Answer (Go Playground)

Exercise 2

Write a program that uses a fan out pattern to generate 100 random numbers concurrently. Have each goroutine generate a single random number and return that number to the main goroutine over a buffered channel. Set the size of the buffer channel so no send ever blocks. Don't allocate more buffers than you need. Have the main goroutine display each random number it receives and then terminate the program.

Template (Go Playground) | Answer (Go Playground)

Exercise 3

Write a program that generates up to 100 random numbers concurrently. Do not send all 100 values so the number of sends/receives is unknown.

Template (Go Playground) | Answer (Go Playground)

Exercise 4

Write a program that generates up to 100 random numbers concurrently using a worker pool. Reject even values. Instruct the workers to shutdown with 100 odd numbers have been collected.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.