Top Banner
48

Golang concurrency design

Apr 16, 2017

Download

Software

Hyejong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Golang concurrency design
Page 2: Golang concurrency design

Moore's law

Page 3: Golang concurrency design

The modern world is parallel

Page 4: Golang concurrency design

이론

Page 5: Golang concurrency design

This is what

typically

happens

in your

computer

현실

Page 6: Golang concurrency design
Page 7: Golang concurrency design

1. Concurrency 1. Concurrency and Parallelism

2. Models for Programming Concurrency2. Concurrency Concepts in Golang 1. Communicating Sequential Processes

-Goroutine -Channel -Select

2. Golang Scheduler3. Best practices for concurrency

Contents

Page 8: Golang concurrency design

Concurrency

Page 9: Golang concurrency design

Parallelism

Page 10: Golang concurrency design

Concurrency vs. Parallelism

Page 11: Golang concurrency design

Concurrency is about dealing with lots of things at once.Parallelism is about doing lots of things at once.Not the same, but related.Concurrency is about structure, parallelism is about execution.Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.

By Rob Pike

Concurrency is not parallelism

Page 12: Golang concurrency design

Lock Condition

Page 13: Golang concurrency design
Page 14: Golang concurrency design

Software Transactional Memory

Page 15: Golang concurrency design

Easy to understand.Easy to use.Easy to reason about.You don't need to be an expert!

A model for software construction

Page 16: Golang concurrency design

Green Threads

user-level

kernel-level

Page 17: Golang concurrency design

Actor

Page 18: Golang concurrency design

Communicating Sequential Processes

Actor와 다른점은 Actor 는 pid에게 전달을 하지만 CSP는 anonymous로 channel로 전달함

Page 19: Golang concurrency design

Go루틴(Goroutines)

• OS 쓰레드보다 경량화된 Go 쓰레드

• 같은 메모리 공간을 공유해서 다른 Goroutines을 병렬로 실행한다.

• 스택에서 작은 메모리로 시작 필요에 따라 힙 메모리에 할당/해제

• 프로그래머는 쓰레드를 처리하지 않고 마찬가지로 OS도 Goroutine의 존재를 모른다.

• OS관점에서는 Goroutine은 C program의 event-driven형식처럼 작동한다.

Page 20: Golang concurrency design

Goroutines vs OS threads

Goroutines OS threads

Memory consumption 2kb 1mb

Setup and teardown costs pretty cheap high-priced

Switching costs (saved/restored)

Program Counter, Stack Pointer and DX.

ALL registers, that is, 16 general purpose registers, PC (Program Counter), SP (Stack Pointer), segment registers, 16 XMM

Goroutine stack size was decreased from 8kB to 2kB in Go 1.4.

Page 21: Golang concurrency design

Goroutines blocking

•network input

•sleeping

•channel operations or

•blocking on primitives in the sync package.

•call function (case by case)

Page 22: Golang concurrency design

Channel

Page 23: Golang concurrency design

Unbuffered Channel

Page 24: Golang concurrency design

Buffered Channel

Page 25: Golang concurrency design

channelchannel

channelSelect

Select

Page 26: Golang concurrency design

func main() {tick := time.Tick(100 * time.Millisecond)boom := time.After(500 * time.Millisecond)

for {select {case <-tick:fmt.Println("tick.")

case <-boom:fmt.Println("BOOM!")return

default:fmt.Println(" .")time.Sleep(50 * time.Millisecond)

}}

}

Select

예제

Page 27: Golang concurrency design

package main

func main() { // create new channel of type int ch := make(chan int)

// start new anonymous goroutine go func() { // send 42 to channel ch <- 42 }() // read from channel <-ch}

Example

Page 28: Golang concurrency design
Page 29: Golang concurrency design
Page 30: Golang concurrency design

func main() {runtime.GOMAXPROCS(1)

go func() {for {}

}()

log.Println("Bye")}

예제

Page 31: Golang concurrency design

GOMAXPROCSGOMAXPROCS는 동시에 일을 시킬수 있는 CPU의 최대 수를 설정하는 변수

GOMAXPROCS=1 GOMAXPROCS=24

Page 32: Golang concurrency design

Concurrency

Page 33: Golang concurrency design

Parallelism

Page 34: Golang concurrency design

Go Scheduler

https://www.goinggo.net/2015/02/scheduler-tracing-in-go.html

Page 35: Golang concurrency design

Go Scheduler

•M: OS threads.

•P: Context to run Go routines.

•G: Go routine.

M0

P

G0

G

G

G

Page 36: Golang concurrency design

Goroutines blocking

•network input

•sleeping

•channel operations or

•blocking on primitives in the sync package.

•call function (case by case)

Page 37: Golang concurrency design

Blocking syscalls

M1

P

G1(syscall)

G2

G3

G4

M1

P G3

G4G2(running)

M2

G1(wait)

M1

P

G1(running)

G2

G3

G4

M2storage

Page 38: Golang concurrency design

RuntimePoller

Network I/O call

M1

P

G1(I/O)

G2

G3

G4

M2M1

P G3

G4G2

G1(I/O)

M1

P

G1(I/O)

G2

G3

G4

Page 39: Golang concurrency design

GOMAXPROCS

GOMAXPROCS = 2

M0

P

G0

G

G

G

Page 40: Golang concurrency design

이론

Page 41: Golang concurrency design

현실

Page 42: Golang concurrency design

Best practices for concurrencyfunc main() {

var wg sync.WaitGroup

for i := 0; i < 10; i++ {wg.Add(1)go func(n int) {log.Println(n)wg.Done()

}(i)}

wg.Wait()fmt.Println("the end")

}

예제

Page 43: Golang concurrency design

Best practices for concurrencyfunc main() {

ch := make(chan int)go func(c chan int) {for {select {case <-c:log.Println("Hi")

}}

}(ch)

go func() {for {ch <- 1time.Sleep(1000 * time.Millisecond)

}}()

time.Sleep(5000 * time.Millisecond)}

예제

Page 44: Golang concurrency design

Best practices for concurrency

func main() {ctx, cancel := context.WithCancel(context.Background())defer cancel()

for n := range gen(ctx) {fmt.Println(n)if n == 5 {cancel()break

}}

}

Page 45: Golang concurrency design

Best practices for concurrency

msgs, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args)failOnError(err, "Failed to register a consumer")

forever := make(chan bool)

go func() { for d := range msgs { log.Printf("Received a message: %s", d.Body) }}()

log.Printf(" [*] Waiting for messages. To exit press CTRL+C")<-forever

MessageQueue(RabbitMQ) Worker

Page 46: Golang concurrency design

Best practices for concurrencyHTTP Server

https://www.techempower.com/benchmarks/previews/round13/#section=data-r13&hw=azu&test=fortune

Page 47: Golang concurrency design

Page 48: Golang concurrency design

Q&A