Go Synchronized
Andrej Vckovski
What I want to transmit

 Go is useful and neat for heavily
concurrent applications
 Webrockets socks!

Go synchronized

2
A small poll

Point your [smartphone] browser to

nca.me/l
Go synchronized

3
nca.me/l

Go synchronized

4
nca.me/l

Go synchronized

5
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
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
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
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: %sn", 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
The application:
Very Instant Massive (Audience) Polling
 Presentations
like this one
 TV shows with
added
interactivity
 Pause
entertainment in
a stadium
 Flipped
Classrooms
Go synchronized

© Nhenze

© Chris Lawrence

© Loozrboy

© Jeff Chenqinyi

10
Browser
JS
that
displays
questions
and
allows
voting

System context

go

Browser
JS
that
displays
questions
and
allows
voting

ipoll
server

Presentation Software (e.g., PowerPoint)

JS
Browser
JS
that
displays
questions
and
allows
voting

Go synchronized

Browser that displays
voting results

11
Browser
JS
that
displays
questions
and
allows
voting

WebSockets

go

 Full-duplex conversation
ipoll
over TCP connectionserver
 JS
RFC 6455
 Available in most modern
browsers
 Simple JavaScript binding
JS
 Handshake by HTTP then
,
user-defined messages over
the same socket

Client
(Browser)
HTTP GET Request,
special attributes
HTTP response
“switch protocol”

Browser
that
displays
questions
and
allows
voting

Browser
that
displays
questions
and
allows
voting

Go synchronized

Server

Message

Presentation Software (e.g., PowerPoint)

JS

Message
Message
Browser that displays
voting results
Message

Message
12
Multiplexer
and
Demultiplexer

Browser
that
displays
questions
and
allows
voting

Browser
that
displays
questions
and
allows
voting

Browser
that
displays
questions
and
allows
voting

Go synchronized

ipoll
server

Presentation Software (e.g., PowerPoint)

Browser that displays
voting results

13
Small Demo

Point your smartphone browser to

nca.me/l
Go synchronized

14
Demo: New Question 1

nca.me/l
Go synchronized

15
Demo: New Question 2

nca.me/l
Go synchronized

16
Demo: New Question 3

nca.me/l
Go synchronized

17
WebSocket on server side (go)
func startWebserver() {
// [...]

http.HandleFunc("/svy", surveyHandler)
}

Go

// [...]

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 {
// [...]
}
}
synchronized
}

18
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
Conclusions

 Go (or “CSP”-design) has massively
simplified the concurrency challenges
 WebSockets are easy to use and will be
increasingly popular

Go synchronized

20
http://golang.org
Rob Pike on “concurrency is not parallelism”:
http://vimeo.com/49718712
(mascot by Renée French)
Bonus poll

Go synchronized

nca.me/l

22
Thanks for the attention!
andrej.vckovski@netcetera.com
netcetera.com
ipoll.ch
(coming soon)

JAZOON'13 - Andrej Vckovski - Go synchronized

  • 1.
  • 2.
    What I wantto transmit  Go is useful and neat for heavily concurrent applications  Webrockets socks! Go synchronized 2
  • 3.
    A small poll Pointyour [smartphone] browser to nca.me/l Go synchronized 3
  • 4.
  • 5.
  • 6.
    About go  Startedby 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
  • 7.
    Concurrency?  Separate, potentiallysimultaneously 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
  • 8.
    Go and concurrency CommunicatingSequential 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
  • 9.
    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: %sn", 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
  • 10.
    The application: Very InstantMassive (Audience) Polling  Presentations like this one  TV shows with added interactivity  Pause entertainment in a stadium  Flipped Classrooms Go synchronized © Nhenze © Chris Lawrence © Loozrboy © Jeff Chenqinyi 10
  • 11.
    Browser JS that displays questions and allows voting System context go Browser JS that displays questions and allows voting ipoll server Presentation Software(e.g., PowerPoint) JS Browser JS that displays questions and allows voting Go synchronized Browser that displays voting results 11
  • 12.
    Browser JS that displays questions and allows voting WebSockets go  Full-duplex conversation ipoll overTCP connectionserver  JS RFC 6455  Available in most modern browsers  Simple JavaScript binding JS  Handshake by HTTP then , user-defined messages over the same socket Client (Browser) HTTP GET Request, special attributes HTTP response “switch protocol” Browser that displays questions and allows voting Browser that displays questions and allows voting Go synchronized Server Message Presentation Software (e.g., PowerPoint) JS Message Message Browser that displays voting results Message Message 12
  • 13.
  • 14.
    Small Demo Point yoursmartphone browser to nca.me/l Go synchronized 14
  • 15.
    Demo: New Question1 nca.me/l Go synchronized 15
  • 16.
    Demo: New Question2 nca.me/l Go synchronized 16
  • 17.
    Demo: New Question3 nca.me/l Go synchronized 17
  • 18.
    WebSocket on serverside (go) func startWebserver() { // [...] http.HandleFunc("/svy", surveyHandler) } Go // [...] 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 { // [...] } } synchronized } 18
  • 19.
    Core data massagedat 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
  • 20.
    Conclusions  Go (or“CSP”-design) has massively simplified the concurrency challenges  WebSockets are easy to use and will be increasingly popular Go synchronized 20
  • 21.
    http://golang.org Rob Pike on“concurrency is not parallelism”: http://vimeo.com/49718712 (mascot by Renée French)
  • 22.
  • 23.
    Thanks for theattention! andrej.vckovski@netcetera.com netcetera.com ipoll.ch (coming soon)