Top Banner
Golang design4concurrency v0.0.1 GoMAD Apr 2015
33
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 design4concurrency

Golangdesign4concurrency

v0.0.1 GoMAD Apr 2015

Page 2: Golang design4concurrency

Alea Soluciones @eferro

@jaimegil

Page 3: Golang design4concurrency

Agenda

Example 1Example 2Code samplesReferencesConclusion

Page 4: Golang design4concurrency

First, an example

Page 5: Golang design4concurrency

As a customer I put sufficient coins into the vending machine and then press the selection button for coke and then press the button to vend and the robotic arm fetches a coke and dumps it into the pickup tray. The coke is nice and cold because the cooling system keeps the air in the

vending machine at 50 degrees.

Page 6: Golang design4concurrency

Nouns (potential clases):Customer, coins, vending machine, selection button, coke, button to vend, robotic arm, pickup tray, cooling system...

Page 7: Golang design4concurrency

Verbs (methods):Selection button: push

Vend button: pushRobotic arm: pickup(coke)

cooling system: cool forever...

Page 8: Golang design4concurrency

OO Solution:Class, methods

How to implement concurrent behaviours???

Page 9: Golang design4concurrency

OO Solution:Class, methods

How to implement concurrent behaviours???

Threads, threads pools, locks, debug, debug, debug...

Page 10: Golang design4concurrency
Page 11: Golang design4concurrency

One goroutine for each truly concurrent activity in the system

Goroutines are not SO processes

Goroutines are not threads

Goroutines are really cheap

Page 12: Golang design4concurrency

One goroutine for each truly concurrent activity in the system

Handling coins, Putting coins into the slots, Handling selections, Cooling the soda, Fetching the coke and putting into the pickup tray....

Page 13: Golang design4concurrency
Page 14: Golang design4concurrency

Another example

Page 15: Golang design4concurrency

As a network operator I can check in real time the state of each home router of our Fiber to the home network

(200,000 routers). The home routers send events with each state change to the central system.

Page 16: Golang design4concurrency

EventProcessor, DBEventSerializer, RouterStateCalculator, EventProcessorsPool...

Page 17: Golang design4concurrency
Page 18: Golang design4concurrency

One goroutine for each truly concurrent activity in the system

Receive events from home routers, Dispatch events, compute state for a home router...

200,000 routers = min 200,000 goroutines

Page 19: Golang design4concurrency
Page 20: Golang design4concurrency

One goroutine for each truly concurrent activity in the system

Goroutines are really cheapGoroutines are really cheapGoroutines are really cheap

Page 21: Golang design4concurrency

Small mem footprintsFast creation

Fasts scheduling

Goroutines

Page 22: Golang design4concurrency
Page 23: Golang design4concurrency

Never share memoryOnly send messages using channels

Communicate using channels

Page 24: Golang design4concurrency

Code Samples

https://github.com/aleasoluciones/golang_design4concurrency_talk/

Page 25: Golang design4concurrency
Page 26: Golang design4concurrency
Page 27: Golang design4concurrency
Page 28: Golang design4concurrency
Page 29: Golang design4concurrency
Page 30: Golang design4concurrency

Design for concurrencyDon't share memory

Lot of goroutines / they are cheap

Conclusions

Page 31: Golang design4concurrency

ReferencesGo concurrency patterns https://talks.golang.org/2012/concurrency.slide

Advanced Go concurrency patternshttps://blog.golang.org/advanced-go-concurrency-patterns

@martinjlogan Designing for Actor Based Systemshttp://blog.erlware.org/designing-for-actor-based-systems/

Golang patterns for serving on-demand, generated contenthttp://blog.gitorious.org/2014/08/11/golang-patterns-for-serving-on-demand-generated-content/

Page 32: Golang design4concurrency

Questions???@eferro

@jaimegil

Page 33: Golang design4concurrency

Thanks !!!@eferro

@jaimegil