jazoon'13 - andrej vckovski - go synchronized
DESCRIPTION
http://guide13.jazoon.com/#/submissions/125TRANSCRIPT
![Page 1: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/1.jpg)
Go Synchronized Andrej Vckovski
![Page 2: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/2.jpg)
What I want to transmit
Go is useful and neat for heavily concurrent applications Webrockets socks!
Go synchronized 2
![Page 3: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/3.jpg)
A small poll
Point your [smartphone] browser to
nca.me/l Go synchronized 3
![Page 4: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/4.jpg)
Go synchronized 4
nca.me/l
![Page 5: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/5.jpg)
Go synchronized 5
nca.me/l
![Page 6: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/6.jpg)
About go
Started by Robert Griesemer, Rob Pike and Ken Thompson in Fall 2007, Open-sourced 2009, Go 1 March 2012
Main features – C/Pascal/Modula-like, garbage collected, no pointer
arithmetics, type safe, very fast compiler – Simple type system without hierarchies – Concepts for concurrent programming in the “CSP”-style – Embeds dependency management – Today about similar memory/execution-performance as
Java or Node.
Go synchronized 6
![Page 7: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/7.jpg)
Concurrency?
Separate, potentially simultaneously executed computations
Usually somehow interacting Candidates to leverage multi-{core|cpu|node}
environments Usually but not necessarily correspond to multiple users Often on the server side
And hard to make it right
Go synchronized 7
![Page 8: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/8.jpg)
Go and concurrency
Communicating Sequential Processes (CSP) pattern (C. A. R. Hoare, 1978) 1. Channels 2. Go-routines 3. select-statement
“Do not communicate by sharing memory; instead, share memory by communicating”
Occam, Erlang, Newsqueak, Limbo, Clojure and many more
Go synchronized 8
![Page 9: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/9.jpg)
Example
package main import ("fmt"; "time"; "math/rand") func main() { table := make(chan string) for _, player := range [...]string{"alice", "bob"} { go func(who string) { num := 0; for { ball := <- table; fmt.Printf("player %s: %s\n", who, ball) table <- fmt.Sprintf("tick-%s-%d",who,num) time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) num++ } }(player) } table <- "go" time.Sleep(10 * time.Second) }
Go synchronized 9
![Page 10: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/10.jpg)
The application: Very Instant Massive (Audience) Polling Presentations
like this one TV shows with
added interactivity
Pause entertainment in a stadium
Flipped Classrooms
© Loozrboy
© Chris Lawrence
© Jeff Chenqinyi
© Nhenze
Go synchronized 10
![Page 11: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/11.jpg)
Browser that
displays questions
and allows voting
Presentation Software (e.g., PowerPoint)
Browser that displays voting results
ipoll server
System context
Go synchronized 11
Browser that
displays questions
and allows voting
Browser that
displays questions
and allows voting
go
JS
JS
JS
JS
![Page 12: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/12.jpg)
Browser that
displays questions
and allows voting
Presentation Software (e.g., PowerPoint)
Browser that displays voting results
ipoll server
Browser that
displays questions
and allows voting
Browser that
displays questions
and allows voting
go
JS
JS
JS
JS
WebSockets
Full-duplex conversation over TCP connection
RFC 6455 Available in most modern
browsers Simple JavaScript binding Handshake by HTTP, then
user-defined messages over the same socket
Client (Browser) Server
HTTP GET Request, special attributes
HTTP response “switch protocol”
Message
Message
Message
Message
Message
Go synchronized 12
![Page 13: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/13.jpg)
Browser that
displays questions
and allows voting
Presentation Software (e.g., PowerPoint)
Browser that displays voting results
ipoll server
Multiplexer and
Demultiplexer
Go synchronized 13
Browser that
displays questions
and allows voting
Browser that
displays questions
and allows voting
![Page 14: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/14.jpg)
Small Demo
Point your smartphone browser to
nca.me/l Go synchronized 14
![Page 15: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/15.jpg)
Demo: New Question 1
Go synchronized 15
nca.me/l
![Page 16: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/16.jpg)
Demo: New Question 2
Go synchronized 16
nca.me/l
![Page 17: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/17.jpg)
Demo: New Question 3
Go synchronized 17
nca.me/l
![Page 18: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/18.jpg)
WebSocket on server side (go) func startWebserver() {
// [...]
http.HandleFunc("/svy", surveyHandler)
// [...]
}
func surveyHandler(c http.ResponseWriter, req *http.Request) { // [...]
go websocket.Handler(func (ws *websocket.Conn) { s.VoterHandler(ws)} ).ServeHTTP(c, req) // [...]
}
func (s *Survey) VoterHandler(ws *websocket.Conn) { defer func() { s.voterChan <- voter{ws, false} log.Printf("connection closed by client") ws.Close() }() s.voterChan <- voter{ws, true} // notify hub of a new voter for { // [...]
len, err := ws.Read(buf) // [...]
var v vote err = json.Unmarshal(buf, &v) if err == nil { s.voteChan <- v } else { // [...]
} } }
Go synchronized 18
![Page 19: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/19.jpg)
Core data massaged at a single place: The “model” implements an event-loop
func (s *Survey) surveyHub() { // [...] t := time.NewTicker(100 * time.Millisecond) // [...] for { select { case _ = <-t.C: // tick arrived // [...] case ctrl := <-s.controlChan: // control message // [...] case voter := <-s.voterChan: // voter subscribed // [...] case viewer := <-s.viewerChan: // viewer subscribed // [...] case admin := <-s.adminChan: // admin subscribed // [...] case vote := <-s.voteChan: // a vote arrived // [...] } } }
19
![Page 20: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/20.jpg)
Conclusions
Go (or “CSP”-design) has massively simplified the concurrency challenges WebSockets are easy to use and will be
increasingly popular
Go synchronized 20
![Page 21: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/21.jpg)
(mascot by Renée French)
Rob Pike on “concurrency is not parallelism”: http://vimeo.com/49718712
http://golang.org
![Page 22: JAZOON'13 - Andrej Vckovski - Go synchronized](https://reader034.vdocuments.site/reader034/viewer/2022052522/554dceb3b4c905c70e8b45cd/html5/thumbnails/22.jpg)
Bonus poll
Go synchronized 22
nca.me/l