Concurrency in Go video course with in depth explanations & examples

Concurrency in Go

cover image

Summary

Coding Examples

Presentation Notes

Go routines

A go routines can block for one of these reasons:

  • Sending/Receiving on channel
  • Network or I/O
  • Blocking System Call
  • Timers
  • Mutexes

Here's the full list of Go routines statuses:

  • Gidle, // 0
  • Grunnable, // 1 runnable and on a run queue
  • Grunning, // 2 running
  • Gsyscall, // 3 performing a syscall
  • Gwaiting, // 4 waiting for the runtime
  • Gmoribund_unused, // 5 currently unused, but hardcoded in gdb scripts
  • Gdead, // 6 goroutine is dead
  • Genqueue, // 7 only the Gscanenqueue is used
  • Gcopystack, // 8 in this state when newstack is moving the stack

Feel free to check the rest of the statuses in the runtime source code

Fairness

  • Infinite loop — preemption (~10ms time slice)
  • Local Run queue — preemption (~10ms time slice)
  • Global run queue starvation is avoided by checking the global run queue for every 61 scheduler tick
  • Network Poller Starvation Background thread poll network occasionally if not polled by the main worker thread

Channels

Here are couple of simple rules to make sure channels are used correctly

  • Before writing to a channel, make sure someone else is reading from it (deadlock)
  • Before reading from a channel, make sure someone else is writing to it (deadlock)
  • When ranging over a channel, ALWAYS make sure the producer closes the channel eventually (deadlock)
  • Writing to a closed channel will result in a runtime panic
  • Reading from a closed channel won't have any effects
  • A channel close, is considered a write operation

Mutexes

Wait Groups

Atomics

Poll Order

  • Local Run queue
  • Global Run queue
  • Network Poller
  • Work Stealing

Deadlocks

The Coffman Conditions are known as the techniques/conditions to help detect, prevent and correct deadlocks. The Coffman Conditions are as follows:

  • Mutual Exclusion

A concurrent process holds exclusive rights to a resource, at any one time.

  • Wait for Condition

A concurrent process must simultaneously hold a resource and be waiting for an additional resource.

  • No Preemption

A resource held by a concurrent process can only be released by that process

  • Circular Wait

A concurrent process (P1) must be waiting on a chain of other concurrent processes (P2), which are in turn waiting on it (P1)

Go Scheduler

Primarily the Go scheduler has the opportunity to get triggered on these events:

  • The use of the keyword go
  • Garbage collection
  • System calls (i.e. open file, read file, e.t.c.)
  • Synchronization and Orchestration (channel read/write)

P, M, G

Once the syscall exists Go tries to apply one of the rules:

  • try to acquire the exact same P, and resume the execution
  • try to acquire a P in the idle list and resume the execution
  • put the goroutine in the global queue and put the associated M back to the idle list

Goroutines do not go in the global queue only when the local queue is full; it is also pushed in it when Go inject a list of goroutines to the scheduler, e.g. from the network poller or goroutines asleep during the garbage collection

Spinning Threads

Net Poller

SysMon

sysmon is smart enough to not consume resources when there is nothing to do. Its cycle time is dynamic and depends on the current activity of the running program. The initial pace is set at 20 nanoseconds, meaning the thread is constantly looking to help. Then, after some cycles, if the thread is not doing anything, the sleep between two cycles will double until it reaches 10ms. If your application does not have many system calls or long-running goroutines, the thread should back off to a 10ms delay most of its time, giving a very light overhead to your application.

The thread is also able to detect when it should not run. Here are two cases:

  • The garbage collector is going to run. sysmon will resume when the garbage collector ends.
  • All the threads are idle, nothing is running.

Work Stealing

Here's how Go makes sure to equally distribute & balance work and make use of computer resources as efficient as possible:

  • pull work from the local queue
  • pull work from the global queue
  • pull work from network poller
  • steal work from the other P’s local queues

Tracing

GOMAXPROCS=2 GODEBUG=schedtrace=1000,scheddetail=1 go run main.go

Concurrency Patterns

Pipelines

In general terms a pipeline is a mechanism for inter-process communication using message passing, where the output of a pipeline is the input for the next pipeline.

Suppose that assembling one car requires three tasks that take 20, 10, and 15 minutes, respectively. Then, if all three tasks were performed by a single station, the factory would output one car every 45 minutes. By using a pipeline of three stations, the factory would output the first car in 45 minutes, and then a new one every 20 minutes.

Resources

Similar Resources

Self-hosted video-hosting website and video archival manager for Niconico, Bilibili, and Youtube

Self-hosted video-hosting website and video archival manager for Niconico, Bilibili, and Youtube

Self-hosted video-hosting website and video archival manager for Niconico, Bilibili, and Youtube

Jan 1, 2023

Go-video-preview-ffmpeg-wrapper - A simple helper wrapper to generate small webm video previews using ffmpeg, useful for web previews.

Go-video-preview-ffmpeg-wrapper A simple helper wrapper to generate small webm video previews using ffmpeg, useful for web previews. Getting Started u

Jan 5, 2022

Golang-video-screensaver - A work in progress Microsoft Windows video screensaver implemented in Go

golang-video-screensaver A work in progress Microsoft Windows video screensaver

Sep 5, 2022

depth is tool to retrieve and visualize Go source code dependency trees.

depth is tool to retrieve and visualize Go source code dependency trees. Install Download the appropriate binary for your platform from the Rele

Dec 30, 2022

🏗️ Fetch a specific commit without any history (shallow depth w/o cloning)

shallow-fetch-sha 🏗️ For a given git repository and commit, fetch and checkout just that commit without any history. This can be extremely useful in

Nov 27, 2021

Learn Golang in-depth by solving 15 Quizzes, 10 Exercises and 4 Projects

Learn Golang in-depth by solving 15 Quizzes, 10 Exercises and 4 Projects

Modern Go (Golang) - The Complete Beginners Guide 2021 Learn Go (Golang) in-dept

Jan 1, 2023

GitHub Action to identify a path of changed files on monorepos, with regex and depth validation.

github-action-go GitHub Action to identify a path of changed files on monorepos, with regex and depth validation. Example use-case is execution path f

Mar 1, 2022

Concurrency-safe Go caching library with expiration capabilities and access counters

cache2go Concurrency-safe golang caching library with expiration capabilities. Installation Make sure you have a working Go environment (Go 1.2 or hig

Jan 1, 2023

A simple logging interface that supports cross-platform color and concurrency.

A simple logging interface that supports cross-platform color and concurrency.

WLog Package wlog creates simple to use UI structure. The UI is used to simply print to the screen. There a wrappers that will wrap each other to crea

Sep 26, 2022

Concurrency-safe Go caching library with expiration capabilities and access counters

cache2go Concurrency-safe golang caching library with expiration capabilities. Installation Make sure you have a working Go environment (Go 1.2 or hig

Dec 31, 2022

Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022

A sync.WaitGroup with error handling and concurrency control

go-waitgroup How to use An package that allows you to use the constructs of a sync.WaitGroup to create a pool of goroutines and control the concurrenc

Dec 31, 2022

golang worker pool , Concurrency limiting goroutine pool

golang worker pool 中文说明 Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

Dec 19, 2022

gpool - a generic context-aware resizable goroutines pool to bound concurrency based on semaphore.

gpool - a generic context-aware resizable goroutines pool to bound concurrency. Installation $ go get github.com/sherifabdlnaby/gpool import "github.c

Oct 31, 2022

Structured Concurrency in Go

nursery: structured concurrency in Go RunConcurrently( // Job 1 func(context.Context, chan error) { time.Sleep(time.Millisecond * 10)

Dec 27, 2022

Concurrency limiting goroutine pool

workerpool Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting task

Dec 28, 2022

concurrency utilities

concurrent concurrent.Map: backport sync.Map for go below 1.9 concurrent.Executor: goroutine with explicit ownership and cancellable concurrent.Map be

Dec 28, 2022

Pholcus is a distributed high-concurrency crawler software written in pure golang

Pholcus is a distributed high-concurrency crawler software written in pure golang

Pholcus Pholcus(幽灵蛛)是一款纯 Go 语言编写的支持分布式的高并发爬虫软件,仅用于编程学习与研究。 它支持单机、服务端、客户端三种运行模式,拥有Web、GUI、命令行三种操作界面;规则简单灵活、批量任务并发、输出方式丰富(mysql/mongodb/kafka/csv/excel等

Dec 30, 2022

Helper library for full uint64 randomness, pool backed for efficient concurrency

fastrand64-go Helper library for full uint64 randomness, pool backed for efficient concurrency Inspired by https://github.com/valyala/fastrand which i

Dec 5, 2021
Comments
  • Prevent race condition on server connection

    Prevent race condition on server connection

    Probably not gonna occur in our case, but surely a good pattern to follow. Explained in : https://www.youtube.com/watch?v=DqHb5KBe7qI starting at 16:20

  • Preserve intended logic

    Preserve intended logic

    in atomics/rage/main.go the first 3 routines perform addition or subtraction, not an update

    would be useful for learning fellows to have the video updated, too (overlay comment)

Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022
Helper library for full uint64 randomness, pool backed for efficient concurrency

fastrand64-go Helper library for full uint64 randomness, pool backed for efficient concurrency Inspired by https://github.com/valyala/fastrand which i

Dec 5, 2021
Project developed for the course Software Systems Analysis and Design (SSAD) at IU in F21 semester.

Go knowledge yield summary Project description Project developed for the course Software Systems Analysis and Design (SSAD) at IU in F21 semester. Eva

Sep 17, 2022
This project provides some working examples using Go and Hotwire Turbo.

hotwire-golang-website This project provides some working examples using Go the hotwire/turbo library published by basecamp.

Dec 29, 2022
Examples of accepting interfaces and returning structs

Go Interfaces When I started writing tests in Go, I sometimes used interfaces to mock things in tests. Then I discovered that this is not the correct

Dec 27, 2022
Go 1.18 generics use cases and examples

Go 1.18 generics use cases What are generics? See Type Parameters Proposal. How to run the examples? As of today, gotip is the simplest way to run the

Jan 10, 2022
A faster RWLock primitive in Go, 2-3 times faster than RWMutex. A Go implementation of concurrency control algorithm in paper

Go Left Right Concurrency A Go implementation of the left-right concurrency control algorithm in paper <Left-Right - A Concurrency Control Technique w

Jan 6, 2023
Go-concurrency-patterns - Sample concurrency patterns with Goroutines

About This sample project provides some concurrency pattern examples in Go using

Feb 21, 2022
Concurrency Lab examples on Go
Concurrency Lab examples on Go

Concurrency Lab 1 If you're stuck look at examples on Go by Example Using the lab sheet There are two ways to use the lab sheet, you can either: creat

Oct 22, 2021
This is the course materials for the Go Data Structures Crash Course!

Go Data Structures Course ?? Welcome Gophers! This is the official repository that contains all of the data structures we cover in the Go Data Structu

May 10, 2022