Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Golang concurrency design

559 views

Published on

Golang concurrency design

Published in: Software

Golang concurrency design

  1. 1. Moore's law
  2. 2. The modern world is parallel
  3. 3. 이론
  4. 4. This is what typically happens in your computer 현실
  5. 5. 1. Concurrency 1. Concurrency and Parallelism 2. Models for Programming Concurrency 2. Concurrency Concepts in Golang 1. Communicating Sequential Processes - Goroutine - Channel - Select 2. Golang Scheduler 3. Best practices for concurrency Contents
  6. 6. Concurrency
  7. 7. Parallelism
  8. 8. Concurrency vs. Parallelism
  9. 9. 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
  10. 10. Lock Condition
  11. 11. Software Transactional Memory
  12. 12. Easy to understand. Easy to use. Easy to reason about. You don't need to be an expert! A model for software construction
  13. 13. Green Threads user-level kernel-level
  14. 14. Actor
  15. 15. Communicating Sequential Processes Actor와 다른점은 Actor 는 pid에게 전달을 하지만 CSP는 anonymous로 channel로 전달함
  16. 16. Go루틴(Goroutines) • OS 쓰레드보다 경량화된 Go 쓰레드 • 같은 메모리 공간을 공유해서 다른 Goroutines을 병렬로 실행한다. • 스택에서 작은 메모리로 시작 필요에 따라 힙 메모리에 할당/해제 • 프로그래머는 쓰레드를 처리하지 않고 마찬가지로 OS도 Goroutine의 존재를 모른다. • OS관점에서는 Goroutine은 C program의 event-driven형식처럼 작동한다.
  17. 17. 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.
  18. 18. Goroutines blocking •network input •sleeping •channel operations or •blocking on primitives in the sync package. •call function (case by case)
  19. 19. Channel
  20. 20. Unbuffered Channel
  21. 21. Buffered Channel
  22. 22. channel channel channel Select Select
  23. 23. 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 예제
  24. 24. 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
  25. 25. func main() { runtime.GOMAXPROCS(1) go func() { for { } }() log.Println("Bye") } 예제
  26. 26. GOMAXPROCS GOMAXPROCS는 동시에 일을 시킬수 있는 
 CPU의 최대 수를 설정하는 변수 GOMAXPROCS=1 GOMAXPROCS=24
  27. 27. Concurrency
  28. 28. Parallelism
  29. 29. Go Scheduler https://www.goinggo.net/2015/02/scheduler-tracing-in-go.html
  30. 30. Go Scheduler •M: OS threads. •P: Context to run Go routines. •G: Go routine. M0 P G0 G G G
  31. 31. Goroutines blocking •network input •sleeping •channel operations or •blocking on primitives in the sync package. •call function (case by case)
  32. 32. Blocking syscalls M1 P G1
 (syscall) G2 G3 G4 M1 P G3 G4 G2
 (running) M2 G1
 (wait) M1 P G1
 (running) G2 G3 G4 M2
 storage
  33. 33. Runtime Poller 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
  34. 34. GOMAXPROCS GOMAXPROCS = 2 M0 P G0 G G G
  35. 35. 이론
  36. 36. 현실
  37. 37. Best practices for concurrency func 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") } 예제
  38. 38. Best practices for concurrency func main() { ch := make(chan int) go func(c chan int) { for { select { case <-c: log.Println("Hi") } } }(ch) go func() { for { ch <- 1 time.Sleep(1000 * time.Millisecond) } }() time.Sleep(5000 * time.Millisecond) } 예제
  39. 39. 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 } } }
  40. 40. 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
  41. 41. Best practices for concurrency HTTP Server https://www.techempower.com/benchmarks/previews/round13/#section=data- r13&hw=azu&test=fortune
  42. 42.
  43. 43. Q&A

×