concurrency-with-go

module
v0.0.0-...-922015f Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2021 License: Apache-2.0

README

Go Concurrency

This is another study solely dedicated to understanding Concurrency and this will be learnt with Go, using the book Concurrency in Go by Katherine Cox-Buday as a guide.

I will be building small programs and making documentation of findings in any way possible.

Concurrency and Parallelism

Concurrency is a property of the code, Parallelism is a property of the running program. We write a concurrent code and hope that it is run in parallel. A concurrent code running on one core is not running in parallel but sequentially in a fast way.

Parallelism is a property of the runtime of our code.

Thoughts on Concurrency

Moore's law ("the number of components on an integrated circuit will double every 2 years") started losing it's impact till around the 2012, companies foresaw slowdown in rate of Moore's law and started looking for different ways in increasing computing power, this led to creating multicore processors, this led to solving problem in a simultaneous way (Parallelism) and this stem from Amdahl's law ("it entails, model the potential performance gains from implementing the solution to a problem in a parellel manner i.e gains are bounded by how much of the program must run in sequential manner").

Solving problems in a parallel manner led to Horizontal Scaling, having multiple instance of a program on different CPUs or machines.

Sections

  • Common Issues in Concurrency

  • Building blocks of Go Concurrency

  • Concurrency Patterns

Tools to analyze concurrent code

Race Detection

Running the following commands can help

    go run -race sample.go      // compile and run the program
    go test -race sample        // test the package
    go build -race sample       // build the command
    go install -race sample     // install the package
pprof

It is a tool that was created at Google and can display profile data either while a program is running or by consuming saved runtime statistics. More

It helps to answer these questions;

  • How your program is performing at runtime
  • How many goroutines are running
  • If the CPU is being fully utilized
  • Memory consumption by the program

Directories

Path Synopsis
building_blocks
channels
Package main illustrated goroutines that take the place of ownership of a channeland another that handles reading from it.
Package main illustrated goroutines that take the place of ownership of a channeland another that handles reading from it.
goroutines/size
Package main aims to measure the amount of computer resources that was consumed to create goroutines
Package main aims to measure the amount of computer resources that was consumed to create goroutines
sync/cond_broadcast
Package main illustrates creating a GUI application with a button.
Package main illustrates creating a GUI application with a button.
sync/cond_signal
Package main illustrates a goroutine that is waiting for a signal and another goroutine that is sending the signals.
Package main illustrates a goroutine that is waiting for a signal and another goroutine that is sending the signals.
sync/mutex
Package main illustrated two goroutines that are attempting to increment or decrement a common value it uses a Mutex to synchronize access to memory
Package main illustrated two goroutines that are attempting to increment or decrement a common value it uses a Mutex to synchronize access to memory
sync/network_service_1
Package main simulates creating a connection without Pool to a service that takes time to respond.
Package main simulates creating a connection without Pool to a service that takes time to respond.
sync/network_service_2
Package main simulates how performant creating a connection with Pool to a service that takes time to respond.
Package main simulates how performant creating a connection with Pool to a service that takes time to respond.
sync/pool_1
Package main illustrates the use of sync.Pool, Pool's primary interface is its Get method.
Package main illustrates the use of sync.Pool, Pool's primary interface is its Get method.
sync/rwmutex
Package main illustrates the use of RWMutex to demonstrate a producer that is less active than the numerous consumers created.
Package main illustrates the use of RWMutex to demonstrate a producer that is less active than the numerous consumers created.
sync/waitgroup
Package main illustrates the usage of waitgroup
Package main illustrates the usage of waitgroup
Package main illustrates an opinionated way on how to handle error propagation in a complex system
Package main illustrates an opinionated way on how to handle error propagation in a complex system
Package main is the implementation of the logic that monitors the health of a goroutine, to achieve this, it needs a reference to a function that can start the goroutine.
Package main is the implementation of the logic that monitors the health of a goroutine, to achieve this, it needs a reference to a function that can start the goroutine.
Package main illustrates a concurrent code, goroutine that exposes a heartbeat.
Package main illustrates a concurrent code, goroutine that exposes a heartbeat.
patterns
bridge_channel
Package main is the implementation of a bridge-channel pattern
Package main is the implementation of a bridge-channel pattern
cancellation/terminate_2
Package main illustrates cancellation of a goroutine blocked by on attempting to write value to a channel
Package main illustrates cancellation of a goroutine blocked by on attempting to write value to a channel
confinement/ad_hoc
Package main has the implementation of a ad hoc confinement concurrency pattern
Package main has the implementation of a ad hoc confinement concurrency pattern
confinement/lexical
Package main has the implementation of a lexical confinement concurrency pattern, I favour this pattern over the ad hoc pattern
Package main has the implementation of a lexical confinement concurrency pattern, I favour this pattern over the ad hoc pattern
context/cancellation
Package main illustrate the use a context for cancellation instead of a done channel
Package main illustrate the use a context for cancellation instead of a done channel
context/data_storage
Package main illustrates using context as a data storage or keep to store and retrive request-scoped data.
Package main illustrates using context as a data storage or keep to store and retrive request-scoped data.
fan_in_out
Package main illustrated a fan-out, fan-in pattern using it to find prime numbers in a stream of data
Package main illustrated a fan-out, fan-in pattern using it to find prime numbers in a stream of data
handle_error
Package main illustrates how to handle errors from goroutines
Package main illustrates how to handle errors from goroutines
or_done
Package main is the implementation of the or-done-channel pattern
Package main is the implementation of the or-done-channel pattern
pipeline
Package main illustrates a best practice pattern for creating pipelines
Package main illustrates a best practice pattern for creating pipelines
Package main is an implementation of the token bucket algorithm to rate limit requests.
Package main is an implementation of the token bucket algorithm to rate limit requests.
Package main illustrates how to replicate a simulated requests over a certain number of handlers
Package main illustrates how to replicate a simulated requests over a certain number of handlers

Jump to

Keyboard shortcuts

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