channels/

directory
v0.0.0-...-79b2278 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT

README

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

Channel Design

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.

Language Mechanics:

  • Use channels to orchestrate and coordinate goroutines.
    • Focus on the signaling semantics and not the sharing of data.
    • Signaling with data or without data.
    • Question their use for synchronizing access to shared state.
      • There are cases where channels can be simpler for this but initially question.
  • Unbuffered channels:
    • Receive happens before the Send.
    • Benefit: 100% guarantee the signal being sent has been received.
    • Cost: Unknown latency on when the signal will be received.
  • Buffered channels:
    • Send happens before the Receive.
    • Benefit: Reduce blocking latency between signaling.
    • Cost: No guarantee when the signal being sent has been received.
      • The larger the buffer, the less guarantee.
      • Buffer of 1 can give you one delayed send of guarantee.
  • Closing channels:
    • Close happens before the Receive. (like Buffered)
    • Signaling without data.
    • Perfect for signaling cancellations and deadlines.
  • NIL channels:
    • Send and Receive block.
    • Turn off signaling
    • Perfect for rate limiting or short-term stoppages.

Design Philosophy:

Depending on the problem you are solving, you may require different channel semantics. Depending on the semantics you need, different architectural choices must be taken.

  • If any given Send on a channel CAN cause the sending goroutine to block:
    • Be careful with Buffered channels larger than 1.
      • Buffers larger than 1 must have reason/measurements.
    • Must know what happens when the sending goroutine blocks.
  • If any given Send on a channel WON'T cause the sending goroutine to block:
    • You have the exact number of buffers for each send.
      • Fan Out pattern
    • You have the buffer measured for max capacity.
      • Drop pattern
  • Less is more with buffers.
    • Don’t think about performance when thinking about buffers.
    • Buffers can help to reduce blocking latency between signaling.
      • Reducing blocking latency towards zero does not necessarily mean better throughput.
      • If a buffer of one is giving you good enough throughput then keep it.
      • Question buffers that are larger than one and measure for size.
      • Find the smallest buffer possible that provides good enough throughput.

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?”

Ardan Labs

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.

Ardan Labs

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.

Ardan Labs

State

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

Ardan Labs

Code Review

Advanced Code Review

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.

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.

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.

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.


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.
This sample program demonstrates the basic channel mechanics for goroutine signaling.
This sample program demonstrates the basic channel mechanics for goroutine signaling.
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.
Parallel link checker.
Parallel link checker.
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.
exercise3
Write a program that uses goroutines to generate up to 100 random numbers.
Write a program that uses goroutines to generate up to 100 random numbers.
exercise4
Write a program that creates a fixed set of workers to generate random numbers.
Write a program that creates a fixed set of workers to generate random numbers.
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.
template3
Write a program that uses goroutines to generate up to 100 random numbers.
Write a program that uses goroutines to generate up to 100 random numbers.
template4
Write a program that creates a fixed set of workers to generate random numbers.
Write a program that creates a fixed set of workers to generate random numbers.

Jump to

Keyboard shortcuts

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