channels/

directory
v0.0.0-...-8d8d9e6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2016 License: Apache-2.0

README

Channels

Channels are a reference type that provide a safe mechanism to share data between goroutines. Unbuffered channel provide a 100% guarantee of delivery that data has passed from one goroutine to the other. Buffered channels allow for data to pass through the channel without such guarantees. Unbuffered channels require both a sending and receiving goroutine to be ready at the same instant before any send or receive operation can complete. Buffered channels don't force goroutines to be ready at the same instant to perform sends and receives.

Notes

  • Use channels to orchestrate goroutines, not to synchronize access to shared state.
  • Unbuffered channels provide a 100% guarantee that data has been exchanged at some point in space and time.
  • Buffered channels provide a way of maintaining continuity. Don't use them just for performance.
  • Buffered channels if used incorrectly can increase latency not reduce it (Buffer Bloat).
  • Closed channels can provide a system wide mechanism for notifications.
  • A send on an unbuffered channel happens before the corresponding receive from that channel completes.
  • A receive from an unbuffered channel happens before the send on that channel completes.
  • The closing of a channel happens before a receive that returns a zero value because the channel is closed.

Diagrams

How an unbuffered channel works.

Ardan Labs

How a buffered channel works.

Ardan Labs

Channel Communication
http://blog.golang.org/share-memory-by-communicating
http://www.goinggo.net/2014/02/the-nature-of-channels-in-go.html

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

Unbuffered channels - Tennis game (Go Playground)
Unbuffered channels - Relay race (Go Playground)
Buffered channels - Retrieving results (Go Playground)
Timer channels and Select (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 every blocks. Don't allocate more buffers than you need. Have the main goroutine display each random number is receives and then terminate the program.

Template (Go Playground) | Answer (Go Playground)


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

Directories

Path Synopsis
advanced
example1
Sample program to show the order of channel communication for unbuffered, buffered and closing channels based on the specification.
Sample program to show the order of channel communication for unbuffered, buffered and closing channels based on the specification.
Sample program to show how to use an unbuffered channel to simulate a game of tennis between two goroutines.
Sample program to show how to use an unbuffered channel to simulate a game of tennis between two goroutines.
Sample program to show how to use an unbuffered channel to simulate a relay race between four goroutines.
Sample program to show how to use an unbuffered channel to simulate a relay race between four goroutines.
This sample program demonstrates how to use a buffered channel to receive results from other goroutines in a guaranteed way.
This sample program demonstrates how to use a buffered channel to receive results from other goroutines in a guaranteed way.
This sample program demonstrates how to use a channel to monitor the amount of time the program is running and terminate the program if it runs too long.
This sample program demonstrates how to use a channel to monitor the amount of time the program is running and terminate the program if it runs too long.
exercises
exercise1
Write a program where two goroutines pass an integer back and forth ten times.
Write a program where two goroutines pass an integer back and forth ten times.
exercise2
Write a program that uses a fan out pattern to generate 100 random numbers concurrently.
Write a program that uses a fan out pattern to generate 100 random numbers concurrently.
template1
Write a program where two goroutines pass an integer back and forth ten times.
Write a program where two goroutines pass an integer back and forth ten times.
template2
Write a program that uses a fan out pattern to generate 100 random numbers concurrently.
Write a program that uses a fan out pattern to generate 100 random numbers concurrently.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL