Concurrency in Go video course with in depth explanations & examples

Overview

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

a tool for creating exploited media files for discord

Discord-Exploits A program for creating exploited media files for discord written in Go. Usage discord-exploits is a command line utility, meaning you

schmenn 194 Jul 22, 2021
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

Ryan Haksi 40 Nov 28, 2020
The Terraform Provider for Factorio

Terraform Provider for Factorio "Infrastructure as Code" for your factory. readme-demo-v-short.mp4 Current Status: Barely functional and mostly useles

null 130 Jul 5, 2021
Helpfully Functional Go like underscore.js

/\ \ __ __ ___ \_\ \ __ _ __ ____ ___ ___ _ __ __ __ __

null 342 Jul 14, 2021
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

Walter Schulze 901 Jul 22, 2021
Robust & Easy to use struct mapper and utility methods for Go

go-model Robust & Easy to use model mapper and utility methods for Go struct. Typical methods increase productivity and make Go development more fun ?

Jeevanandam M. 310 Jul 14, 2021
Reload Go code in a running process at function/method level granularity

got reload? Function/method-level stateful hot reloading for Go! Status Very much work in progress.

null 32 Jul 14, 2021
sigbypass4xx is a utility to automate well-know techniques used to bypass access control restrictions.

sigbypass4xx sigbypass4xx is a utility to automate well-know techniques used to bypass access control restrictions. Resources Usage Installation From

Signed Security 3 Jul 12, 2021
Molecule is a Go library for parsing protobufs in an efficient and zero-allocation manner

Molecule Molecule is a Go library for parsing protobufs in an efficient and zero-allocation manner. The API is loosely based on this excellent Go JSON

Richard Artoul 339 Jul 7, 2021
Procswap is a simple application that allows you to prioritize processes on a Windows machine.

Procswap is a simple application that allows you to prioritize processes on a Windows machine.

Billy 13 May 31, 2021
a thread-safe concurrent map for go

concurrent map As explained here and here, the map type in Go doesn't support concurrent reads and writes. concurrent-map provides a high-performance

Or Hiltch 2.4k Jul 25, 2021
Clojure-esque extension language for Go.

let-go Greetings loafers! (Ī»-gophers haha, get it?) This is supposed to be a compiler and bytecode VM for a language resembling Clojure as close as po

Marcin Gasperowicz 38 Jun 24, 2021
Experiments with Go generics

generics Quick experiments with Go generics algebra, a generic square root function for float, complex and and rational. future, a concurrent cache ("

Alan Donovan 53 Jul 19, 2021
A directory of hardware related libs, tools, and tutorials for Go

Go + hardware This repo is a directory of tools, packages and tutorials to let you introduce Go in your hardware projects. Why Go? Go can target platf

Jaana Dogan 1.2k Jul 19, 2021