- Last Updated: [[2020-12-19]]
- The Go Scheduler is an external monitor that oversees [[Goroutines]] in [[Go]] and contributes significantly to [making Go extremely performant]([[Go is built with performance in mind]])
- # Functions of the Go Scheduler
- Traffic cop
- Assigns goroutines to [[Kernel thread]]s
- Makes blocking calls to stop both kernel threads and goroutines
- Manages network I/O
- Redirects work when necessary ([[Work-stealing]] and [[Work hand-off]])
- Janitor
- Engages garbage collection
- # Features of the Go Scheduler
- Allows for high concurrency (hundreds of thousands of [[Goroutines]] can run at the same time)
- [[Distributed runqueue]] for each thread and a global runqueue prevent file locking from the runqueue being accessed simultaneously
- Allows for parallelism (if machines have more than one core, Go apps can take advantage of the extra capability)
- [[Throttling access to resource]] (runqueue) to the number of cores is future-proofing while reducing the chances of contention.
- Minimizes the amount of kernel threads created, destroyed, and used in general (this is an expensive process)
- [[Thread reuse]] means that threads end up running more than one goroutine, [amortizing the cost]([[Amortizing performance cost]]) of a goroutine over the life of the thread.
- [[Work-stealing]] between threads minimizes the amount of time that threads are idle, and decreases overall processing time.
- [[Work hand-off]] lets the Go Scheduler step in with its own global runqueue to take work from one thread and give it to another if the first thread is blocked by a long-running goroutine.
- A background thread called `sysmon` imposes a form of [[Timeboxing]]: when a goroutine runs for longer than 10 ms, it forces the goroutine to yield to the Go Scheduler.
- # Limitations
- "Runqueues are FIFO, not priority-based (unlike Linux)"
- "No strong preemption"
- "The global runqueue of the scheduler is lesser priority, so a long-running goroutine could still bring the app to a halt."
- "Go doesn't know about the hardware of the machine it's operating in."
- # References
- [[The Scheduler Saga]]
- [[Go is built with performance in mind]].